Skip to content

Commit

Permalink
2023 - Day 19 - part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
fmmr committed Dec 19, 2023
1 parent ebc73ee commit 8b66ca8
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 8 deletions.
99 changes: 93 additions & 6 deletions src/main/kotlin/no/rodland/advent_2023/Day19.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,109 @@ import no.rodland.advent.Day
// template generated: 19/12/2023
// Fredrik Rødland 2023

class Day19(val input: List<String>) : Day<Long, Long, List<String>> {
class Day19(val input: List<String>) : Day<Int, Long, Pair<Map<String, Day19.Workflow>, List<Day19.Part>>> {

private val parsed = input.parse()

override fun partOne(): Long {
return 2
override fun partOne(): Int {
val (workflows, parts) = parsed
var wf: Workflow
return parts.map { part ->
wf = workflows["in"]!!
while (wf.id != "A" && wf.id != "R") {
val id = wf.filter(part)
wf = workflows[id]!!
}
wf to part.sum()
}.filter { it.first.id == "A" }.sumOf { it.second }
}


override fun partTwo(): Long {
return 2
}

override fun List<String>.parse(): List<String> {
return map { line ->
line
override fun List<String>.parse(): Pair<Map<String, Workflow>, List<Part>> {

val (workflowStrings, ratingStrings) = joinToString("\n").split("\n\n").map { it.split("\n") }
val workflows = workflowStrings.map {
val id = it.substringBefore("{")
val rules = it.substringAfter("{").substringBefore("}").split(",")
.map { rule -> Rule.fromString(rule) }
Workflow(id, rules)
}
val parts = ratingStrings.map {
val x = it.substringAfter("x=").substringBefore(",")
val m = it.substringAfter("m=").substringBefore(",")
val a = it.substringAfter("a=").substringBefore(",")
val s = it.substringAfter("s=").substringBefore("}")
Part(x.toInt(), m.toInt(), a.toInt(), s.toInt())
}
return (workflows + acceptWF + rejectWF).associateBy { it.id } to parts
}

data class Workflow(val id: String, val filters: List<Rule>) {
fun filter(part: Part) = filters.first { it.filter(part) }.destination
}

class CheckRule(private val valToCheck: Char, private val greaterThan: Boolean, private val intToCheck: Int, dest: String) : Rule(dest) {
override fun filter(part: Part) = when (greaterThan) {
true -> part.value(valToCheck) > intToCheck
false -> part.value(valToCheck) < intToCheck
}
}

sealed class Rule(open val destination: String) {
abstract fun filter(part: Part): Boolean

companion object {
fun fromString(string: String): Rule {
return when {
string.contains(">") -> {
val x = string.substringBefore(">")
val i = string.substringAfter(">").substringBefore(":")
val dest = string.substringAfter(":")
CheckRule(x.first(), true, i.toInt(), dest)
}

string.contains("<") -> {
val x = string.substringBefore("<")
val i = string.substringAfter("<").substringBefore(":")
val dest = string.substringAfter(":")
CheckRule(x.first(), false, i.toInt(), dest)
}

string == "A" -> acceptRule
string == "R" -> rejectRule
else -> NoCheckRule(string)
}
}
}
}


open class NoCheckRule(dest: String) : Rule(dest) {
override fun filter(part: Part) = true
}

data class Part(val x: Int, val m: Int, val a: Int, val s: Int) {
fun value(char: Char) = when (char) {
'x' -> x
'm' -> m
'a' -> a
's' -> s
else -> error("unknown char: $char")
}

fun sum() = x + m + a + s
}

companion object {
val acceptRule = NoCheckRule("A")
val rejectRule = NoCheckRule("R")
val acceptWF = Workflow("A", listOf(acceptRule))
val rejectWF = Workflow("R", listOf(rejectRule))

}

override val day = "19".toInt()
Expand Down
4 changes: 2 additions & 2 deletions src/test/kotlin/no/rodland/advent_2023/Day19Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ internal class Day19Test {
private val data19 = "2023/input_19.txt".readFile()
private val test19 = "2023/input_19_test.txt".readFile()

private val resultTestOne = 2L
private val resultTestOne = 19114
private val resultTestTwo = 2L
private val resultOne = 2L
private val resultOne = 432434
private val resultTwo = 2L

val test = defaultTestSuiteParseOnInit(
Expand Down

0 comments on commit 8b66ca8

Please sign in to comment.