In these solutions I strive for elegance, generality, and efficiency, simultaneously if possible.
Time-to-solution was not a metric I measured myself by, preferring aesthetics above all.
The ultimate goal is to solve each problem in a purely functional paradigm, with no loops or mutable local variables.
To this end, I've written a decorator, @tail_recursive
, which allows writing
tail-recursive functions in a purely functional style without incurring unbounded stack height and extra function calls.
This operates at the byte code level.
I experimented with an AST-based solution, but found operating at the byte code level more satisfactory.
This does mean that much of the code in this repo is restricted to running in CPython (as opposed to e.g. PyPy).
From repo root, run:
pipenv install --dev
# help on the commands documented below
./main --help
# run solution to day 1 problem
./main run 1
# show info about the day 10 problem, including optional arguments that can be passed with --args <name>=<json_value>
./main info 10
# run tests for day 12 problem
./main test 12
# run day 24 solution with particular input read from stdin as opposed to the default input file
cat my_input.txt | ./main run 24
...................................................................................................
..........................................................................................o........
.........................................................................................ooo.......
........................................................................................ooooo......
.......................................................................................#ooooo#.....
......................................................................................o#ooooo#.....
.....................................................................................oo#ooooo#.....
....................................................................................oo##ooooo######
...................................................................................ooo#ooooooo....#
..................................................................................oooo#oooooooo...#
.................................................................................ooooo#ooooooooo..#
................................................................................oooooo#############
...............................................................................oooooooo............
..............................................................................oooooooooo...........
.............................................................................ooooooo#o#oo..........
............................................................................oooooooo#o#ooo.........
...........................................................................ooooooooo#o#oooo........
..........................................................................oo#########o#######......
.........................................................................ooo#.......ooo.....#......
........................................................................oooo#......ooooo....#......
.......................................................................ooooo#################......
......................................................................ooooooo......................
.....................................................................ooooooooo.....................
....................................................................#oooooooooo.#..................
....................................................................#############..................
...................................................................................................
...................................................................................................
..................................................................oo...............................
.................................................................oooo..............................
.............................................................#..oooooo.............................
.............................................................##########............................
...................................................................................................
.......................................................................o...........................
......................................................................ooo#.........................
.....................................................................oooo#.........................
....................................................................ooooo#.........................
...................................................................oooooo#.........................
..................................................................o#ooooo#.........................
.................................................................oo#ooooo#.#.......................
................................................................ooo#ooooo#.#.......................
...............................................................oooo#o#o#o#.#.......................
..............................................................ooo#o#o#o#o#.#.......................
.............................................................oooo#o#o#o#o#.#.......................
............................................................ooooo###########.......................
...........................................................ooooooo.................................
..........................................................ooooooooo................................
.........................................................ooooo#####o...............................
........................................................ooooooo...ooo..............................
.......................................................oooo#####.#####.............................
......................................................oooooo.......................................
.....................................................ooo#####.#####.#####..........................
....................................................ooooo..........................................
...................................................oo#####.#####.#####.#####.......................
..................................................oooo.............................................
.................................................o#####.#####.#####.#####.#####....................
................................................ooo................................................
...............................................ooooo...............................................
..............................................#ooooo#..............................................
..............................................#ooooo#..............................................
..............................................#ooooo#..............................................
.............................................o#ooooo#..............................................
............................................o##ooooo#####..........................................
...........................................oo#ooooooo...#..........................................
..........................................ooo#oooooooo..#..........................................
.........................................oooo#ooooooooo.#..........................................
........................................ooooo#oooooooooo#..........................................
.......................................oooooo#oooooooooo#..........................................
......................................ooooooo#oooooooooo#..........................................
.....................................oooooooo############..........................................
....................................oooooooooo.....................................................
...................................oooo#ooooooo....................................................
..................................ooooo###############.............................................
.................................ooooooo...........................................................
................................ooooooooo..........................................................
...............................ooooo#####o.........................................................
..............................ooooooo...ooo........................................................
.............................oooo#####.#####.......................................................
............................oooooo.................................................................
...........................ooo#####.#####.#####....................................................
..........................ooooo....................................................................
.........................ooooooo...................................................................
........................ooo#####o..................................................................
.......................ooooo...ooo.................................................................
......................oo#####.#####................................................................
.....................oooo..........................................................................
....................o#####.#####.#####.............................................................
...................ooo.............................................................................
..................#####.#####.#####.#####..........................................................
.................o.................................................................................
................ooo................................................................................
...............#####...............................................................................
..............o....................................................................................
.............ooo...................................................................................
............#####.#####............................................................................
..........oo.......................................................................................
.........oooo......................................................................................
........o#####.#####.#####.........................................................................
.......ooo.........................................................................................
......ooooo........................................................................................
#....o#ooooo.o#....................................................................................
#...oo#ooooooo#....................................................................................
#..ooo#ooooooo#....................................................................................
#.oooo#ooooooo#....................................................................................
#ooooo#o#ooooo#....................................................................................
#ooooo#o#ooooo#....................................................................................
#o#ooo#o#ooooo#....................................................................................
#o#o#o#o#ooooo#....................................................................................
#o#o#o#o#o#o#o#....................................................................................
#o#o#o#o#o#o#o#....................................................................................
###############....................................................................................
...................................................................................................
...................................................................................................
...................................................................................................
...................................................................................................
...................................................................................................
.............#.....................................................................................
.............#.....................................................................................
.............#oo...................................................................................
...........#.#ooo..................................................................................
...........#.#ooo#.................................................................................
...........#.#ooo#.................................................................................
...........#.#o#o#.................................................................................
...........#######.................................................................................
...................................................................................................
.................oo................................................................................
..........#.....ooo#...............................................................................
..........##########...............................................................................
...................................................................................................
....................o..............................................................................
...................#oo.#...........................................................................
...................#ooo#...........................................................................
..................##ooo####........................................................................
..................#ooooo..#........................................................................
..................#oooooo.#........................................................................
..................#ooooooo#........................................................................
..................#ooooooo#........................................................................
..................#ooooooo#........................................................................
..................#########........................................................................
...................................................................................................
.................o.................................................................................
................ooo................................................................................
...............ooooo...............................................................................
..............ooooooo..............................................................................
.............#oooooooo.............................................................................
............o#ooooo#ooo#...........................................................................
...........oo#ooooo#ooo#...........................................................................
..........ooo#o#ooo#o#o#...........................................................................
.........#ooo#o#o#o#o#o#...........................................................................
.........#ooo#o#o#o#o#o#...........................................................................
.........#o#o#o#o#o#o#o#...........................................................................
.........###############...........................................................................
day 14 sample output