Home

Awesome

Advent of Code 2017

All my Advent of Code repos:

 

Solutions

My aim is to provide clean and readable, yet idiomatic, solutions in all three languages. If you have any comment/suggestion/advice, please let me know!

Originally, I've solved all tasks in Nim and Python as they were released. OCaml solutions were added in November 2019 as a preparation for AoC 2019 (these were my first steps in OCaml).

TaskNim solutionOCaml solutionPython solutionNote
Day 1: Inverse Captchaday01.nimday01.mlday01.pyTaking advantage of Python's negative indices.
Day 2: Corruption Checksumday02.nimday02.mlday02.py
Day 3: Spiral Memoryday03.nimday03.mlday03.pyBuilding a spiral with table/map/dict in all three versions; using iterators in Nim and Python.
Day 4: High-Entropy Passphrasesday04.nimday04.mlday04.py
Day 5: A Maze of Twisty Trampolines, All Alikeday05.nimday05.mlday05.pyUsed try-except in Python for some nice speed improvement.
Day 6: Memory Reallocationday06.nimday06.mlday06.pyPython doesn't have OrderedSet (had to use OrderedDict).
Day 7: Recursive Circusday07.nimday07.mlday07.pyPython's Counter.most_common() is quite helpful/useful here.
Day 8: I Heard You Like Registersday08.nimday08.mlday08.py
Day 9: Stream Processingday09.nimday09.mlday09.py
Day 10: Knot Hashday10.nimday10.mlday10.pyChanged solutions to be reusable for Day 14. Python version uses deque with pop, rotate, and insert. Nim version is a more 'traditional' one.
Day 11: Hex Edday11.nimday11.mlday11.pyPython version uses cube coordinates, Nim and OCaml versions use axial coordinates.
Day 12: Digital Plumberday12.nimday12.mlday12.pyBFS in Python, DFS in Nim and OCaml.
Day 13: Packet Scannersday13.nimday13.mlday13.pyAll three versions precalculate possible values of delay using Chinese remainder theorem to gain a significant speedup.
Day 14: Disk Defragmentationday14.nimday14.mlday14.py
Day 15: Dueling Generatorsday15.nimday15.mlday15.pyPython: generator generator generating generator's values. In Nim, using bit masking gives great speed boost.
Day 16: Permutation Promenadeday16.nimday16.mlday16.py
Day 17: Spinlockday17.nimday17.mlday17.pyBrute force in Python, using deque.rotate. The expected version in Nim, optimized.
Day 18: Duetday18.nimday18.mlday18.py
Day 19: A Series of Tubesday19.nimday19.mlday19.pyAll three solutions use complex numbers, which are great for the rotations in 2D plane.
Day 20: Particle Swarmday20.nimday20.mlday20.py
Day 21: Fractal Artday21.nimday21.mlday21.pyUnoptimized solution in OCaml. Nim and Python solutions are optimized for the second part. Python version uses numpy and expands the grid (3 steps at once), Nim version counts the number of times each pattern is present after 18 iterations.
Day 22: Sporifica Virusday22.nimday22.mlday22.pyOCaml: sum types and pattern matching is the name of the game. Python version uses a dict and a complex plane, Nim version uses an array (faster than a table) of a regular 2D plane with enum for the rotating directions.
Day 23: Coprocessor Conflagrationday23.nimday23.mlday23.py
Day 24: Electromagnetic Moatday24.nimday24.mlday24.pyBFS in Python. A recursive search in Nim and OCaml, optimized.
Day 25: The Halting Problemday25.nimday25.mlday25.pyPython version uses (default)dict. Nim version uses arrays, which are much faster than tables.
Total time:0.49 sec1.01 sec*16.1 sec** OCaml: unoptimized day21.ml. Python: without the brute-forced day17.py, and day15.py was run in pypy3. For the detailed run times, see below.

 

Run times

Times are in milliseconds, the reported results are the average of 20 runs.

daynimocamlpython
010.30.813.3
020.50.813.2
030.40.713.6
042.32.415.7
0569.063.82732.1
062.43.632.6
072.43.920.9
081.21.514.5
090.41.917.2
100.51.424.7
110.61.319.5
121.82.316.7
131.417.421.5
148.832.3566.2
15195.4383.22083*
166.871.0143.5
170.94.8-*
180.74.688.8
190.81.221.4
209.218.21622.5
210.5145.0197.1
2250.4139.33555.9
230.51.015.1
2410.852.11242.2
2548.852.33649.1

OCaml Day21 was not optimized.
Python Day15 was run with pypy3, Python Day17 was brute-forced.