-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day05.kt
130 lines (125 loc) · 4.74 KB
/
Day05.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import java.io.File
fun main() {
println(Day05().part1(1))
println(Day05().part2(5))
}
class Day05 {
fun part1(input: Int): Int {
val vals =
File("files/day5.txt").bufferedReader().readLine().split(",").map { x: String -> x.toInt() }.toMutableList()
var i = 0
while (i < vals.size) {
var fullcode = vals[i].toString()
while (fullcode.length != 5) {
fullcode = "0$fullcode"
}
val opcode = fullcode.substring(fullcode.length - 2).toInt()
when (opcode) {
1 -> {
val param1 = if (fullcode[2] == '1') vals[i + 1] else vals[vals[i + 1]]
val param2 = if (fullcode[1] == '1') vals[i + 2] else vals[vals[i + 2]]
vals[vals[i + 3]] = param1 + param2
i += 4
}
2 -> {
val param1 = if (fullcode[2] == '1') vals[i + 1] else vals[vals[i + 1]]
val param2 = if (fullcode[1] == '1') vals[i + 2] else vals[vals[i + 2]]
vals[vals[i + 3]] = param1 * param2
i += 4
}
3 -> {
vals[vals[i + 1]] = input
i += 2
}
4 -> {
val param1 = if (fullcode[2] == '1') vals[i + 1] else vals[vals[i + 1]]
if (param1 != 0) {
return param1
}
i += 2
}
99 -> {
return vals[0]
}
else -> return -1
}
}
return -1
}
fun part2(input: Int): Int {
val vals =
File("files/day5.txt").bufferedReader().readLine().split(",").map { x: String -> x.toInt() }.toMutableList()
var i = 0
while (i < vals.size) {
var fullcode = vals[i].toString()
while (fullcode.length != 5) {
fullcode = "0$fullcode"
}
val opcode = fullcode.substring(fullcode.length - 2).toInt()
when (opcode) {
1 -> {
val param1 = if (fullcode[2] == '1') vals[i + 1] else vals[vals[i + 1]]
val param2 = if (fullcode[1] == '1') vals[i + 2] else vals[vals[i + 2]]
vals[vals[i + 3]] = param1 + param2
i += 4
}
2 -> {
val param1 = if (fullcode[2] == '1') vals[i + 1] else vals[vals[i + 1]]
val param2 = if (fullcode[1] == '1') vals[i + 2] else vals[vals[i + 2]]
vals[vals[i + 3]] = param1 * param2
i += 4
}
3 -> {
vals[vals[i + 1]] = input
i += 2
}
4 -> {
return if (fullcode[2] == '1') vals[i + 1] else vals[vals[i + 1]]
}
5 -> {
val param1 = if (fullcode[2] == '1') vals[i + 1] else vals[vals[i + 1]]
val param2 = if (fullcode[1] == '1') vals[i + 2] else vals[vals[i + 2]]
if (param1 != 0) {
i = param2
} else {
i += 3
}
}
6 -> {
val param1 = if (fullcode[2] == '1') vals[i + 1] else vals[vals[i + 1]]
val param2 = if (fullcode[1] == '1') vals[i + 2] else vals[vals[i + 2]]
if (param1 == 0) {
i = param2
} else {
i += 3
}
}
7 -> {
val param1 = if (fullcode[2] == '1') vals[i + 1] else vals[vals[i + 1]]
val param2 = if (fullcode[1] == '1') vals[i + 2] else vals[vals[i + 2]]
if (param1 < param2) {
vals[vals[i + 3]] = 1
} else {
vals[vals[i + 3]] = 0
}
i += 4
}
8 -> {
val param1 = if (fullcode[2] == '1') vals[i + 1] else vals[vals[i + 1]]
val param2 = if (fullcode[1] == '1') vals[i + 2] else vals[vals[i + 2]]
if (param1 == param2) {
vals[vals[i + 3]] = 1
} else {
vals[vals[i + 3]] = 0
}
i += 4
}
99 -> {
return vals[0]
}
else -> return -1
}
}
return -1
}
}