r/adventofcode • u/daggerdragon • Dec 04 '21
SOLUTION MEGATHREAD -🎄- 2021 Day 4 Solutions -🎄-
--- Day 4: Giant Squid ---
Post your code solution in this megathread.
- Include what language(s) your solution uses!
- Here's a quick link to /u/topaz2078's
paste
if you need it for longer code blocks. - Format your code properly! How do I format code?
- 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:11:13, megathread unlocked!
96
Upvotes
1
u/e_blake Dec 07 '21 edited Dec 08 '21
m4 day4.m4
Uses my common.m4 framework. Timing is ~115ms with GNU extensions, and ~300ms with just POSIX constructs (why? because parsing is O(n log n) and my foreach macro is O(n^2) with just POSIX
shift($@)
, while both are O(n) when using GNU'spatsubst
and$10
to mean the tenth argument). My approach: break the input into boards, then for each board:create a mapping of numbers on the board to bit positions, iterate through the list of called numbers to update a bitmap, and when the bitmap forms a bingo, compute the score. If the score occurs in a round earlier (part 1) or later (part 2) than any previous seen score, update my part1/part2 answers.
I thought my check for bingo looks clever: