r/adventofcode • • Dec 09 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 09 Solutions -🎄-

NEW AND NOTEWORTHY

Advent of Code 2020: Gettin' Crafty With It

  • 13 days remaining until the submission deadline on December 22 at 23:59 EST
  • Full details and rules are in the Submissions Megathread

--- Day 09: Encoding Error ---


Post your solution in this megathread. Include what language(s) your solution uses! If you need a refresher, the full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:06:26, megathread unlocked!

41 Upvotes

995 comments sorted by

View all comments

2

u/Krakhan Dec 09 '20 edited Dec 09 '20

Ruby

I'm liking how versatile arrays are in Ruby. A bit disappointed that it seems weird to break out of a nested loop though, but if anyone has better suggestions for doing this I'm open to suggestions. :)

paste

2

u/petercooper Dec 09 '20

One way is to refactor out the nested loop. each_cons would assist in this situation. Consider that arr.each_cons(3) will return an enumerator of each block of 3 elements. See my solution elsewhere on this post if you want to see it in situ.

2

u/Intro245 Dec 09 '20

One solution for this problem that works in most languages is to put the loops into a separate function and use return to break out. So in this case, the return value could be either contiguous_numbers or the solution to part 2.

1

u/daggerdragon Dec 09 '20

As per our posting guidelines in the wiki under How Do the Daily Megathreads Work?, please edit your post to put your oversized code in a paste or other external link.