Skip to content

Commit 723ef0e

Browse files
committed
Day01 - Part 2
1 parent 5c9f823 commit 723ef0e

File tree

2 files changed

+35
-44
lines changed

2 files changed

+35
-44
lines changed

src/main/kotlin/Day01.kt

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,38 @@
11
import Day01.Rotation.Left
22
import Day01.Rotation.Right
33
import kotlin.io.path.readLines
4+
import kotlin.math.absoluteValue
45

56
class Day01 : Day {
67
private val start = 50
78

89
override fun partOne(filename: String, verbose: Boolean): Int {
9-
val rotations = filename.asPath().readLines().map { line ->
10+
val rotations = filename.parseRotations(verbose)
11+
12+
return rotations
13+
.map { it.movement }
14+
.runningFold(start, Int::plus)
15+
.count { it % 100 == 0 }
16+
}
17+
18+
override fun partTwo(filename: String, verbose: Boolean): Any {
19+
val rotations = filename.parseRotations(verbose)
20+
21+
return rotations
22+
.flatMap {
23+
(1..(it.movement).absoluteValue).map { _ ->
24+
when(it) {
25+
is Left -> -1
26+
is Right -> 1
27+
}
28+
}
29+
}
30+
.runningFold(start, Int::plus)
31+
.count { it % 100 == 0 }
32+
}
33+
34+
private fun String.parseRotations(verbose: Boolean): List<Rotation> {
35+
val rotations = asPath().readLines().map { line ->
1036
when (line.first()) {
1137
'L' -> Left(line.drop(1).toInt())
1238
'R' -> Right(line.drop(1).toInt())
@@ -20,55 +46,19 @@ class Day01 : Day {
2046
}
2147
}
2248

23-
var current = start
24-
25-
if (verbose) {
26-
println("The dial starts by pointing at $current")
27-
}
28-
29-
var zeroCount = 0
30-
rotations.forEach { rotation ->
31-
current = rotation.rotate(current)
32-
if (verbose) {
33-
println("The dial is rotated $rotation to point at $current")
34-
}
35-
if (current == 0) {
36-
zeroCount++
37-
}
38-
}
39-
40-
return zeroCount
41-
}
42-
43-
override fun partTwo(filename: String, verbose: Boolean): Any {
44-
TODO("Not yet implemented")
49+
return rotations
4550
}
4651

4752
private sealed interface Rotation {
48-
fun rotate(current: Int): Int
53+
val movement: Int
4954

50-
data class Left(private val steps: Int) : Rotation {
51-
override fun rotate(current: Int): Int {
52-
var next = current - steps
53-
while (next < 0) {
54-
next += 100
55-
}
56-
return next
57-
}
58-
59-
override fun toString() = "L$steps"
55+
data class Left(val distance: Int) : Rotation {
56+
override val movement = -distance
57+
override fun toString() = "L$distance"
6058
}
6159

62-
data class Right(private val steps: Int) : Rotation {
63-
override fun rotate(current: Int): Int {
64-
var next = current + steps
65-
while (next > 99) {
66-
next -= 100
67-
}
68-
return next
69-
}
70-
71-
override fun toString() = "R$steps"
60+
data class Right(override val movement: Int) : Rotation {
61+
override fun toString() = "R$movement"
7262
}
7363
}
7464

src/test/kotlin/Day01Test.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
class Day01Test : DayTest<Day01>("Day01_test.txt") {
22
override val partOneExpected = 3
3+
override val partTwoExpected = 6
34
}

0 commit comments

Comments
 (0)