[2018 Day 4] In Review (Repose Record)
Today we need to sneak in to get to the prototype suit to fix on issues with it. And this being the 1500s, we don't have continually cyloning scanners to go through, but fallible guards that fall asleep at times. Well, most of them, three of the ones in my input can stay awake (at least until 1am).
This is one of those the problems where it's like a real job. The input is very much like a typical log file that you might want to write a script to collect and present data from. And, my initial solution for this was done in a way I'd do at work. It's a state machine with lots of die assertions in it for anything unexpected. Along the way I collect the data in a hash table, mapping each guard to a structure to store the fields needed. At the end of reading the input, the answer has been found by the state machine, and I just need to multiply the two values and print.
It's probably not what I'd do now in AoC. I'd be less focused on reading the input and validating... and more likely to hack to quickly get the data in. Something like:
$/ = 'Guard';
my ($junk, @log) = map {[split /\n/]} <>;
That splits the input like "Guard" is the "newline", the first line will be the bit before the first occurrence of "Guard" so we just junk it. Here I've chosen to do it with assignment to a junk variable, instead of a shift... I find this a bit more self documenting. The key is that the @log array has broken things up by the daily reports... each one is an array where I can grab the guard ID from the first line, and the sleep-awake intervals from pairs of the rest.
The same sort of brute forcing that worked on day 3, will work even better here. Instead of a million cells and 2D rectangles, it's just 60 per guard and 1D intervals. Which really means that the focus of this problem is in parsing the log file... in day 3, the data needed to be parsed, but it was compact, with everything about a rectangle on a single line (and a "just grab all numbers" template line, slurps everything quickly). In this problem, the data you need is spread out across lines. Meaning it might be seen as the input going from 1D to 2D.
Posted Apr 4, 2026, 1:09 PM UTC