Add Input.findAll
This commit is contained in:
@@ -8,16 +8,13 @@ class Day14 : BaseDay() {
|
||||
|
||||
private fun run(step: Int): Long {
|
||||
val template = input.lines.value.first()
|
||||
// {CH=(CB, BH), NN=(NC, CN), ...}
|
||||
val pairs = input.lines.value.drop(2)
|
||||
.associate { it.split(" -> ").let { (a, b) -> a to (a[0] + b to b + a[1]) } }
|
||||
|
||||
val pairs = input.findAll("(\\S+) -> (\\S)").associate { it[0] to it[1] }
|
||||
val polymer = Counter(template.windowed(2)) // {NN=1, NC=1, CB=1}
|
||||
val counts = Counter(template.toCharArray().toList()) // {N=2, C=1, ...}
|
||||
|
||||
repeat(step) {
|
||||
polymer.forEach { (pair, value) ->
|
||||
val (left, right) = pairs[pair]!!
|
||||
val (left, right) = pairs[pair]!!.let { m -> pair[0] + m to m + pair[1] }
|
||||
polymer[pair] -= value
|
||||
polymer[left] += value
|
||||
polymer[right] += value
|
||||
|
||||
Reference in New Issue
Block a user