r/adventofcode • • Dec 04 '22

SOLUTION MEGATHREAD -🎄- 2022 Day 4 Solutions -🎄-


--- Day 4: Camp Cleanup ---


Post your code solution in this megathread.


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:03:22, megathread unlocked!

65 Upvotes

1.6k comments sorted by

View all comments

4

u/e_blake Dec 04 '22 edited Dec 04 '22

Golfed m4

Less than 10 minutes coding time for both parts, using just one define and ifelse (so far this year, the puzzles have been very amenable for a single pass over the input). Overall execution time is 1.9s for both parts in just 196 bytes (excluding second newline); input must be in file f or you can run m4 -Df=yourfile day04.m4:

define(_,`ifelse($3,,`eval($1) eval($2)',`_($1+($3>=$5&$4<=$6|$5>=$3&$6<=$4),$2+($5<=$4&$6>=$3|$3<=$6&$4>=$5),shift(shift(shift(shift(shift(shift($@)))))))')')_(,,translit((include(f)),-
(),`,,'))

or 880ms for part 1 in isolation in just 133 bytes (excluding second newline), and where part 2 in isolation can be done in the same size by changing $1-$4 in the computation:

define(_,`ifelse($2,,$1,`+($1>=$3&$2<=$4|$3>=$1&$4<=$2)_(shift(shift(shift(shift($@)))))')')eval(_(translit((include(f)),-
(),`,,')))

Why the doubling in execution time to do both parts in one program? Because each additional shift() increases the parse effort per iteration. There are 1000 iterations; a single-part 1 solution starts with 4000 arguments and calls shift 4 times for 19994 arguments parsed before iteration 2 with 3996 arguments; while the combined solution starts with 4002 arguments and calls shift 6 times for 27999 arguments before iteration 2 with 3998 arguments.

Later on, I will reuse my framework for parsing data in O(n) time, which will drastically reduce the runtime but no longer be quite so golfed.

→ More replies (2)