Day07 + optimizations
This commit is contained in:
@@ -11,8 +11,8 @@ typealias Entries = List<Entry>
|
||||
@Day(4)
|
||||
class Day04(@Text val input: Input<String>) {
|
||||
|
||||
val entries: List<Entries> = input.value.trim().split("\n\n").map {
|
||||
it.replace("\n", " ").split(" ").map { it.split(":").let { (k, v) -> k.trim() to v.trim() } }
|
||||
val entries = input.value.split("\n\n").map {
|
||||
it.split(" ", "\n").map { it.split(":").let { (k, v) -> k to v } }
|
||||
}
|
||||
|
||||
private fun Entries.hasRequiredKeys() = map { it.first }
|
||||
@@ -20,10 +20,10 @@ class Day04(@Text val input: Input<String>) {
|
||||
|
||||
fun part1() = entries.count { it.hasRequiredKeys() }
|
||||
|
||||
private val hclRegex = "^#[0-9a-f]{6}$".toRegex()
|
||||
private val pidRegex = "^[0-9]{9}$".toRegex()
|
||||
private val hclRegex = Regex("#[0-9a-f]{6}")
|
||||
private val pidRegex = Regex("[0-9]{9}")
|
||||
|
||||
private fun Entry.hasValidValues() = let { (k, v) ->
|
||||
private fun Entry.isValid() = let { (k, v) ->
|
||||
when (k) {
|
||||
"byr" -> v.toInt() in 1920..2002
|
||||
"iyr" -> v.toInt() in 2010..2020
|
||||
@@ -40,7 +40,7 @@ class Day04(@Text val input: Input<String>) {
|
||||
}
|
||||
}
|
||||
|
||||
fun part2() = entries.count { it.hasRequiredKeys() && it.all { it.hasValidValues() } }
|
||||
fun part2() = entries.count { it.hasRequiredKeys() && it.all { it.isValid() } }
|
||||
}
|
||||
|
||||
fun main() = with(createDay<Day04>()) {
|
||||
|
||||
@@ -11,18 +11,16 @@ class Day06(@Text val input: Input<String>) {
|
||||
|
||||
private val groups = input.value.split("\n\n")
|
||||
|
||||
fun part1(): Int = groups.map { it.replace("\\s+".toRegex(), "") }
|
||||
.map { it.toCharArray().toSet() }
|
||||
.map { it.size }.sum()
|
||||
fun part1() = groups.sumBy { it.replace("\n", "").toCharArray().toSet().size }
|
||||
|
||||
fun part2(): Int = groups.map {
|
||||
val group = it.split("\n").map { it.trim() }
|
||||
fun part2() = groups.sumBy {
|
||||
val group = it.split("\n")
|
||||
val bag = CharBags.mutable.empty()
|
||||
|
||||
group.forEach { chars: String -> chars.forEach { bag.add(it) } }
|
||||
bag.selectByOccurrences { group.size == it }
|
||||
.selectUnique()
|
||||
.size()
|
||||
}.sum()
|
||||
|
||||
bag.selectByOccurrences { group.size == it }.toSet().size()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package be.vandewalleh.aoc.days
|
||||
|
||||
import be.vandewalleh.aoc.utils.input.Day
|
||||
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.factory.Stacks
|
||||
import org.eclipse.collections.api.multimap.list.ImmutableListMultimap
|
||||
import org.eclipse.collections.api.stack.MutableStack
|
||||
import org.eclipse.collections.impl.factory.Multimaps
|
||||
|
||||
data class Bag(val count: Int, val color: String)
|
||||
|
||||
@Day(7)
|
||||
class Day07(@Lines val input: Input<List<String>>) {
|
||||
|
||||
private val map: ImmutableListMultimap<String, Bag>
|
||||
|
||||
init {
|
||||
val mutableMap = Multimaps.mutable.list.empty<String, Bag>()
|
||||
|
||||
val colorRegex = "^(\\w+ \\w+)".toRegex()
|
||||
val requirementRegex = "(\\d+) (\\w+ \\w+) bag".toRegex()
|
||||
|
||||
for (line in input.value) {
|
||||
val outerColor = colorRegex.find(line)!!.groupValues[1]
|
||||
for (match in requirementRegex.findAll(line)) {
|
||||
val (_, count, color) = match.groupValues
|
||||
mutableMap.put(outerColor, Bag(count.toInt(), color))
|
||||
}
|
||||
}
|
||||
|
||||
map = mutableMap.toImmutable()
|
||||
}
|
||||
|
||||
private fun bagSequence(rootColor: String): Sequence<Bag> = sequence {
|
||||
val stack: MutableStack<Bag> = Stacks.mutable.ofAll(map.get(rootColor))
|
||||
while (stack.notEmpty()) {
|
||||
val current = stack.pop().also { yield(it) }
|
||||
map[current.color]?.let {
|
||||
it.forEach { (count, color) ->
|
||||
stack.push(Bag(current.count * count, color))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun part1() = map.keySet().count { bagSequence(it).any { it.color == "shiny gold" } }
|
||||
|
||||
fun part2() = bagSequence("shiny gold").sumBy { it.count }
|
||||
|
||||
}
|
||||
|
||||
fun main() = with(createDay<Day07>()) {
|
||||
println(part1())
|
||||
println(part2())
|
||||
}
|
||||
Reference in New Issue
Block a user