Added readme solution infos for days 12 and 25

This commit is contained in:
Stefan Müller 2024-11-20 20:28:07 +01:00
parent 3008efc4ae
commit f86f70d569
1 changed files with 22 additions and 2 deletions

View File

@ -2,11 +2,11 @@
Solver for [Advent of Code 2023](https://adventofcode.com/2023/) puzzles.
This is a single command line application for all puzzles written in [FreePascal](https://www.freepascal.org) with [Lazarus](https://www.lazarus-ide.org/) 2.2.6 and compiled with FPC 3.2.2.
This is a single command line application for all 49 puzzles written in [FreePascal](https://www.freepascal.org) with [Lazarus](https://www.lazarus-ide.org/) 2.2.6 and compiled with FPC 3.2.2.
## Puzzle Input
This project does not contain the puzzle or example inputs as per the [copyright notice of Advent of Code](https://adventofcode.com/about). In order to run the compiled application, the puzzle inputs have to be downloaded from the [Advent of Code 2023](https://adventofcode.com/2023/) puzzle pages, and placed as text files into the `bin\data` directory, e.g. `bin\data\cube_conundrum.txt` or `bin\data\example\cube_conundrum.txt`. The application will output an error message with details, if it cannot find an input file.
This project does not contain the puzzle or example inputs as per the [copyright notice of Advent of Code](https://adventofcode.com/about). In order to run the compiled application, the puzzle inputs have to be downloaded from the [Advent of Code 2023](https://adventofcode.com/2023/) puzzle pages, and placed as text files into the `bin\data` directory, e.g. `bin\data\cube_conundrum.txt`, or `bin\data\example\cube_conundrum.txt` for the unit tests. The application will output an error message with details if it cannot find an input file.
## Tests
@ -104,6 +104,18 @@ While parsing the input, the solver tracks coordinates of each galaxy, and for e
This approach was trivial to adapt for part 2, since all that was needed was another factor that had to be multiplied with the values tracked for the rows and columns before applying the *+1*.
### Day 12: Hot Springs
:mag_right: Puzzle: <https://adventofcode.com/2023/day/12>, :white_check_mark: Solver: [`UHotSprings.pas`](solvers/UHotSprings.pas)
All arrangements for part 1 can easily be found by recursively trying all possible positions for each contiguous group of damaged springs in order.
Interestingly for part 2, I found the improved version of algorithm relatively easy to describe, but tricky to implement. My solver essentially finds all combinations to assign each damaged group ("validation number") from the input to exactly one "block" (a maximal contiguous string of `#` and `?`), such that for each block the assigned groups would fit if all `#` were `?`. If the number of arrangements for a block is not trivial, the solver then finds all combinations to assign each "damage" (a maximal contiguous string of `#`) to one of the numbers assigned to the block, such that the unassigned numbers would still fit in between, for at least some arrangements of the assigned numbers. Lastly, the solver iterates through all combinations of actual positions of the numbers assigned to damages, and calculates the number of combinations to arrange the skipped numbers in between directly from binomial coefficients.
Importantly, the algorithm is optimized to discard any of the tested arrangements as early as possible. For example, if numbers assigned to a block lead to zero combinations for that block, the combination is discarded before numbers or blocks further right in the input are considered.
In addition, the solver keeps track of calculated combinations for all non-trivial number-to-block assignments, which helps because of the many duplicates due to the way in which the input for part 2 is created.
### Day 13: Point of Incidence
:mag_right: Puzzle: <https://adventofcode.com/2023/day/13>, :white_check_mark: Solver: [`UPointOfIncidence.pas`](solvers/UPointOfIncidence.pas)
@ -208,6 +220,14 @@ For part 2, I believe there is no polynomial algorithm known for the general cas
While I found part 1 quite trivial, part 2 left me with the feeling that my approach might be mad. Eventually, I managed to find the ray hitting all other rays by solving the general equation system for three known and one unknown rays with some shortcuts for this particular problem, for example assuming the existence of a unique solution. However, this involved excessive manual pre-calculations, arbitrary length integer arithmetic, and a root finder for integer polynomials, all implemented by myself without additional third-party libraries.
### Day 25: Snowverload :star:
:mag_right: Puzzle: <https://adventofcode.com/2023/day/25>, :white_check_mark: Solver: [`USnowverload.pas`](solvers/USnowverload.pas)
This puzzle has only one part, for which my solver uses a graph data structure and runs [Karger's algorithm](https://en.wikipedia.org/wiki/Karger%27s_algorithm) on it until it finds a 3-cut. This was quite fun because I had known about Karger's algorithm, which is an efficient _probabilistic_ min-cut algorithm, but had not yet have the opportunity to implement it.
The main difficulty was to set up the graph data structure in such a way that the edge contractions from the algorithm can be reverted quickly to repeat the process.
## License
Copyright (C) 2023-2024 Stefan Müller