Minor update to Day 15 because I should have used a linked hashmap in the first place, if I had only read the instructions clearly enough.
This commit is contained in:
parent
6eb93c51a3
commit
3a8a2d79a9
@ -73,27 +73,20 @@ rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun part2(input: List<String>): Int {
|
fun part2(input: List<String>): Int {
|
||||||
val boxes = Array<MutableList<String>>(256) { ArrayList() }
|
val boxes = Array<MutableMap<String, Int>>(256) { LinkedHashMap() }
|
||||||
println(hash("cm"))
|
|
||||||
input.first().split(",").forEach { it ->
|
input.first().split(",").forEach { it ->
|
||||||
if (it.contains('=')) {
|
if (it.contains('=')) {
|
||||||
val (label, value) = it.split("=")
|
val (label, value) = it.split("=")
|
||||||
val hash = hash(label)
|
boxes[hash(label)][label] = value.toInt()
|
||||||
if (boxes[hash].none { it.startsWith("$label ") }) {
|
|
||||||
boxes[hash].add("$label $value")
|
|
||||||
} else {
|
|
||||||
boxes[hash] = boxes[hash].map { content -> if (content.startsWith("$label ")) "$label $value" else content }.toMutableList()
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
val label = it.removeSuffix("-")
|
val label = it.removeSuffix("-")
|
||||||
val hash = hash(label)
|
boxes[hash(label)].remove(label)
|
||||||
boxes[hash] = boxes[hash].filterNot { it.startsWith("$label ") }.toMutableList()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
val vals = boxes.mapIndexed { boxno, contents ->
|
return boxes.mapIndexed { boxno, contents ->
|
||||||
contents.mapIndexed { index, strings -> (boxno + 1) * strings.split(" ")[1].toInt() * (index + 1) }.sum()
|
contents.map { entry -> entry.value }
|
||||||
}
|
.mapIndexed { index, value -> (boxno + 1) * value * (index + 1) }.sum()
|
||||||
return vals.sum()
|
}.sum()
|
||||||
}
|
}
|
||||||
|
|
||||||
// test if implementation meets criteria from the description, like:
|
// test if implementation meets criteria from the description, like:
|
||||||
|
Loading…
Reference in New Issue
Block a user