Clean parsing
This commit is contained in:
parent
90c9961d72
commit
edc60130dc
@ -1,36 +1,33 @@
|
|||||||
package be.vandewalleh.aoc.days
|
package be.vandewalleh.aoc.days
|
||||||
|
|
||||||
import be.vandewalleh.aoc.utils.input.Day
|
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.Input
|
||||||
import be.vandewalleh.aoc.utils.input.Lines
|
|
||||||
import be.vandewalleh.aoc.utils.input.createDay
|
import be.vandewalleh.aoc.utils.input.createDay
|
||||||
import org.eclipse.collections.api.RichIterable
|
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
|
||||||
|
|
||||||
@Day(16)
|
@Day(16)
|
||||||
class Day16(@Lines val input: Input<List<String>>) {
|
class Day16(@Groups val input: Input<List<List<String>>>) {
|
||||||
|
|
||||||
|
private val rangesGroup = input.value[0]
|
||||||
|
private val myTicket = input.value[1][1]
|
||||||
|
private val nearbyTicketsGroup = input.value[2].drop(1)
|
||||||
|
|
||||||
fun part1(): Int {
|
fun part1(): Int {
|
||||||
val minMax = mutableListOf<IntRange>()
|
val minMax = mutableListOf<IntRange>()
|
||||||
|
|
||||||
val re = "(\\d+)-(\\d+)".toRegex()
|
val re = "(\\d+)-(\\d+)".toRegex()
|
||||||
for (line in input.value) {
|
for (line in rangesGroup) {
|
||||||
re.findAll(line).forEach {
|
re.findAll(line).forEach {
|
||||||
val (min, max) = it.destructured
|
val (min, max) = it.destructured
|
||||||
minMax.add(min.toInt()..max.toInt())
|
minMax.add(min.toInt()..max.toInt())
|
||||||
}
|
}
|
||||||
if (line.startsWith("your ticket")) break
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var ok = false
|
|
||||||
val nearbyTickets = mutableListOf<Int>()
|
val nearbyTickets = mutableListOf<Int>()
|
||||||
for (line in input.value) {
|
for (line in nearbyTicketsGroup) {
|
||||||
if (!ok && line.startsWith("nearby tickets:")) {
|
|
||||||
ok = true
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if (!ok) continue
|
|
||||||
line.splitToSequence(",").forEach { nearbyTickets.add(it.toInt()) }
|
line.splitToSequence(",").forEach { nearbyTickets.add(it.toInt()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,24 +52,16 @@ class Day16(@Lines val input: Input<List<String>>) {
|
|||||||
val minMax = Multimaps.mutable.list.empty<String, IntRange>()
|
val minMax = Multimaps.mutable.list.empty<String, IntRange>()
|
||||||
|
|
||||||
val re = "(\\d+)-(\\d+)".toRegex()
|
val re = "(\\d+)-(\\d+)".toRegex()
|
||||||
for (line in input.value) {
|
for (line in rangesGroup) {
|
||||||
if (line.startsWith("your ticket")) break
|
val name = line.substringBefore(":")
|
||||||
val name = line.split(':')[0]
|
re.findAll(line).forEach {
|
||||||
re.findAll(line)!!.forEach {
|
|
||||||
val (min, max) = it.destructured
|
val (min, max) = it.destructured
|
||||||
minMax.put(name, min.toInt()..max.toInt())
|
minMax.put(name, min.toInt()..max.toInt())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var ok = false
|
|
||||||
val validTickets = mutableListOf<List<Int>>()
|
val validTickets = mutableListOf<List<Int>>()
|
||||||
|
for (line in nearbyTicketsGroup) {
|
||||||
for (line in input.value) {
|
|
||||||
if (!ok && line.startsWith("nearby tickets:")) {
|
|
||||||
ok = true
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if (!ok) continue
|
|
||||||
val ticket = line.splitToSequence(",").map { it.toInt() }.toList()
|
val ticket = line.splitToSequence(",").map { it.toInt() }.toList()
|
||||||
if (isTicketValid(ticket, minMax.valuesView().toList())) {
|
if (isTicketValid(ticket, minMax.valuesView().toList())) {
|
||||||
validTickets.add(ticket)
|
validTickets.add(ticket)
|
||||||
@ -116,10 +105,6 @@ class Day16(@Lines val input: Input<List<String>>) {
|
|||||||
catMap.remove(cat, one)
|
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() }
|
val myTicketValues = myTicket.split(",").map { it.toInt() }
|
||||||
var mult = 1L
|
var mult = 1L
|
||||||
catMap.forEachKeyValue { category, index ->
|
catMap.forEachKeyValue { category, index ->
|
||||||
|
|||||||
@ -24,3 +24,6 @@ annotation class Text
|
|||||||
|
|
||||||
@DayInput
|
@DayInput
|
||||||
annotation class Lines
|
annotation class Lines
|
||||||
|
|
||||||
|
@DayInput
|
||||||
|
annotation class Groups
|
||||||
|
|||||||
@ -60,6 +60,10 @@ class InputFactory(private val resourceLoader: ResourceLoader) {
|
|||||||
fun text(injectionPoint: InjectionPoint<*>): Input<String> =
|
fun text(injectionPoint: InjectionPoint<*>): Input<String> =
|
||||||
injectionPoint.read().wrap()
|
injectionPoint.read().wrap()
|
||||||
|
|
||||||
|
@Groups
|
||||||
|
fun groups(injectionPoint: InjectionPoint<*>): Input<List<List<String>>> =
|
||||||
|
injectionPoint.read().split("\n\n").map { it.lines() }.wrap()
|
||||||
|
|
||||||
|
|
||||||
private fun <T> T.wrap() = Input(this)
|
private fun <T> T.wrap() = Input(this)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user