1+ import Day01.Rotation.Left
2+ import Day01.Rotation.Right
3+ import kotlin.io.path.readLines
4+
5+ class Day01 : Day {
6+ private val start = 50
7+
8+ override fun partOne (filename : String , verbose : Boolean ): Int {
9+ val rotations = filename.asPath().readLines().map { line ->
10+ when (line.first()) {
11+ ' L' -> Left (line.drop(1 ).toInt())
12+ ' R' -> Right (line.drop(1 ).toInt())
13+ else -> throw IllegalArgumentException (" Unexpected line $line " )
14+ }
15+ }
16+
17+ if (verbose) {
18+ rotations.forEach {
19+ println (it)
20+ }
21+ }
22+
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" )
45+ }
46+
47+ private sealed interface Rotation {
48+ fun rotate (current : Int ): Int
49+
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 "
60+ }
61+
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 "
72+ }
73+ }
74+
75+ companion object : Day .Main (" Day01.txt" ) {
76+ @JvmStatic
77+ fun main (args : Array <String >) = main()
78+ }
79+ }
0 commit comments