-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day19.kts
123 lines (101 loc) · 2.69 KB
/
Day19.kts
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
import java.io.File
import java.util.*
import kotlin.system.exitProcess
// input processing
val inputs = File("inputs/day19.txt").readLines()
val ops = mapOf<String, (Int, Int) -> Int>("addr" to ::addr, "addi" to ::addi, "mulr" to ::mulr,
"muli" to ::muli, "banr" to ::banr, "bani" to ::bani, "borr" to ::borr, "bori" to ::bori, "setr" to ::setr,
"seti" to ::seti, "gtir" to ::gtir, "gtri" to ::gtri, "gtrr" to ::gtrr, "eqir" to ::eqir, "eqri" to ::eqri, "eqrr" to ::eqrr)
var registers = mutableListOf<Int>()
fun addr(a: Int, b: Int): Int {
return registers[a] + registers[b]
}
fun addi(a: Int, b: Int): Int {
return registers[a] + b
}
fun mulr(a: Int, b: Int): Int {
return registers[a] * registers[b]
}
fun muli(a: Int, b: Int): Int {
return registers[a] * b
}
fun banr(a: Int, b: Int): Int {
return registers[a].and(registers[b])
}
fun bani(a: Int, b: Int): Int {
return registers[a].and(b)
}
fun borr(a: Int, b: Int): Int {
return registers[a].or(registers[b])
}
fun bori(a: Int, b: Int): Int {
return registers[a].or(b)
}
fun setr(a: Int, b: Int): Int {
return registers[a]
}
fun seti(a: Int, b: Int): Int {
return a
}
fun gtir(a: Int, b: Int): Int {
if (a > registers[b]) return 1
else return 0
}
fun gtri(a: Int, b: Int): Int {
if (b < registers[a]) return 1
else return 0
}
fun gtrr(a: Int, b: Int): Int {
if (registers[a] > registers[b]) return 1
else return 0
}
fun eqir(a: Int, b: Int): Int {
if (a == registers[b]) return 1
else return 0
}
fun eqri(a: Int, b: Int): Int {
if (b == registers[a]) return 1
else return 0
}
fun eqrr(a: Int, b: Int): Int {
if (registers[a] == registers[b]) return 1
else return 0
}
val ip = inputs[0].replace("#ip ", "").toInt()
val ins = inputs.toMutableList()
ins.removeAt(0)
val instr = ins.map {
val details = it.split(" ")
val ins = ops[details[0]]!!
val params = details.toMutableList()
params.removeAt(0)
Pair(ins, params.map { it.toInt() })
}
registers = mutableListOf(1, 0, 0, 0, 0, 0)
var instNum = 0
instr.withIndex().forEach {(idx, c) -> println("$idx ${inputs[idx + 1]}")}
var r = 0
println()
while (instNum >= 0 && instNum < instr.count() && r<300000) {
println("$instNum ${inputs[instNum + 1]}")
val cur = instr[instNum]
registers[cur.second[2]] = cur.first(cur.second[0], cur.second[1])
for (i in 0..5) print("${registers[i]} ")
println()
registers[ip] = registers[ip] + 1
instNum = registers[ip]
r++
}
println(registers[0])
println(registers[ip])
/*
* Part 2
* R5 = 10551367
* for (R1 = 1; R1 <= R5; R++) {
* For (R4 = 1; R4 <= R5; R4++) {
* if (R1*R4 == R5) R0 += R1
* }
*
* }
*
*/