File tree Expand file tree Collapse file tree 2 files changed +48
-0
lines changed
main/kotlin/de/ronny_h/aoc/year2018/day01
test/kotlin/de/ronny_h/aoc/year2018/day01 Expand file tree Collapse file tree 2 files changed +48
-0
lines changed Original file line number Diff line number Diff line change 1+ package de.ronny_h.aoc.year2018.day01
2+
3+ import de.ronny_h.aoc.AdventOfCode
4+
5+ fun main () = ChronalCalibration ().run (500 , 709 )
6+
7+ class ChronalCalibration : AdventOfCode <Int >(2018 , 1 ) {
8+ override fun part1 (input : List <String >): Int = input.toListOfInt().sum()
9+
10+ private fun List<String>.toListOfInt (): List <Int > = map {
11+ if (it.startsWith(' +' )) it.substring(1 ).toInt() else it.toInt()
12+ }
13+
14+ override fun part2 (input : List <String >): Int {
15+ var frequency = 0
16+ val seenFrequencies = mutableSetOf (frequency)
17+ generateSequence { input.toListOfInt() }
18+ .flatten()
19+ .takeWhile {
20+ frequency + = it
21+ seenFrequencies.add(frequency)
22+ }.last()
23+ return frequency
24+ }
25+ }
Original file line number Diff line number Diff line change 1+ package de.ronny_h.aoc.year2018.day01
2+
3+ import de.ronny_h.aoc.extensions.asList
4+ import io.kotest.core.spec.style.StringSpec
5+ import io.kotest.matchers.shouldBe
6+
7+ class ChronalCalibrationTest : StringSpec ({
8+
9+ val input = """
10+ +1
11+ -2
12+ +3
13+ +1
14+ """ .asList()
15+
16+ " part 1: the resulting frequency after all of the changes in frequency have been applied" {
17+ ChronalCalibration ().part1(input) shouldBe 3
18+ }
19+
20+ " part 2: the first frequency the device reaches twice" {
21+ ChronalCalibration ().part2(input) shouldBe 2
22+ }
23+ })
You can’t perform that action at this time.
0 commit comments