-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day25.kt
77 lines (74 loc) · 2.75 KB
/
Day25.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package aoc2017.day25
fun computeDiagnosticChecksum(steps: Int = 12919244): Int {
var currentState = State.A
var currentIndex = 0
val machineMap = mutableMapOf(currentIndex to 0)
repeat(steps) {
when (currentState) {
State.A ->
if (isValueNullOrZero(machineMap, currentIndex)) {
machineMap[currentIndex] = 1
currentIndex++
currentState = State.B
} else {
machineMap[currentIndex] = 0
currentIndex--
currentState = State.C
}
State.B ->
if (isValueNullOrZero(machineMap, currentIndex)) {
machineMap[currentIndex] = 1
currentIndex--
currentState = State.A
} else {
machineMap[currentIndex] = 1
currentIndex++
currentState = State.D
}
State.C ->
if (isValueNullOrZero(machineMap, currentIndex)) {
machineMap[currentIndex] = 1
currentIndex++
currentState = State.A
} else {
machineMap[currentIndex] = 0
currentIndex--
currentState = State.E
}
State.D ->
if (isValueNullOrZero(machineMap, currentIndex)) {
machineMap[currentIndex] = 1
currentIndex++
currentState = State.A
} else {
machineMap[currentIndex] = 0
currentIndex++
currentState = State.B
}
State.E ->
if (isValueNullOrZero(machineMap, currentIndex)) {
machineMap[currentIndex] = 1
currentIndex--
currentState = State.F
} else {
machineMap[currentIndex] = 1
currentIndex--
currentState = State.C
}
State.F ->
if (isValueNullOrZero(machineMap, currentIndex)) {
machineMap[currentIndex] = 1
currentIndex++
currentState = State.D
} else {
machineMap[currentIndex] = 1
currentIndex++
currentState = State.A
}
}
}
return machineMap.values.count { it == 1 }
}
private fun isValueNullOrZero(machineMap: Map<Int, Int>, currentIndex: Int) =
machineMap[currentIndex] == null || machineMap[currentIndex] == 0
enum class State { A, B, C, D, E, F }