Add Input.findAll
This commit is contained in:
parent
3550f65914
commit
1d745a0855
@ -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
|
||||
|
||||
@ -1,11 +1,16 @@
|
||||
package be.vandewalleh.aoc.utils.input
|
||||
|
||||
import org.intellij.lang.annotations.Language
|
||||
|
||||
class Input(private val value: String) {
|
||||
val text get() = value
|
||||
|
||||
val lines get() = Mapper(value.lines())
|
||||
val csv get() = Mapper(value.split(','))
|
||||
|
||||
fun findAll(@Language("RegExp") re: String): List<List<String>> =
|
||||
re.toRegex().findAll(value).map { it.groupValues.drop(1) }.toList()
|
||||
|
||||
class Mapper(val value: List<String>) {
|
||||
val ints get() = value.map { it.toInt() }
|
||||
val longs get() = value.map { it.toLong() }
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user