/advent-of-code-2021

Solutions to Advent of Code 2021 in Kotlin

Primary LanguageKotlin

🎄🎁🎅 2021 Advent of Code in Kotlin 🎅🎁🎄

Project goals

The project goal is to deliver some pretty, readable and concise solutions to Advent of Code 2021 problems all written in Kotlin language. It should show the other developer how some constructions from the language can be used and how to solve some kind of tricky problems that appear during the Advent of Code.

Problems source

You can find all problems at the page of Advent of Code 2021. The description of each problem contains some sample test data, but I also included my input data files from the contest in the resources' directory of the project to make my project working with some sample, real world data.

Solution template

When soling each day problem I use my template of AdventDay that I defined in AdventDay.kt - it's worth looking into this definition by yourself, and also you can read more about it at my blog.

It's enough to create some Kotlin object that inherits from AdventDay to get the solution running. If you're interested in the details, let's look into the definition of the Advent.kt to see how to find all object classes in Kotlin and run some method on them 😎.

To run all solutions, simply call ./gradlew run

Tests

The solutions are tested with the values gathered from adventofcode.com. Every day of Advent has its expected output defined in AdventTest.kt and in order to verify correct state of the outputs for days we run every day solution and catch it standard output stream to compare it with expected value.

To run all tests, simply call ./gradlew test

Problems

The problems solutions are included in project, but for every of them you can also find some corresponding article at my website, where I discuss not only the given problem, but also some cool features of Kotlin or I deep dive into some language constructions.

Problem Solution Blog Post Tags
Day 1: Sonar Sweep Day1.kt Day 1 Blog Post Sequence<T>, windowed
Day 2: Dive! Day2.kt Day 2 Blog Post fold, inline, takeIf
Day 3: Binary Diagnostic Day3.kt Day 3 Blog Post readable, abstracted
Day 4: Giant Squid Day4.kt Day 4 Blog Post groupDividedBy, transpose, context
Day 5: Hydrothermal Venture Day5.kt Day 5 Blog Post DefaultMap<K, V>, directedTo, IntProgression
Day 6: Lanternfish Day6.kt Day 6 Blog Post crossinline, data structures
Day 7: The Treachery of Whales Day7.kt Day 7 Blog Post median, average, math, named args
Day 8: Seven Segment Search Day8.kt Day 8 Blog Post operator fun, deduction
Day 9: Smoke Basin Day9.kt Day 9 Blog Post operator fun, tailrec fun, dfs, bfs
Day 10: Syntax Scoring Day10.kt Day 10 Blog Post foldRight, Stack<V>, bracket pairing
Day 11: Dumbo Octopus Day11.kt Day 11 Blog Post Sequence<T>, operator fun set, operator fun get, LazyDefaultMap<K, V>
Day 12: Passage Pathing Day12.kt Day 12 Blog Post @JvmInline, value class, dfs, paths in graph
Day 13: Transparent Origami Day13.kt Day 13 Blog Post fold, immutable data, reflections, origami
Day 14: Extended Polymerization Day14.kt Day 14 Blog Post immutable data, data structure, buildMap { }, operator fun invoke
Day 15: Chiton Day15.kt Day 15 Blog Post weighted graph, dijkstra, shortest path
Day 16: Packet Decoder Day16.kt Day 16 Blog Post by notNull(), delegated properties, Sequence<T>, date parsing
Day 17: Trick Shot Day17.kt Day 17 Blog Post simulation, physics, immutable data
Day 18: Snailfish Day18.kt Day 18 Blog Post tree, reduce, traversal, lateinit var
Day 19: Beacon Scanner Day19.kt Day 19 Blog Post 3D rotation, matching, infix fun, named block
Day 20: Trench Map Day20.kt Day 20 Blog Post infix fun, operator fun, extension properties, IntRange
Day 21: Dirac Dice Day21.kt Day 21 Blog Post simulate, immutable data, DefaultMap<K, V>, counting, fold, generateSequence
Day 22: Reactor Reboot Day22.kt Day 22 Blog Post IntRange, Range3D, infix fun, operator fun
Day 23: Amphipod Day23.kt Day 23 Blog Post HashSet, simulation, memorization, Dijkstra, data class
Day 24: Arithmetic Logic Unit Day24.kt Day 24 Blog Post ALU, reverse engineering, interpreter, sealed interface vs enum class
Day 25: Sea Cucumber Day25.kt Day 25 Blog Post immutable data class, function receiver, Sequence<T>