-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day21.kt
95 lines (78 loc) · 3.01 KB
/
Day21.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
package day21
import common.InputRepo
import common.readSessionCookie
import common.solve
fun main(args: Array<String>) {
val day = 21
val input = InputRepo(args.readSessionCookie()).get(day = day)
solve(day, input, ::solveDay21Part1, ::solveDay21Part2)
}
fun solveDay21Part1(input: List<String>): Long {
val expressions = input.map { parseJob(it) }.associateBy { it.name }
val eval = expressions["root"]!!.eval(expressions)
return eval
}
abstract class Expression(val name: String) {
abstract fun eval(expressions: Map<String, Expression>): Long
abstract fun print(expressions: Map<String, Expression>): String
}
class OperationExpression(name: String, val param1: String, val op: Char, val param2: String) : Expression(name) {
override fun eval(expressions: Map<String, Expression>): Long {
val expr1 = expressions[param1]!!.eval(expressions)
val expr2 = expressions[param2]!!.eval(expressions)
return when (op) {
'+' -> expr1 + expr2
'-' -> expr1 - expr2
'/' -> expr1 / expr2
'*' -> expr1 * expr2
else -> throw IllegalArgumentException("aaaa")
}
}
override fun print(expressions: Map<String, Expression>): String {
val expr1 = expressions[param1]!!.print(expressions)
val expr2 = expressions[param2]!!.print(expressions)
return when (op) {
'+' -> """($expr1 + $expr2)"""
'-' -> """($expr1 - $expr2)"""
'/' -> """($expr1 / $expr2)"""
'*' -> """($expr1 * $expr2)"""
'=' -> """($expr1) = ($expr2)"""
else -> throw IllegalArgumentException("aaaa")
}
}
}
class LiteralExpression(name: String, private val literal: Long) : Expression(name) {
override fun eval(expressions: Map<String, Expression>): Long {
return literal
}
override fun print(expressions: Map<String, Expression>): String {
return if (this.name == "humn") {
"x"
} else {
literal.toString()
}
}
}
fun parseJob(line: String): Expression {
val moveRegex = """^(\w+): (?:(\w+) ([+-/*]) (\w+)|(\d+))$""".toRegex()
return moveRegex.matchEntire(line)
?.destructured
?.let { (name, param1, op, param2, literal) ->
if (literal.isEmpty()) {
OperationExpression(name, param1, op.first(), param2)
} else {
LiteralExpression(name, literal.toLong())
}
}
?: throw IllegalArgumentException("Bad input '$line'")
}
fun solveDay21Part2(input: List<String>): Int {
val expressions = input.map { parseJob(it) }.associateBy { it.name }.toMutableMap()
val root = expressions["root"] as OperationExpression
expressions["root"] = OperationExpression(root.name, root.param1, '=', root.param2)
val eval = expressions["root"]!!.print(expressions)
println(eval)
// TODO solve the equation in code
// i just used https://www.mathpapa.com/algebra-calculator.html
return -1
}