My solutions for the Advent of Code 2021 written in Kotlin.
This repo is based my Kotlin template for AoC
https://github.com/henningBunk/advent-of-code-2021/blob/main/app/src/y2021/day01/Day01.kt
Let's begin :-)
First I followed a plan of giving fold
a try but was bummed that I only got the acc and not two values to compare with each other. Therefore, I had to handle list indices. Solved it though. Cleaned it up with some extension functions and without using fold, but rather with list transforms.
After looking at other solutions I learned about the .windowed
function and implemented an alternative solution with it.
I wanted to solve it only by transforming the input data without creating variables. It was a challenge but it worked quite well. I introduced two classes for the second part to make it more readable.
Wow, that was harder than I expected. I took forever for that .transpose()
function and regretted for the first time not using Python.
For part 2 I could hone my rusty recursion skills which I only used at AoC outside of university.
I think I should come back to this problem and work on a simpler and cleaner solution.
Nothing special on this day. I tried to write clean and tested code. I am quite happy with the results and even worked test driven for some parts.
The previous days prepared me well for writing the winning conditions which were quite fun to write. I also wrote one for diagonals which I later discovered were unnecessary.
Again pretty straight forward today. I used a list of mutable lists to count the vents. I like how the extension functions make it very easy to read what's happening in the solve functions.
My naive approach for part 1 could heat up the room pretty good for part 2.
Pretty happy with this one since it only took me only a few minutes and both tests were green on the first try, such a rare sight.
I wonder if I there is a sigma operand for the second calculation, I feel like there should be one.
Also, the runtime is pretty slow since it's a brute force approach. But right now I don't feel like optimizing. Found the trick with the triangle numbers. Which reduced the runtime from 5s to 100ms, not using higher order function reduced it further to 30ms.
I struggled with this one. I chose the wrong datatype to store my remaining candidates for each segment. I used an array of strings and for the operations I used char arrays. Here I got hugely confused with Kotlin's Array and CharArray. Afterwards I read the docs and am wiser now :) I refactored my solution now that it is easier to read and more concise but it is still my original logic. I would like to implement it again using sets because I think that's way more appropriate. Also would like to test out a solution of a colleague who didn't decode the pattern but immediately determined the value of the codes.
Great and interesting day!
// edit:
Tried it again using a map and the approach of my colleague not to decode each segment by itself but rather the numbers as a whole. SO. MUCH. EASIER!
See: https://github.com/henningBunk/advent-of-code-2021/blob/main/app/src/y2021/day08/Day08WithoutAnalyzingIndividualSegments.kt
Simple solution with a queue.
Solved this one with my colleague using Ping-Pong-TDD-Pair-Programming. Great practice and lots of fun :)
Nothing special today. I like the solution of putting the octopus in a map with the coordinates as a key. Makes finding the neighbours pretty easy and you don't have to check for limits of the map. Since each octopus knows it's neighbours, I don't need to remember their position of the map which is nice too.
TODO
TODO
TODO
TODO
TODO
TODO
TODO
- To create all days for the current year, execute
./gradlew create
- To create a new day for the current year, execute
./gradlew create --day=9
- To create a new day for a specific year, execute
./gradlew create --year=2019 --day=9
- To create all days for a year execute
./gradlew create --year=2019
This repo was build using inspiration from these repositories which I am thankful for:
- https://gitlab.com/martyschaer/advent-of-code-complete
- Interface for the Solutions with solving funs
- Annotations to mark the year and days
- https://github.com/kotlin-hands-on/advent-of-code-kotlin-template
- A simpler folder structure
- A single run command to run tests against the sample data and on success resume with the real data