Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mstksg/advent-of-code-2020
🎅🌟❄️☃️🎄🎁
https://github.com/mstksg/advent-of-code-2020
advent-of-code advent-of-code-2020
Last synced: about 2 months ago
JSON representation
🎅🌟❄️☃️🎄🎁
- Host: GitHub
- URL: https://github.com/mstksg/advent-of-code-2020
- Owner: mstksg
- License: bsd-3-clause
- Created: 2020-11-28T01:34:41.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2021-12-02T00:56:47.000Z (about 3 years ago)
- Last Synced: 2024-10-12T00:56:22.994Z (2 months ago)
- Topics: advent-of-code, advent-of-code-2020
- Language: Haskell
- Homepage:
- Size: 28.6 MB
- Stars: 98
- Watchers: 9
- Forks: 8
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
Advent of Code 2020
=====================*[2016][]* / *[2017][]* / *[2018][]* / *[2019][]* / *2020* / *[2021][]*
[2016]: https://github.com/mstksg/advent-of-code-2016
[2017]: https://github.com/mstksg/advent-of-code-2017
[2018]: https://github.com/mstksg/advent-of-code-2018
[2019]: https://github.com/mstksg/advent-of-code-2019
[2021]: https://github.com/mstksg/advent-of-code-2021It's the most wonderful time of the year!
My [Advent of Code 2020][aoc] Haskell solutions here, along with an automated
fetching, testing, running environment (powered by the
*[advent-of-code-api][]* library). The interactive development environment and
runner/bench marker/viewer/tester has been pulled out [here][dev], so this is
implemented as "fork" of it with my own solutions and reflections.Check out the [reflections][] (with [rss feed][rss]) and [package
haddocks][haddock] --- more info below![aoc]: https://adventofcode.com/2020
[haddock]: https://mstksg.github.io/advent-of-code-2020/
[advent-of-code-api]: https://hackage.haskell.org/package/advent-of-code-api
[dev]: https://github.com/mstksg/advent-of-code-dev[Reflections and Benchmarks][reflections]
-----------------------------------------[Available as RSS Feed][rss]
[rss]: http://feeds.feedburner.com/jle-advent-of-code-2020
| Challenge | Reflections | Code | Rendered | Benchmarks |
| --------- | ----------- | --------- | ---------- | ---------- |
| Day 1 | [x][d01r] | [x][d01g] | [x][d01h] | [x][d01b] |
| Day 2 | [x][d02r] | [x][d02g] | [x][d02h] | [x][d02b] |
| Day 3 | [x][d03r] | [x][d03g] | [x][d03h] | [x][d03b] |
| Day 4 | [x][d04r] | [x][d04g] | [x][d04h] | [x][d04b] |
| Day 5 | [x][d05r] | [x][d05g] | [x][d05h] | [x][d05b] |
| Day 6 | [x][d06r] | [x][d06g] | [x][d06h] | [x][d06b] |
| Day 7 | [x][d07r] | [x][d07g] | [x][d07h] | [x][d07b] |
| Day 8 | [x][d08r] | [x][d08g] | [x][d08h] | [x][d08b] |
| Day 9 | [x][d09r] | [x][d09g] | [x][d09h] | [x][d09b] |
| Day 10 | [x][d10r] | [x][d10g] | [x][d10h] | [x][d10b] |
| Day 11 | [x][d11r] | [x][d11g] | [x][d11h] | [x][d11b] |
| Day 12 | [x][d12r] | [x][d12g] | [x][d12h] | [x][d12b] |
| Day 13 | [x][d13r] | [x][d13g] | [x][d13h] | [x][d13b] |
| Day 14 | [x][d14r] | [x][d14g] | [x][d14h] | [x][d14b] |
| Day 15 | [x][d15r] | [x][d15g] | [x][d15h] | [x][d15b] |
| Day 16 | [x][d16r] | [x][d16g] | [x][d16h] | [x][d16b] |
| Day 17 | [x][d17r] | [x][d17g] | [x][d17h] | [x][d17b] |
| Day 18 | [x][d18r] | [x][d18g] | [x][d18h] | [x][d18b] |
| Day 19 | [x][d19r] | [x][d19g] | [x][d19h] | [x][d19b] |
| Day 20 | [x][d20r] | [x][d20g] | [x][d20h] | [x][d20b] |
| Day 21 | [x][d21r] | [x][d21g] | [x][d21h] | [x][d21b] |
| Day 22 | [x][d22r] | [x][d22g] | [x][d22h] | [x][d22b] |
| Day 23 | [x][d23r] | [x][d23g] | [x][d23h] | [x][d23b] |
| Day 24 | [x][d24r] | [x][d24g] | [x][d24h] | [x][d24b] |
| Day 25 | [x][d25r] | [x][d25g] | [x][d25h] | [x][d25b] |"Rendered" links go to haddock source renders for code, with reflections in the
documentation. Haddock source renders have hyperlinked identifiers,
so you can follow any unrecognized identifiers to see where I have defined them
in the library.[reflections]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md
### `:~>` type
If you're looking at my actual github solutions, you'll notice that this year
I'm implementing my solutions in terms of a `:~>` record type:```haskell
data a :~> b = MkSol
{ sParse :: String -> Maybe a -- ^ parse input into an `a`
, sSolve :: a -> Maybe b -- ^ solve an `a` input to a `b` solution
, sShow :: b -> String -- ^ print out the `b` solution for submission
}
```An `a :~> b` is a solution to a challenge expecting input of type `a` and
producing answers of type `b`. It also packs in functions to parse a `String`
into an `a`, and functions to show a `b` as a `String` to submit as an answer.This helps me mentally separate out parsing, solving, and showing, allowing for
some cleaner code and an easier time planning my solution.Such a challenge can be "run" on string inputs by feeding the string into
`sParse`, then `sSolve`, then `sShow`:```haskell
-- | Run a ':~>' on some input, retuning 'Maybe'
runSolution :: Challenge -> String -> Maybe String
runSolution MkSol{..} s = do
x <- sParse s
y <- sSolve x
pure (sShow y)
```In the actual library, I have `runSolution` return an `Either` so I can debug
which stage the error happened in.You might also notice the function `dyno_`, used like `dyno_ "limit" 10000`. This
is how I implement parameters in problems that vary between test data and
actual input. For example, Day 6 involved finding points that had a total
distance of less than 10000, but for the test input, we found the points that
had a total distance of less than 32. So, I have a system that lets me write
`dyno_ "limit" 10000` in my code instead of hard-coding in `10000`. This
`10000` would be replaced by `32` when running with test data (which is parsed
from [this file][7btest])[7btest]: https://github.com/mstksg/advent-of-code-2018/blob/master/test-data/06b.txt
Interactive
-----------The *[AOC.Run.Interactive][interactive]* module has code (powered by
*[advent-of-code-api][]*) for testing your solutions and submitting within
GHCI, so you don't have to re-compile. If you edit your solution programs, they
are automatically updated when you hit `:r` in ghci.[interactive]: https://mstksg.github.io/advent-of-code-2020/AOC2020-Run-Interactive.html
```haskell
ghci> execSolution_ $ solSpec 'day02a -- get answer for challenge based on solution
ghci> testSolution_ $ solSpec 'day02a -- run solution against test suite
ghci> viewPrompt_ $ solSpec 'day02a -- view the prompt for a part
ghci> waitForPrompt_ $ solSpec 'day02a -- count down to the prompt for a part
ghci> submitSolution_ $ solSpec 'day02a -- submit a solution, and retry after cooldown automatically
```These are loaded with session key stored in the configuration file (see next
section).Executable
----------Comes with test examples given in problems.
You can install using `stack`:
```bash
$ git clone https://github.com/mstksg/advent-of-code-2020
$ cd advent-of-code-2020
$ stack setup
$ stack install
```The executable `aoc2020` includes a testing and benchmark suite, as well as a
way to view prompts within the command line:```
$ aoc2020 --help
aoc2020 - Advent of Code 2020 challenge runnerUsage: aoc2020 [-c|--config PATH] COMMAND
Run challenges from Advent of Code 2020. Available days: 1, 2, 3 (..)Available options:
-c,--config PATH Path to configuration file (default: aoc-conf.yaml)
-h,--help Show this help textAvailable commands:
run Run, test, and benchmark challenges
view View a prompt for a given challenge
submit Test and submit answers for challenges
test Alias for run --test
bench Alias for run --bench
countdown Alias for view --countdown$ aoc2020 run 3 b
>> Day 03b
>> [✓] 243
```You can supply input via stdin with `--stdin`:
```
$ aoc2020 run 1 --stdin
>> Day 01a
+1
+2
+1
-3[?] 1
>> Day 01b
[?] 1
```Benchmarking is implemented using *criterion*
```
$ aoc2020 bench 2
>> Day 02a
benchmarking...
time 1.317 ms (1.271 ms .. 1.392 ms)
0.982 R² (0.966 R² .. 0.999 R²)
mean 1.324 ms (1.298 ms .. 1.373 ms)
std dev 115.5 μs (77.34 μs .. 189.0 μs)
variance introduced by outliers: 65% (severely inflated)>> Day 02b
benchmarking...
time 69.61 ms (68.29 ms .. 72.09 ms)
0.998 R² (0.996 R² .. 1.000 R²)
mean 69.08 ms (68.47 ms .. 69.99 ms)
std dev 1.327 ms (840.8 μs .. 1.835 ms)
```Test suites run the example problems given in the puzzle description, and
outputs are colorized in ANSI terminals.```
$ aoc2020 test 1
>> Day 01a
[✓] (3)
[✓] (3)
[✓] (0)
[✓] (-6)
[✓] Passed 4 out of 4 test(s)
[✓] 416
>> Day 01b
[✓] (2)
[✓] (0)
[✓] (10)
[✓] (5)
[✓] (14)
[✓] Passed 5 out of 5 test(s)
[✓] 56752
```This should only work if you're running `aoc2020` in the project directory.
**To run on actual inputs**, the executable expects inputs to be found in the
folder `data/XX.txt` in the directory you are running in. That is, the input
for Day 7 will be expected at `data/07.txt`.*aoc2020 will download missing input files*, but requires a session token.
This can be provided in `aoc-conf.yaml`:```yaml
session: [[ session token goes here ]]
```Session keys are also required to download "Part 2" prompts for each challenge.
You can "lock in" your current answers (telling the executable that those are
the correct answers) by passing in `--lock`. This will lock in any final
puzzle solutions encountered as the verified official answers. Later, if you
edit or modify your solutions, they will be checked on the locked-in answers.These are stored in `data/ans/XXpart.txt`. That is, the target output for Day 7
(Part 2, `b`) will be expected at `data/ans/07b.txt`. You can also manually
edit these files.You can view prompts: (use `--countdown` to count down until a prompt is
released, and display immediately)```
$ aoc2020 view 3 b
>> Day 03b
--- Part Two ---
----------------Amidst the chaos, you notice that exactly one claim doesn't overlap by
even a single square inch of fabric with any other claim. If you can
somehow draw attention to it, maybe the Elves will be able to make
Santa's suit after all!For example, in the claims above, only claim `3` is intact after all
claims are made.*What is the ID of the only claim that doesn't overlap?*
```You can also submit answers:
```
$ aoc2020 submit 1 a
```Submissions will automatically run the test suite. If any tests fail, you will
be asked to confirm submission or else abort. The submit command will output
the result of your submission: The message from the AoC website, and whether or
not your answer was correct (or invalid or ignored). Answers that are
confirmed correct will be locked in and saved for future testing against, in
case you change your solution.All networking features are powered by *[advent-of-code-api][]*.
[d01g]: https://github.com/mstksg/advent-of-code-2020/blob/master/src/AOC/Challenge/Day01.hs
[d01h]: https://mstksg.github.io/advent-of-code-2020/src/AOC.Challenge.Day01.html
[d01r]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-1
[d01b]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-1-benchmarks
[d02g]: https://github.com/mstksg/advent-of-code-2020/blob/master/src/AOC/Challenge/Day02.hs
[d02h]: https://mstksg.github.io/advent-of-code-2020/src/AOC.Challenge.Day02.html
[d02r]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-2
[d02b]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-2-benchmarks
[d03g]: https://github.com/mstksg/advent-of-code-2020/blob/master/src/AOC/Challenge/Day03.hs
[d03h]: https://mstksg.github.io/advent-of-code-2020/src/AOC.Challenge.Day03.html
[d03r]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-3
[d03b]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-3-benchmarks
[d04g]: https://github.com/mstksg/advent-of-code-2020/blob/master/src/AOC/Challenge/Day04.hs
[d04h]: https://mstksg.github.io/advent-of-code-2020/src/AOC.Challenge.Day04.html
[d04r]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-4
[d04b]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-4-benchmarks
[d05g]: https://github.com/mstksg/advent-of-code-2020/blob/master/src/AOC/Challenge/Day05.hs
[d05h]: https://mstksg.github.io/advent-of-code-2020/src/AOC.Challenge.Day05.html
[d05r]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-5
[d05b]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-5-benchmarks
[d06g]: https://github.com/mstksg/advent-of-code-2020/blob/master/src/AOC/Challenge/Day06.hs
[d06h]: https://mstksg.github.io/advent-of-code-2020/src/AOC.Challenge.Day06.html
[d06r]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-6
[d06b]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-6-benchmarks
[d07g]: https://github.com/mstksg/advent-of-code-2020/blob/master/src/AOC/Challenge/Day07.hs
[d07h]: https://mstksg.github.io/advent-of-code-2020/src/AOC.Challenge.Day07.html
[d07r]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-7
[d07b]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-7-benchmarks
[d08g]: https://github.com/mstksg/advent-of-code-2020/blob/master/src/AOC/Challenge/Day08.hs
[d08h]: https://mstksg.github.io/advent-of-code-2020/src/AOC.Challenge.Day08.html
[d08r]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-8
[d08b]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-8-benchmarks
[d09g]: https://github.com/mstksg/advent-of-code-2020/blob/master/src/AOC/Challenge/Day09.hs
[d09h]: https://mstksg.github.io/advent-of-code-2020/src/AOC.Challenge.Day09.html
[d09r]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-9
[d09b]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-9-benchmarks
[d10g]: https://github.com/mstksg/advent-of-code-2020/blob/master/src/AOC/Challenge/Day10.hs
[d10h]: https://mstksg.github.io/advent-of-code-2020/src/AOC.Challenge.Day10.html
[d10r]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-10
[d10b]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-10-benchmarks
[d11g]: https://github.com/mstksg/advent-of-code-2020/blob/master/src/AOC/Challenge/Day11.hs
[d11h]: https://mstksg.github.io/advent-of-code-2020/src/AOC.Challenge.Day11.html
[d11r]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-11
[d11b]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-11-benchmarks
[d12g]: https://github.com/mstksg/advent-of-code-2020/blob/master/src/AOC/Challenge/Day12.hs
[d12h]: https://mstksg.github.io/advent-of-code-2020/src/AOC.Challenge.Day12.html
[d12r]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-12
[d12b]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-12-benchmarks
[d13g]: https://github.com/mstksg/advent-of-code-2020/blob/master/src/AOC/Challenge/Day13.hs
[d13h]: https://mstksg.github.io/advent-of-code-2020/src/AOC.Challenge.Day13.html
[d13r]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-13
[d13b]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-13-benchmarks
[d14g]: https://github.com/mstksg/advent-of-code-2020/blob/master/src/AOC/Challenge/Day14.hs
[d14h]: https://mstksg.github.io/advent-of-code-2020/src/AOC.Challenge.Day14.html
[d14r]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-14
[d14b]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-14-benchmarks
[d15g]: https://github.com/mstksg/advent-of-code-2020/blob/master/src/AOC/Challenge/Day15.hs
[d15h]: https://mstksg.github.io/advent-of-code-2020/src/AOC.Challenge.Day15.html
[d15r]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-15
[d15b]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-15-benchmarks
[d16g]: https://github.com/mstksg/advent-of-code-2020/blob/master/src/AOC/Challenge/Day16.hs
[d16h]: https://mstksg.github.io/advent-of-code-2020/src/AOC.Challenge.Day16.html
[d16r]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-16
[d16b]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-16-benchmarks
[d17g]: https://github.com/mstksg/advent-of-code-2020/blob/master/src/AOC/Challenge/Day17.hs
[d17h]: https://mstksg.github.io/advent-of-code-2020/src/AOC.Challenge.Day17.html
[d17r]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-17
[d17b]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-17-benchmarks
[d18g]: https://github.com/mstksg/advent-of-code-2020/blob/master/src/AOC/Challenge/Day18.hs
[d18h]: https://mstksg.github.io/advent-of-code-2020/src/AOC.Challenge.Day18.html
[d18r]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-18
[d18b]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-18-benchmarks
[d19g]: https://github.com/mstksg/advent-of-code-2020/blob/master/src/AOC/Challenge/Day19.hs
[d19h]: https://mstksg.github.io/advent-of-code-2020/src/AOC.Challenge.Day19.html
[d19r]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-19
[d19b]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-19-benchmarks
[d20g]: https://github.com/mstksg/advent-of-code-2020/blob/master/src/AOC/Challenge/Day20.hs
[d20h]: https://mstksg.github.io/advent-of-code-2020/src/AOC.Challenge.Day20.html
[d20r]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-20
[d20b]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-20-benchmarks
[d21g]: https://github.com/mstksg/advent-of-code-2020/blob/master/src/AOC/Challenge/Day21.hs
[d21h]: https://mstksg.github.io/advent-of-code-2020/src/AOC.Challenge.Day21.html
[d21r]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-21
[d21b]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-21-benchmarks
[d22g]: https://github.com/mstksg/advent-of-code-2020/blob/master/src/AOC/Challenge/Day22.hs
[d22h]: https://mstksg.github.io/advent-of-code-2020/src/AOC.Challenge.Day22.html
[d22r]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-22
[d22b]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-22-benchmarks
[d23g]: https://github.com/mstksg/advent-of-code-2020/blob/master/src/AOC/Challenge/Day23.hs
[d23h]: https://mstksg.github.io/advent-of-code-2020/src/AOC.Challenge.Day23.html
[d23r]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-23
[d23b]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-23-benchmarks
[d24g]: https://github.com/mstksg/advent-of-code-2020/blob/master/src/AOC/Challenge/Day24.hs
[d24h]: https://mstksg.github.io/advent-of-code-2020/src/AOC.Challenge.Day24.html
[d24r]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-24
[d24b]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-24-benchmarks
[d25g]: https://github.com/mstksg/advent-of-code-2020/blob/master/src/AOC/Challenge/Day25.hs
[d25h]: https://mstksg.github.io/advent-of-code-2020/src/AOC.Challenge.Day25.html
[d25r]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-25
[d25b]: https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-25-benchmarks