-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay14.kt
35 lines (30 loc) · 1.18 KB
/
Day14.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
package me.markrobbo
class Day14 {
data class PortComputer(var bitmask: String = "X".repeat(36), val memory: MutableMap<Int, String> = mutableMapOf()) {
fun setValue(memoryLocation: Int, value: Long) {
memory[memoryLocation] = value.toString(2).padStart(36, '0')
.zip(bitmask)
.map { (valueChar, bitmaskChar) ->
if (bitmaskChar == 'X') {
valueChar
} else {
bitmaskChar
}
}.joinToString("")
}
fun sumAllInMemory() = memory.values.sumOf { it.toLong(2) }
}
fun solvePart1(input: List<String>): Long {
val computer = PortComputer()
input.map {
"""(\w+)(?:\[(\d+)])? = (.*)""".toRegex().matchEntire(it)?.destructured?.let { (command, arrayIndex, value) ->
when (command) {
"mask" -> computer.bitmask = value
"mem" -> computer.setValue(arrayIndex.toInt(), value.toLong())
else -> IllegalArgumentException()
}
}
}
return computer.sumAllInMemory()
}
}