Archive for the ‘2023’ Category

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).

(more…)

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.

(more…)

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:

two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen

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

(more…)

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.

(more…)

Advent of Code 2023 Starts Friday

Monday, November 27th, 2023

My day job doesn’t (normally) involve programming; (as is obvious) I don’t have as much time these days for hobby electronics and programming as I used to; and I really miss it.

So I was delighted when Kip introduced me to Advent of Code a few years ago. It’s a programming contest that releases a new puzzle each midnight (US eastern time; 23:00 my time) on the night that becomes December 1 through the night that becomes December 25. You work the puzzles at your own pace in whatever programming language you want (a few years ago, I did one puzzle’s heavy lifting in software and evaluated the dozen remaining results by eye) and the site checks your answers for correctness and shows you stats about your ranking within the global participants and within personal leaderboards of which you’re a member.

The puzzles are cute word problems and come with examples of input, logic, and output. Each participant is given a randomly- or algorithmically-generated input file, therefore a different correct output from other participants. (The algorithmic and programming expertise of the AoC creator/maintainer is hard to overstate.) When you upload a correct output to the first part of the puzzle, you’re given the problem for the second part of the puzzle to run on the same input — which often as not causes a rewrite if you used inadvisable programming shortcuts in your solution to the first part.

As always, I’ll be doing this year’s puzzles in Perl. I’m reasonably fluent in it; and of the languages in which I’m reasonably fluent, Perl is very, very good at processing input in the kinds of formats used in these puzzles. (But so is _____ ! Yes, but I’m not fluent in _____ !)

And I’ll be posting them here, discussing both the algorithmic approach I take to the puzzle and the Perl code that accomplishes it.

You might find the puzzles interesting. You might find my algorithms interesting. You might want to learn more Perl (although I doubt it). You might want to trash-talk Perl (and I just won’t approve your comments). You might want to elevate my Perl programming by showing me more concise, more idiomatic, faster, or more elegant ways of writing the same code (and I’ll be thrilled).

See you Friday evening or Saturday morning, when I have time to post Friday’s code.