profile picture

Puzzling with Possum: Parsing Inputs for Advent of Code

Advent of Code 2022, Day 1

One important consideration is food - in particular, the number of Calories each Elf is carrying (your puzzle input).

The Elves take turns writing down the number of Calories contained by the various meals, snacks, rations, etc. that they've brought with them, one item per line. Each Elf separates their own inventory from the previous Elf's inventory (if any) by a blank line.

As a first step we can parse a group of integers which are each separated by a newline. We use array_sep(p, sep) to collect the integer values into an array. This will only parse the inventory of the first Elf, stopping when we find a second newline instead of an integer on line 4.

To parse the entire input we want to parse each Elf's inventory and collect them all into an array. We can do this by wrapping our parser for a single inventory in another array_sep, but this time the parser for the separator is multiple newlines.

Arrays of arrays come up frequently enough that there's a standard library parser called table_sep(p, sep, row_sep) for this use case. Intuitively table_sep is for tabular data such as rows of comma separated values. While this puzzle input does not visually look like a table it's structurally equivalent, with a newline separating each inventory item and newlines separating each inventory "row".

Possum's standard library includes shortened aliases for a number of the most common parsers. We can abbreviate integer to int, newline to nl, and newlines to nls. Sometimes it's nice to use the longer version to be explicit, but I'd consider the abbreviated aliases to be more idomatic.

Finally, we use the + operator to create the parser nl + nl, which will parse exactly two newlines. Using this parser in place of nls is a bit more specific, since we know that the input format is always going to have two newlines between inventories. This isn't terribly important, but it feels like a nice touch.

Advent of Code 2022, Day 13

Your handheld device must still not be working properly; the packets from the distress signal got decoded out of order. You'll need to re-order the list of received packets (your puzzle input) to decode the message.

Your list consists of pairs of packets; pairs are separated by a blank line.

...

Packet data consists of lists and integers. Each list starts with [, ends with ], and contains zero or more comma-separated values (either integers or other lists). Each packet is always a list and appears on its own line.

When comparing two values, the first value is called left and the second value is called right.

This input looks kind of like 2022 day 1 ! Packets are separated by newlines, and pairs are separated by two newlines. We don't know how parse the indevidual packets, but otherwise this follows the same table_sep structure. to The line parser matches any text left on the current line,

This looks pretty good, but there's a bug.

Advent of Code 2023, Day 2

As you walk, the Elf shows you a small bag and some cubes which are either red, green, or blue. Each time you play this game, he will hide a secret number of cubes of each color in the bag, and your goal is to figure out information about the number of cubes.

To get information, once a bag has been loaded with cubes, the Elf will reach into the bag, grab a handful of random cubes, show them to you, and then put them back in the bag. He'll do this a few times per game.

You play several games and record the information from each game (your puzzle input). Each game is listed with its ID number (like the 11 in Game 11: ...) followed by a semicolon-separated list of subsets of cubes that were revealed from the bag (like 3 red, 5 green, 4 blue).

This puzzle input is pretty dense with information, so we'll have to break it down into a bunch of small steps.

Advent of Code 2022, Day 5

The ship has a giant cargo crane capable of moving crates between stacks. To ensure none of the crates get crushed or fall over, the crane operator will rearrange them in a series of carefully-planned steps. After the crates are rearranged, the desired crates will be at the top of each stack.

The Elves don't want to interrupt the crane operator during this delicate procedure, but they forgot to ask her which crate will end up where, and they want to be ready to unload them as soon as possible so they can embark.

They do, however, have a drawing of the starting stacks of crates and the rearrangement procedure (your puzzle input).