Advent of Code 2022
In the next couple of weeks I'll be working on last years Advent of Code to get a better understanding of Kotlin.
Over the next couple of weeks I’ll be posting about the Advent of Code 2022 solutions. For each day I’ll create three posts, a post about the data sanitation. And of course for both parts a separate blog post, so one post can just focus on one particular problem.
The objective of each day is available on the advent of code website, so it won’t be shared here.
I’ll be using Kotlin to create this year’s Advent of Code because I want to learn Kotlin and this will be a nice starting point.
Project structure
Before we can get started with the actual Advent of Code, we need to setup our project stucture. For this I’ll be using different submodules for each day, so we can truly seperate the different objectives.
- aoc-2022
- day1
- src
- main
- kotlin
- aoc
- PartOne.kt
- PartTwo.kt
- Sanitizer.kt
- main.kt
- resources
- input.txt
- test
- kotlin
- aoc
- PartOneTest.kt
- PartTwoTest.kt
- SanitizerTest.kt
- resources
- input.txt
- build.gradle.kts
- day2
- src
- main
- kotlin
- aoc
- PartOne.kt
- PartTwo.kt
- Sanitizer.kt
- main.kt
- resources
- input.txt
- test
- kotlin
- aoc
- PartOneTest.kt
- PartTwoTest.kt
- SanitizerTest.kt
- resources
- input.txt
- build.gradle.kts
- dayN
- src
- main
- kotlin
- aoc
- PartOne.kt
- PartTwo.kt
- Sanitizer.kt
- main.kt
- resources
- input.txt
- test
- kotlin
- aoc
- PartOneTest.kt
- PartTwoTest.kt
- SanitizerTest.kt
- resources
- input.txt
- build.gradle.kts
- settings.gradle.kts The puzzle input well be stored in the input.txt file under the resources folder. For the tests, the input.txt will contain the sample input.
This way, we can setup our test cases to use the sample input and validate that the output equals the sample answer. And we don’t need to update our
code when switching between the sample data and the actual data.
Decisions
In our file structure you can see that we’ve already made some decisions for our implementation. One being that we create a module for each day, and another one being that we seperate the data sanitation and the two parts of each day.
The reason we create a module for each day, is that experience has thought me that no day depends on the previous day. So it doesn’t make sense to put all the different types of objectives in the same project or module.
And for the seperation between the data sanitation and the parts of each day, this is done so each part can focus on it’s specific logic without worrying about the data structure. You could say that both parts of the day can be put in one class or one file, but I think it’s cleaner to completely seperate them and have duplicate code than to have very generic shared code.
The main.kt file contains a Main class and a main method as the entry-point for the module. This is used to execute the code with the actual input data. In most cases
the main method will look something like the following code. So for this reason, this code isn’t shared in the upcoming blogposts.
fun main() {
val resource = {}::class.java.getResource("/input.txt")
val sanitizer = Sanitizer(resource)
// Part one
val partOne = PartOne(sanitizer)
println("Part one: ${partOne.getResult()}")
// Part two
val partTwo = PartTwo(sanitizer)
println("Part two: ${partTwo.getResult()}")
} Posts in this series
AoC 2022, Day one - Sanitizer
Parse the input for day one into a table of elves.
AoC 2022, Day one - Part One
Count the calories that each elve is carrying
AoC 2022, Day one - Part Two
Calculate which elve is carrying the most calories
AoC 2022, Day two - Sanitizer
Turn the input into a list of rock-paper-scissor games.
AoC 2022, Day two - Part One
Get the outcome of each rock-paper-scissors match.
AoC 2022, Day two - Part Two
Calculate the strategy we need to play to get the expected outcome based on our opponents move.
AoC 2022, Day three - Sanitizer
Turn the input into a list of rucksacks.
AoC 2022, Day three - Part One
Find the same items that are stored in the two compartments of each rucksack.
AoC 2022, Day three - Part Two
Find the item type that is common between two or more elves.
AoC 2022, Day four - Sanitizer
Turn the input into a list of assigned compartments for the elves to unload.
AoC 2022, Day four - Part One
Find the compartments that overlap between the different assignments.
AoC 2022, Day four - Part Two
Find the compartments that overlap completely between the different assignments.
AoC 2022, Day five - Sanitizer
Turn the input into a list of stacks and another list of operations for the crane.
AoC 2022, Day five - Part One
Execute the instructions to move the cargo from one stack to another.
AoC 2022, Day 5 - Part Two
Execute the instructions to move the cargo from one stack to another in the same order.
AoC 2022, Day six - Sanitizer
Turn the input into a stream returned from a signal.
AoC 2022, Day six - Part One
Find the start of packet marker in the data stream.
AoC 2022, Day six - Part Two
Get the first character after the start of packet marker from the stream.