generated from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay21.kt
162 lines (143 loc) · 4.71 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
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
package year2022.`21`
import readInput
@JvmInline
value class NodeTitle(val value: String)
enum class OperationType {
PLUS, MINUS, MUL, DIV;
companion object {
fun from(string: String): OperationType {
return when (string) {
"*" -> MUL
"/" -> DIV
"+" -> PLUS
"-" -> MINUS
else -> error("!!!")
}
}
}
}
sealed class Command {
data class Value(val amount: Long) : Command()
data class Operation(
val leftNode: NodeTitle,
val rightNode: NodeTitle,
val type: OperationType
) : Command()
}
fun parseInput(input: List<String>): Map<NodeTitle, Command> {
return input
.map {
val res = it.split(": ")
val nodeTitle = NodeTitle(res[0])
val split = res[1].split(" ")
val rightSide = if (split.size == 1) {
Command.Value(split.first().toLong())
} else {
val (left, operation, right) = split
Command.Operation(
leftNode = NodeTitle(left),
rightNode = NodeTitle(right),
type = OperationType.from(operation)
)
}
nodeTitle to rightSide
}
.associate { it }
}
fun evalCommand(
command: Command,
nodeMap: Map<NodeTitle, Command>
): Long {
return when (command) {
is Command.Operation -> {
val left = nodeMap[command.leftNode]!!
val right = nodeMap[command.rightNode]!!
when (command.type) {
OperationType.PLUS -> evalCommand(left, nodeMap) + evalCommand(right, nodeMap)
OperationType.MINUS -> evalCommand(left, nodeMap) - evalCommand(right, nodeMap)
OperationType.MUL -> evalCommand(left, nodeMap) * evalCommand(right, nodeMap)
OperationType.DIV -> evalCommand(left, nodeMap) / evalCommand(right, nodeMap)
}
}
is Command.Value -> command.amount
}
}
fun buildExpression(
command: Command,
stringBuilder: StringBuilder,
map: Map<NodeTitle, Command>
) {
when (command) {
is Command.Operation -> {
val left = map[command.leftNode]!!
val right = map[command.rightNode]!!
stringBuilder.append("(")
buildExpression(left, stringBuilder, map)
when (command.type) {
OperationType.PLUS -> stringBuilder.append("+")
OperationType.MINUS -> stringBuilder.append("-")
OperationType.MUL -> stringBuilder.append("*")
OperationType.DIV -> stringBuilder.append("/")
}
buildExpression(right, stringBuilder, map)
stringBuilder.append(")")
}
is Command.Value -> {
if (command.amount == 0L) {
stringBuilder.append("x")
} else {
stringBuilder.append(command.amount)
}
}
}
}
fun main() {
fun part1(input: List<String>): Long {
val root = NodeTitle("root")
val map = parseInput(input)
val rootNode = map[root]!!
return evalCommand(rootNode, map)
}
/**
* Lazy solution - just build string and put it into online solver
*/
fun part2(input: List<String>): String {
val root = NodeTitle("root")
val humn = NodeTitle("humn")
val map = parseInput(input)
.mapValues { (nodeTitle, command) ->
if (nodeTitle == humn) {
Command.Value(0)
} else {
command
}
}
val expressionString = StringBuilder().also { stringBuilder ->
val rootNode = map[root]!! as Command.Operation
/**
* Build left side of expression
*/
buildExpression(map[rootNode.leftNode]!!, stringBuilder, map)
stringBuilder.append("=")
/**
* Calculate and build right side of expression
*/
val rightResult = evalCommand(map[rootNode.rightNode]!!, map)
stringBuilder.append(rightResult.toString())
stringBuilder.appendLine()
}.toString()
/**
* Just Copy-paste result of expression and put in here:
* https://www.mathpapa.com/equation-solver/
*/
return expressionString
}
// test if implementation meets criteria from the description, like:
val testInput = readInput("Day21_test")
val part1Test = part1(testInput)
println(part1Test)
check(part1Test == 152L)
val input = readInput("Day21")
println(part1(input))
println(part2(input))
}