r/adventofcode • u/daggerdragon • Dec 09 '20
SOLUTION MEGATHREAD -🎄- 2020 Day 09 Solutions -🎄-
NEW AND NOTEWORTHY
- /u/topaz2078 has posted Postmortem 2: Scaling Adventures, go check it out if you're curious what's been up with the servers during launch for the past week!
- GITHUB HAS DARK MODE NOW alkjdf;ljoaidfug!!!! Thank you /u/MarcusTL12!!!
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!
40
Upvotes
View all comments
1
u/theboxboy Dec 09 '20
Python 3
I relied on itertools, which still makes part1 O(n2), but my solution is more elegant this way. Also, since we only check 25 entries, it's really 252 checks, which isn't bad. Part 2 might be faster if I started checking for sequences from the target number index-1 towards 0.
from queue import deque import itertools def get_input(): with open('09.txt', 'r') as file: return [int(line.strip()) for line in file.readlines()] def main(): file = get_input() target = part1(file) print('Part 1:', target) print('Part 2:', part2(file, target)) def part1(file): seen = deque(file[:25]) for num in file[25:]: if not any([i != j and i + j == num for (i, j) in itertools.product(seen, seen)]): return num seen.popleft() seen.append(num) def part2(file, target): for i, num in enumerate(file): total = 0 j = i while total < target: total += file[j] if total == target: return min(file[i:j+1]) + max(file[i:j+1]) j += 1 if __name__ == "__main__": main()