Clean
This commit is contained in:
parent
edc60130dc
commit
b9a0c574e9
@ -4,7 +4,6 @@ import be.vandewalleh.aoc.utils.input.Day
|
|||||||
import be.vandewalleh.aoc.utils.input.Groups
|
import be.vandewalleh.aoc.utils.input.Groups
|
||||||
import be.vandewalleh.aoc.utils.input.Input
|
import be.vandewalleh.aoc.utils.input.Input
|
||||||
import be.vandewalleh.aoc.utils.input.createDay
|
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.Multimaps
|
||||||
import org.eclipse.collections.impl.factory.primitive.IntSets
|
import org.eclipse.collections.impl.factory.primitive.IntSets
|
||||||
|
|
||||||
@ -15,15 +14,19 @@ class Day16(@Groups val input: Input<List<List<String>>>) {
|
|||||||
private val myTicket = input.value[1][1]
|
private val myTicket = input.value[1][1]
|
||||||
private val nearbyTicketsGroup = input.value[2].drop(1)
|
private val nearbyTicketsGroup = input.value[2].drop(1)
|
||||||
|
|
||||||
|
private val rangeRe = "(\\d+)-(\\d+)".toRegex()
|
||||||
|
|
||||||
|
private fun extractRanges(line: String): Sequence<IntRange> = rangeRe.findAll(line)
|
||||||
|
.map {
|
||||||
|
val (min, max) = it.destructured
|
||||||
|
min.toInt()..max.toInt()
|
||||||
|
}
|
||||||
|
|
||||||
fun part1(): Int {
|
fun part1(): Int {
|
||||||
val minMax = mutableListOf<IntRange>()
|
val minMax = mutableListOf<IntRange>()
|
||||||
|
|
||||||
val re = "(\\d+)-(\\d+)".toRegex()
|
|
||||||
for (line in rangesGroup) {
|
for (line in rangesGroup) {
|
||||||
re.findAll(line).forEach {
|
minMax.addAll(extractRanges(line))
|
||||||
val (min, max) = it.destructured
|
|
||||||
minMax.add(min.toInt()..max.toInt())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
val nearbyTickets = mutableListOf<Int>()
|
val nearbyTickets = mutableListOf<Int>()
|
||||||
@ -51,12 +54,10 @@ class Day16(@Groups val input: Input<List<List<String>>>) {
|
|||||||
fun part2(): Long {
|
fun part2(): Long {
|
||||||
val minMax = Multimaps.mutable.list.empty<String, IntRange>()
|
val minMax = Multimaps.mutable.list.empty<String, IntRange>()
|
||||||
|
|
||||||
val re = "(\\d+)-(\\d+)".toRegex()
|
|
||||||
for (line in rangesGroup) {
|
for (line in rangesGroup) {
|
||||||
val name = line.substringBefore(":")
|
val name = line.substringBefore(":")
|
||||||
re.findAll(line).forEach {
|
extractRanges(line).forEach {
|
||||||
val (min, max) = it.destructured
|
minMax.put(name, it)
|
||||||
minMax.put(name, min.toInt()..max.toInt())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,46 +69,47 @@ class Day16(@Groups val input: Input<List<List<String>>>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val catMap = Multimaps.mutable.list.empty<String, Int>()
|
val indexesByCategory = Multimaps.mutable.list.empty<String, Int>()
|
||||||
|
|
||||||
for (a in minMax.keyMultiValuePairsView()) {
|
for (entry in minMax.keyMultiValuePairsView()) {
|
||||||
val category = a.one
|
val category = entry.one
|
||||||
val ranges = a.two.toList()
|
val ranges = entry.two.toList()
|
||||||
|
|
||||||
for (i in validTickets.first().indices) {
|
for (i in validTickets.first().indices) {
|
||||||
val allInRange = validTickets
|
val allInRange = validTickets
|
||||||
.asSequence()
|
.asSequence()
|
||||||
.map { it[i] }
|
.map { it[i] }
|
||||||
.all { inRanges(it, ranges) }
|
.all { inRanges(it, ranges) }
|
||||||
if (allInRange) catMap.put(category, i)
|
if (allInRange) indexesByCategory.put(category, i)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val removed = IntSets.mutable.empty()
|
val removed = IntSets.mutable.empty()
|
||||||
while (!catMap.multiValuesView().all { it.size() == 1 }) {
|
while (!indexesByCategory.multiValuesView().all { it.size() == 1 }) {
|
||||||
val values = catMap.multiValuesView()
|
val values = indexesByCategory.multiValuesView()
|
||||||
val categoriesToRemove = mutableListOf<String>()
|
val categoriesToRemove = mutableListOf<String>()
|
||||||
|
|
||||||
val one = values.filter { it.size() == 1 }
|
val one = values.filter { it.size() == 1 }
|
||||||
.map { it.first() }
|
.map { it.first() }
|
||||||
.find { !removed.contains(it) }
|
.find { !removed.contains(it) }
|
||||||
?.also { removed.add(it) }
|
?.also { removed.add(it) }
|
||||||
?: error("???")
|
?: error("No values left to remove")
|
||||||
|
|
||||||
for (v in catMap.keyMultiValuePairsView()) {
|
for (entry in indexesByCategory.keyMultiValuePairsView()) {
|
||||||
val cat = v.one
|
val values = entry.two
|
||||||
val values: RichIterable<Int> = v.two
|
|
||||||
if (values.size() < 2) continue
|
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) {
|
for (category in categoriesToRemove) {
|
||||||
catMap.remove(cat, one)
|
indexesByCategory.remove(category, one)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
val myTicketValues = myTicket.split(",").map { it.toInt() }
|
val myTicketValues = myTicket.split(",").map { it.toInt() }
|
||||||
var mult = 1L
|
var mult = 1L
|
||||||
catMap.forEachKeyValue { category, index ->
|
|
||||||
|
indexesByCategory.forEachKeyValue { category, index ->
|
||||||
if (category.startsWith("departure")) {
|
if (category.startsWith("departure")) {
|
||||||
mult *= myTicketValues[index]
|
mult *= myTicketValues[index]
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user