diff --git a/days/src/main/kotlin/Day16.kt b/days/src/main/kotlin/Day16.kt index 9d96493..dcc1414 100644 --- a/days/src/main/kotlin/Day16.kt +++ b/days/src/main/kotlin/Day16.kt @@ -1,36 +1,33 @@ package be.vandewalleh.aoc.days 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.Lines 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 @Day(16) -class Day16(@Lines val input: Input>) { +class Day16(@Groups val input: Input>>) { + + private val rangesGroup = input.value[0] + private val myTicket = input.value[1][1] + private val nearbyTicketsGroup = input.value[2].drop(1) fun part1(): Int { val minMax = mutableListOf() val re = "(\\d+)-(\\d+)".toRegex() - for (line in input.value) { + for (line in rangesGroup) { re.findAll(line).forEach { val (min, max) = it.destructured minMax.add(min.toInt()..max.toInt()) } - if (line.startsWith("your ticket")) break } - var ok = false val nearbyTickets = mutableListOf() - for (line in input.value) { - if (!ok && line.startsWith("nearby tickets:")) { - ok = true - continue - } - if (!ok) continue + for (line in nearbyTicketsGroup) { line.splitToSequence(",").forEach { nearbyTickets.add(it.toInt()) } } @@ -55,24 +52,16 @@ class Day16(@Lines val input: Input>) { val minMax = Multimaps.mutable.list.empty() val re = "(\\d+)-(\\d+)".toRegex() - for (line in input.value) { - if (line.startsWith("your ticket")) break - val name = line.split(':')[0] - re.findAll(line)!!.forEach { + for (line in rangesGroup) { + val name = line.substringBefore(":") + re.findAll(line).forEach { val (min, max) = it.destructured minMax.put(name, min.toInt()..max.toInt()) } } - var ok = false val validTickets = mutableListOf>() - - for (line in input.value) { - if (!ok && line.startsWith("nearby tickets:")) { - ok = true - continue - } - if (!ok) continue + for (line in nearbyTicketsGroup) { val ticket = line.splitToSequence(",").map { it.toInt() }.toList() if (isTicketValid(ticket, minMax.valuesView().toList())) { validTickets.add(ticket) @@ -116,10 +105,6 @@ class Day16(@Lines val input: Input>) { catMap.remove(cat, one) } } - - // TODO - val myTicket = "173,191,61,199,101,179,257,79,193,223,139,97,83,197,251,53,89,149,181,59" - val myTicketValues = myTicket.split(",").map { it.toInt() } var mult = 1L catMap.forEachKeyValue { category, index -> diff --git a/utils/src/main/kotlin/be/vandewalleh/aoc/utils/input/Annotations.kt b/utils/src/main/kotlin/be/vandewalleh/aoc/utils/input/Annotations.kt index 807d5fc..7d85617 100644 --- a/utils/src/main/kotlin/be/vandewalleh/aoc/utils/input/Annotations.kt +++ b/utils/src/main/kotlin/be/vandewalleh/aoc/utils/input/Annotations.kt @@ -24,3 +24,6 @@ annotation class Text @DayInput annotation class Lines + +@DayInput +annotation class Groups diff --git a/utils/src/main/kotlin/be/vandewalleh/aoc/utils/input/InputFactory.kt b/utils/src/main/kotlin/be/vandewalleh/aoc/utils/input/InputFactory.kt index 3ff983a..45cb616 100644 --- a/utils/src/main/kotlin/be/vandewalleh/aoc/utils/input/InputFactory.kt +++ b/utils/src/main/kotlin/be/vandewalleh/aoc/utils/input/InputFactory.kt @@ -60,6 +60,10 @@ class InputFactory(private val resourceLoader: ResourceLoader) { fun text(injectionPoint: InjectionPoint<*>): Input = injectionPoint.read().wrap() + @Groups + fun groups(injectionPoint: InjectionPoint<*>): Input>> = + injectionPoint.read().split("\n\n").map { it.lines() }.wrap() + private fun T.wrap() = Input(this)