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!

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

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

Okay