DEV Community

Cover image for Inverse Captcha
Robert Mion
Robert Mion

Posted on

1 1

Inverse Captcha

Advent of Code 2017 Day 1

Part 1

  1. Circular lists from the beginning!
  2. Writing my working algorithm

Circular lists from the beginning!

  • I knew it was a recurring theme throughout the year
  • But I didn't know it started with Day 1

Writing my working algorithm

An outline of my algorithm

Split the input into an array of numbers
For each number
  Accumulate a sum, starting a 0
    If the next number (wrapping if necessary) is the same number
      Increment the sum by the number at the current index
    Otherwise, increment the sum by 0...leaving it unchanged
Enter fullscreen mode Exit fullscreen mode

A reduce() that leverages all of the possible parameters:

return input
  .split('')
  .map(Number)
  .reduce((acc, curr, index, RA) =>
    acc += RA[index] == RA[(index + 1) % RA.length]
        ? curr : 0
  , 0)
Enter fullscreen mode Exit fullscreen mode

Part 2

Adjusting my working algorithm

Instead of checking the next index, I'll check the index half a list away.

An outline of my algorithm

Split the input into an array of numbers
For each number
  Accumulate a sum, starting a 0
    If the number half a list length away (wrapping if necessary) is the same number
      Increment the sum by the number at the current index
    Otherwise, increment the sum by 0...leaving it unchanged
Enter fullscreen mode Exit fullscreen mode

A small tweak to the number being added to index, and Voila!

return input
  .split('')
  .map(Number)
  .reduce((acc, curr, index, RA) =>
    acc += RA[index] == RA[(index + (RA.length / 2)) % RA.length]
        ? curr : 0
  , 0)
Enter fullscreen mode Exit fullscreen mode

I did it!!

  • I solved both parts!
  • Using one long statement comprised of chained array methods!
  • Where both parts only differ by a single operand!

Year in review

Since I worked in reverse, today was the first time I saw this animation:

Snapshot of this year's map

This static map should which puzzles I made simulators for:
Map with Simulators indicated

  • 46 stars! A new personal best!
  • 21 two-star days! A new personal best!
  • 4 one-star days! A new personal best!
  • 0 no-star days! A new personal best!
  • 8 simulators built! A tie with 2018 for lowest amount.
  • Tons of GIFs created...many which helped me solve the puzzle!

It's been a blast, 2017!

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay