Skip to content

Commit 907b066

Browse files
committed
Solution 2018-01 (Chronal Calibration)
1 parent a016593 commit 907b066

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
})

0 commit comments

Comments
 (0)