This repository has been archived by the owner on Jul 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday24.kt
220 lines (207 loc) · 9.54 KB
/
day24.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import kotlin.math.*
private abstract class Expr {
abstract fun simplifyCustom() : Expr
abstract val possibleValues: List<Long>?
abstract val minValue: Long
abstract val maxValue: Long
fun simplify(): Expr = if (possibleValues?.size == 1) ConstExpr(possibleValues!!.single()) else
simplifyCustom().let { if (it != this) it.simplify() else it }
operator fun plus(other: Expr) = AddExpr(this, other).simplify()
operator fun times(other: Expr) = MulExpr(this, other).simplify()
operator fun div(other: Expr) = DivExpr(this, other).simplify()
operator fun rem(other: Expr) = ModExpr(this, other).simplify()
}
private data class ConstExpr(val x: Long) : Expr() {
override val possibleValues = listOf(x)
override val minValue = x
override val maxValue = x
override fun simplifyCustom() = this
override fun toString() = x.toString()
}
private data class VarExpr(val x: Int) : Expr() {
override val possibleValues = (1L..9L).toList()
override val minValue = 1L
override val maxValue = 9L
override fun simplifyCustom() = this
override fun toString() = "x$x"
}
private data class AddExpr(val l:Expr, val r:Expr) : Expr() {
override val possibleValues =
if (l.possibleValues == null || r.possibleValues == null)
null
else
l.possibleValues!!.flatMap { lit -> r.possibleValues!!.map { lit + it } }.distinct().takeIf {
it.size <= 1000
}?.distinct()
override val minValue = l.minValue + r.minValue
override val maxValue = l.maxValue + r.maxValue
override fun simplifyCustom() : Expr = when {
l is ConstExpr && r is ConstExpr -> ConstExpr(l.x + r.x)
l is AddExpr && l.r is ConstExpr && r is ConstExpr -> l.l + ConstExpr(l.r.x + r.x)
l is ConstExpr -> AddExpr(r, l).simplify()
r is ConstExpr && r.x == 0L -> l
l is AddExpr && l.r is ConstExpr -> l.l + (l.r + r)
r is AddExpr && r.r is ConstExpr -> (l + r.l) + r.r
l is IfEqExpr -> IfEqExpr(l.l, l.r, l.onEq + r, l.onNeq + r)
r is IfEqExpr -> IfEqExpr(r.l, r.r, l + r.onEq, l + r.onNeq)
else -> this
}
override fun toString() = "($l+$r)"
}
private data class MulExpr(val l:Expr, val r:Expr) : Expr() {
override fun toString() = "($l*$r)"
override val possibleValues = if (l.possibleValues == null || r.possibleValues == null)
null
else
l.possibleValues!!.flatMap { lit -> r.possibleValues!!.map { lit * it } }.distinct().takeIf {
it.size <= 1000
}?.distinct()
override val minValue = minOf(l.minValue * r.minValue, l.minValue * r.maxValue, l.maxValue * r.minValue, l.maxValue * r.maxValue)
override val maxValue = minOf(l.minValue * r.minValue, l.minValue * r.maxValue, l.maxValue * r.minValue, l.maxValue * r.maxValue)
override fun simplifyCustom() : Expr = when {
l is ConstExpr && r is ConstExpr -> ConstExpr(l.x * r.x)
l is MulExpr && l.r is ConstExpr && r is ConstExpr -> l.l * ConstExpr(l.r.x * r.x)
l is ConstExpr -> r * l
r is ConstExpr && r.x == 0L -> ConstExpr(0)
r is ConstExpr && r.x == 1L -> l
r is ConstExpr && l is AddExpr -> (l.l * r) + (l.r * r)
r is ConstExpr && l is DivExpr -> (l.l * r) / l.r
l is IfEqExpr -> IfEqExpr(l.l, l.r, l.onEq * r, l.onNeq * r)
r is IfEqExpr -> IfEqExpr(r.l, r.r, l * r.onEq, l * r.onNeq)
else -> this
}
}
private data class DivExpr(val l:Expr, val r:Expr) : Expr() {
override fun toString() = "($l/$r)"
override val possibleValues = if (l.possibleValues == null || r.possibleValues == null)
null
else
l.possibleValues!!.flatMap { lit -> r.possibleValues!!.filter { it > 0 }.map { lit / it } }.distinct().takeIf {
it.size <= 1000
}
override val minValue = l.minValue / r.maxValue
override val maxValue = l.maxValue / r.minValue
override fun simplifyCustom() : Expr = when {
l is ConstExpr && r is ConstExpr -> ConstExpr(l.x / r.x)
l is DivExpr -> l.l / (l.r * r)
r is ConstExpr && r.x == 1L -> l
l is MulExpr && r is ConstExpr && l.r is ConstExpr && l.r.x % r.x == 0L -> l.l * (l.r / r)
l is AddExpr && r is ConstExpr ->
listOf(this, ((l.l / r) + (l.r / r))).minByOrNull {
it.toString().length
}!!
l is IfEqExpr -> IfEqExpr(l.l, l.r, l.onEq / r, l.onNeq / r)
else -> this
}
}
private data class ModExpr(val l:Expr, val r:Expr) : Expr() {
override fun toString() = "($l%$r)"
override val minValue = 0L
override val maxValue = r.maxValue - 1
override val possibleValues = if (l.possibleValues == null || r.possibleValues == null)
null
else
l.possibleValues!!.flatMap { lit -> r.possibleValues!!.filter { it > 0 }.map { lit % it } }.distinct().takeIf {
it.size <= 1000
}
override fun simplifyCustom() : Expr = when {
l is ConstExpr && r is ConstExpr -> ConstExpr(l.x % r.x)
r is ConstExpr && l.possibleValues != null && l.possibleValues!!.all { it < r.x } -> l
l is AddExpr -> listOf(this, ModExpr((l.l % r) + (l.r % r), r)).minByOrNull { it.toString().length }!!
l is MulExpr -> listOf(this, ModExpr((l.l % r) * (l.r % r), r)).minByOrNull { it.toString().length }!!
l is IfEqExpr -> IfEqExpr(l.l, l.r, l.onEq % r, l.onNeq % r)
else -> this
}
}
private data class IfEqExpr(val l:Expr, val r:Expr, val onEq: Expr, val onNeq: Expr) : Expr() {
override fun toString() = "(if ($l==$r) $onEq else $onNeq)"
override val possibleValues =
if (onEq.possibleValues == null || onNeq.possibleValues == null)
null
else
(onEq.possibleValues!! + onNeq.possibleValues!!).distinct().takeIf {
it.size <= 1000
}
override val minValue = min(onEq.minValue, onNeq.minValue)
override val maxValue = max(onEq.maxValue, onNeq.maxValue)
override fun simplifyCustom() = when {
onEq.toString() == onNeq.toString() -> onEq
l is IfEqExpr && r is ConstExpr && r.x == 1L -> l
l is IfEqExpr && r is ConstExpr && r.x == 0L -> IfEqExpr(l.l, l.r, l.onNeq, l.onEq).simplify()
onEq is IfEqExpr && onEq.l.toString() == l.toString() && onEq.r.toString() == r.toString() -> IfEqExpr(
l, r, onEq.onEq, onNeq
).simplify()
onNeq is IfEqExpr && onNeq.l.toString() == l.toString() && onNeq.r.toString() == r.toString() -> IfEqExpr(
l, r, onEq, onNeq.onNeq
).simplify()
r.possibleValues == null -> this
r.possibleValues!!.none { it in l.minValue..l.maxValue } -> onNeq
l.possibleValues == null -> this
l.possibleValues!!.intersect(r.possibleValues!!.toSet()).isEmpty() -> onNeq
l.possibleValues!!.size == 1 && r.possibleValues!!.size == 1 ->
if (l.possibleValues!!.single() == r.possibleValues!!.single()) onEq else onNeq
else -> this
}
}
private fun simplifyNestedIf(expr: Expr, trueConds:Set<String>, falseConds:Set<String>, replaceLeafs: Boolean) : Expr = if (expr !is IfEqExpr) {
if (replaceLeafs) {
IfEqExpr(expr, ConstExpr(0), ConstExpr(1), ConstExpr(0)).simplify()
} else {
expr
}
} else {
val c = "${expr.l}==${expr.r}"
if (c in trueConds)
simplifyNestedIf(expr.onEq, trueConds, falseConds, replaceLeafs)
else if (c in falseConds)
simplifyNestedIf(expr.onNeq, trueConds, falseConds, replaceLeafs)
else if (expr.l is IfEqExpr) {
simplifyNestedIf(IfEqExpr(
expr.l.l,
expr.l.r,
IfEqExpr(expr.l.onEq, expr.r, expr.onEq, expr.onNeq),
IfEqExpr(expr.l.onNeq, expr.r, expr.onEq, expr.onNeq),
), trueConds, falseConds, replaceLeafs)
} else
IfEqExpr(
simplifyNestedIf(expr.l, trueConds, falseConds, false),
simplifyNestedIf(expr.r, trueConds, falseConds, false),
simplifyNestedIf(expr.onEq, trueConds + c, falseConds, replaceLeafs),
simplifyNestedIf(expr.onNeq, trueConds, falseConds + c, replaceLeafs)).simplify()
}
private fun nameToReg(s: String) = when (s) {
"w" -> 0
"x" -> 1
"y" -> 2
"z" -> 3
else -> TODO(s)
}
private fun nameToVal(acc:List<Expr>, s: String) = s.toIntOrNull()?.let { ConstExpr(it.toLong()) } ?:
acc[nameToReg(s)]
private fun Expr.collectOnes() : List<List<String>> = when (this) {
is ConstExpr -> if (x == 1L) listOf(emptyList()) else emptyList()
is IfEqExpr -> onEq.collectOnes().map { it + "$l==$r" } + onNeq.collectOnes().map { it + "$l!=$r" }
else -> TODO()
}
fun main() {
var varId = 1
val data = readAllLines().map { it.split(" ") }
val r = data.fold(
MutableList<Expr>(4) { ConstExpr(0) }
) { acc, v ->
when (v[0]) {
"inp" -> acc[nameToReg(v[1])] = VarExpr(varId++).simplify()
"add" -> acc[nameToReg(v[1])] = nameToVal(acc, v[1]) + nameToVal(acc, v[2])
"mul" -> acc[nameToReg(v[1])] = nameToVal(acc, v[1]) * nameToVal(acc, v[2])
"div" -> acc[nameToReg(v[1])] = nameToVal(acc, v[1]) / nameToVal(acc, v[2])
"mod" -> acc[nameToReg(v[1])] = nameToVal(acc, v[1]) % nameToVal(acc, v[2])
"eql" -> acc[nameToReg(v[1])] = IfEqExpr(nameToVal(acc, v[1]), nameToVal(acc, v[2]), ConstExpr(1L), ConstExpr(0L)).simplify()
else -> TODO(v[0])
}
acc
}[3].let {
simplifyNestedIf(it, emptySet(), emptySet(), true)
}
println("$r")
println(r.collectOnes().joinToString("\n"))
}