Archive for December, 2023

AoC 2023 D2P2: Reusing List of Lists of Lists Extraction

Saturday, December 2nd, 2023

In day 2 part 2, rather than verify whether each game / bag has no more than the specified number of cubes of each color, we’re to determine the number (maximum) cubes of each color that we see in across any pull from each game / bag.

my %max = ( red => 12, green => 13, blue => 14);

We no longer need to set the maximum permissible count.

my %max = ( red => 0, green => 0, blue => 0);

Instead, within each row of input, accumulate the maximum count of each color seen on any pull of this game.

Perl doesn’t require initializing hashes that will be used to accumulate information and it’s idiomatic not to; but with strict/warnings, my later comparisons will fail against uninitialized hash buckets; and it’s easier to initialize them than to add logic to check whether they’re already defined.

$max{$color} = $count if $count > $max{$color};

Then in the innermost loop when processing each color, set the new max for this game / bag to that count if the count exceeds the previously-seen maximum.

$sum += $max{red} * $max{green} * $max{blue};

Finally, we’re to sum the product of RGB counts from each line (rather than the game ID as in p1).


AoC 2023 D2P1: List of Lists of Lists Extraction

Saturday, December 2nd, 2023

Day 2′s problem 1 requires parsing this format:

Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green

to determine which rows have no more than 12 red, 13 green, and 14 blue cubes in the bag, then summing the game ID of those rows. In other words, interpreting lists of colors from lists of pulls from a list of games.


AoC 2023 D1P2: Convert Words to Digits

Friday, December 1st, 2023

I can’t link to part two of today’s problem because you have to solve part 1 to unlock the second half of the web page; but I’m sure it’s been countlessly reposted by now and I’ll summarize it:

Oops, some of the digits that we want to add up have been written as English words instead of numerals. Recognize the words one, two, three, four, five, six, seven, eight, and nine as digits also. And we have new sample input:


Which yields 29 + 83 + 13 + 14 + 42 + 14 + 76 = 281.


AoC 2023 D1P1: Extract Digits from Text

Friday, December 1st, 2023

As with most Advent of Code problems, today’s first problem has a lot of cute story and some ambiguity in the phrasing that needs to be teased out and disambiguated.

It works out to: Extract the first and last digits on each line of text, use them to construct a two-digit number, and sum those numbers. Unstated but visible in the example is that if a line has a single digit in it, you use that for both digits of that line’s contribution.