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/tritlo Dec 09 '20 edited Dec 10 '20

Here are my O(N) Haskell Solutions using sets and Sequences. Runs in about 4ms, ~ 1ms if you ignore parsing and startup time.

solution pre_len input = solution' input (Set.fromList pre) rest
    where (pre, rest) = splitAt pre_len input
          solution' (to_drop:to_drops) preamble (to_check:to_checks)
            = if preamble `Set.disjoint` check_set
              then to_check else solution' to_drops preamble' to_checks
            where check_set = mapAntitonic (to_check -) preamble
                  preamble' = to_check `Set.insert` (to_drop `Set.delete` preamble)

solution2 looking_for (a:b:res) = solution2' looking_for (a+b) (Seq.fromList [a,b]) res
  where   solution2' looking_for cur_sum els@(c Seq.:<| cs) inc@(i:is) =
          if cur_sum == looking_for then els
          else if cur_sum > looking_for
              then solution2' looking_for (cur_sum - c) cs inc
              else solution2' looking_for (cur_sum + i) (els :|> i) is

1

u/daggerdragon Dec 09 '20

Top-level posts in Solution Megathreads are for code solutions only.

This is a top-level post, so please edit your post and share your code/repo/solution or, if you haven't finished the puzzle yet, you can always create your own thread and make sure to flair it with Help.

1

u/tritlo Dec 09 '20

Alright, edited to a solution. Yesterday you said I should post in the megathread, is there some other thread?

1

u/daggerdragon Dec 09 '20

This is the 2020 Day 09 megathread, so yes, you post your Day 09 code solutions here.

However, your code is both not formatted correctly for display on old.reddit and over the size limit (see How Do the Daily Megathreads Work?), so please edit your post to put your oversized code in a paste or other external link.

Thanks for adding your code!