Module advent_of_code_2021::day_4 [−][src]
Expand description
This is my solution for Advent of Code - Day 4 - Giant Squid
Today we had to build an engine to track games of Bingo. Given a set of numbers that would be called, and a set of 100 bingo cards, the challenge was to work out the one that would win first (part one), and the one that would win last (part two). I’m quite pleased with the data structure I came up with to store the bingo cards, but there was a lot of fighting with the borrow checker so I’ve ended up with some ugly bits of code.
The key to the solution is BingoCard and parse_card that turns the raw input into this
internal representation. The game is then simulated by repeatedly calling
BingoCard::mark_number until the criteria for the current part have been met.
play_bingo implements part one and just runs until a card wins, play_bingo_until_last
implements part two and removes cards from the set as they win until none are left. There is
a final small helper BingoCard::sum_remaining that calculates the number needed for the
final submission.
Structs
This represents the key information to know if a 5 x 5 bingo card has won.
Functions
This takes a string with 5 lines, each with 5 space-separated numbers, representing a 5 x 5
bingo card. A regex is used to split the numbers on a line as single digit numbers cause
there to be two spaces prefixing those numbers. Iterator::enumerate is used to track the
current co-ordinates for building the map of unmarked numbers. The row and column arrays are
initialised to 0s as no numbers have yet been marked.
Parse the puzzle input into the internal representation. first there is a line of numbers in the sequence the will be called to mark on the cards, then 100 5 x 5 grids of numbers representing each card. The first line and each card are separated by blank lines.
Iterate through the numbers, marking each card as appropriate. Return the first card to win and the number that triggered it, as both are needed to calculate the puzzle solution.
Iterate through the numbers, marking each card as appropriate. Very similar to play_bingo
except it needs to keep going until all cards have won. This leads to some complexity to
manage removing the cards from the iterator as we’re looping over the same list.
The entry point for running the solutions with the ‘real’ puzzle input.
