From b9a0c574e952270e3116f724666dc993008f358a Mon Sep 17 00:00:00 2001 From: Hubert Van De Walle Date: Wed, 16 Dec 2020 09:44:23 +0100 Subject: [PATCH] Clean --- days/src/main/kotlin/Day16.kt | 52 ++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/days/src/main/kotlin/Day16.kt b/days/src/main/kotlin/Day16.kt index dcc1414..fd7ea7e 100644 --- a/days/src/main/kotlin/Day16.kt +++ b/days/src/main/kotlin/Day16.kt @@ -4,7 +4,6 @@ import be.vandewalleh.aoc.utils.input.Day import be.vandewalleh.aoc.utils.input.Groups import be.vandewalleh.aoc.utils.input.Input import be.vandewalleh.aoc.utils.input.createDay -import org.eclipse.collections.api.RichIterable import org.eclipse.collections.impl.factory.Multimaps import org.eclipse.collections.impl.factory.primitive.IntSets @@ -15,15 +14,19 @@ class Day16(@Groups val input: Input>>) { private val myTicket = input.value[1][1] private val nearbyTicketsGroup = input.value[2].drop(1) + private val rangeRe = "(\\d+)-(\\d+)".toRegex() + + private fun extractRanges(line: String): Sequence = rangeRe.findAll(line) + .map { + val (min, max) = it.destructured + min.toInt()..max.toInt() + } + fun part1(): Int { val minMax = mutableListOf() - val re = "(\\d+)-(\\d+)".toRegex() for (line in rangesGroup) { - re.findAll(line).forEach { - val (min, max) = it.destructured - minMax.add(min.toInt()..max.toInt()) - } + minMax.addAll(extractRanges(line)) } val nearbyTickets = mutableListOf() @@ -51,12 +54,10 @@ class Day16(@Groups val input: Input>>) { fun part2(): Long { val minMax = Multimaps.mutable.list.empty() - val re = "(\\d+)-(\\d+)".toRegex() for (line in rangesGroup) { val name = line.substringBefore(":") - re.findAll(line).forEach { - val (min, max) = it.destructured - minMax.put(name, min.toInt()..max.toInt()) + extractRanges(line).forEach { + minMax.put(name, it) } } @@ -68,46 +69,47 @@ class Day16(@Groups val input: Input>>) { } } - val catMap = Multimaps.mutable.list.empty() + val indexesByCategory = Multimaps.mutable.list.empty() - for (a in minMax.keyMultiValuePairsView()) { - val category = a.one - val ranges = a.two.toList() + for (entry in minMax.keyMultiValuePairsView()) { + val category = entry.one + val ranges = entry.two.toList() for (i in validTickets.first().indices) { val allInRange = validTickets .asSequence() .map { it[i] } .all { inRanges(it, ranges) } - if (allInRange) catMap.put(category, i) + if (allInRange) indexesByCategory.put(category, i) } } val removed = IntSets.mutable.empty() - while (!catMap.multiValuesView().all { it.size() == 1 }) { - val values = catMap.multiValuesView() + while (!indexesByCategory.multiValuesView().all { it.size() == 1 }) { + val values = indexesByCategory.multiValuesView() val categoriesToRemove = mutableListOf() val one = values.filter { it.size() == 1 } .map { it.first() } .find { !removed.contains(it) } ?.also { removed.add(it) } - ?: error("???") + ?: error("No values left to remove") - for (v in catMap.keyMultiValuePairsView()) { - val cat = v.one - val values: RichIterable = v.two + for (entry in indexesByCategory.keyMultiValuePairsView()) { + val values = entry.two if (values.size() < 2) continue - if (values.find { it == one } != null) categoriesToRemove.add(cat) + val category = entry.one + if (values.find { it == one } != null) categoriesToRemove.add(category) } - for (cat in categoriesToRemove) { - catMap.remove(cat, one) + for (category in categoriesToRemove) { + indexesByCategory.remove(category, one) } } val myTicketValues = myTicket.split(",").map { it.toInt() } var mult = 1L - catMap.forEachKeyValue { category, index -> + + indexesByCategory.forEachKeyValue { category, index -> if (category.startsWith("departure")) { mult *= myTicketValues[index] }