Skip to content

Commit b4b6f48

Browse files
committed
Solved day19 part 2
1 parent 4dcf079 commit b4b6f48

File tree

3 files changed

+93
-3
lines changed

3 files changed

+93
-3
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44

55
// project meta data
66
group 'de.havox_design.aoc2023'
7-
version '0.18.6'
7+
version '0.18.7'
88

99
// Switch to gradle "all" distribution.
1010
wrapper {

day19/src/main/kotlin/de/havox_design/aoc2023/day19/Day19.kt

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package de.havox_design.aoc2023.day19
22

3+
import java.util.*
4+
35
class Day19(private var filename: String) {
46
private val ICON_ACCEPT = "A"
57
private val ICON_REJECT = "R"
@@ -28,8 +30,49 @@ class Day19(private var filename: String) {
2830
.sumOf { it.total() }
2931
}
3032

31-
fun solvePart2(): Long =
32-
167409079868000L
33+
fun solvePart2(): Long {
34+
val (rules) = readInput()
35+
val results = mutableListOf<RangeAssignment>()
36+
val queue = linkedListOf(RangeAssignment() to ICON_START)
37+
38+
while (queue.isNotEmpty()) {
39+
val (current, label) = queue.removeFirst()
40+
41+
when (label) {
42+
ICON_ACCEPT -> {
43+
results.add(current)
44+
continue
45+
}
46+
47+
ICON_REJECT -> continue
48+
}
49+
50+
val rule = rules[label]!!
51+
val result = rule
52+
.decisions
53+
.runningFold(Triple(current, current, label)) { (_, falseValue), condition ->
54+
falseValue
55+
.split(condition)
56+
.let { (a, b) -> Triple(a, b, condition.successLabel) }
57+
}
58+
.drop(1)
59+
result
60+
.map { it.first to it.third }
61+
.filter { it.first.isValid() }
62+
.toCollection(queue)
63+
when {
64+
result.last().second.isValid() -> {
65+
queue.add(result.last().second to rule.elseRule)
66+
}
67+
}
68+
}
69+
70+
return results
71+
.sumOf { it.sum() }
72+
}
73+
74+
private fun <T> linkedListOf(vararg elements: T) =
75+
elements.toCollection(LinkedList())
3376

3477
private fun readInput(): Pair<Map<String, SortRule>, List<Assignment>> {
3578
val input = getResourceAsText(filename)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package de.havox_design.aoc2023.day19
2+
3+
data class RangeAssignment(
4+
val xMin: Long = 1L,
5+
val xMax: Long = 4000L,
6+
val mMin: Long = 1L,
7+
val mMax: Long = 4000L,
8+
val aMin: Long = 1L,
9+
val aMax: Long = 4000L,
10+
val sMin: Long = 1L,
11+
val sMax: Long = 4000L
12+
) {
13+
fun split(condition: Condition): Pair<RangeAssignment, RangeAssignment> =
14+
when (condition.field) {
15+
'x' -> when (condition.lessThen) {
16+
true -> copy(xMax = condition.value - 1) to copy(xMin = condition.value)
17+
else -> copy(xMin = condition.value + 1) to copy(xMax = condition.value)
18+
}
19+
20+
'm' -> when (condition.lessThen) {
21+
true -> copy(mMax = condition.value - 1) to copy(mMin = condition.value)
22+
else -> copy(mMin = condition.value + 1) to copy(mMax = condition.value)
23+
}
24+
25+
'a' -> when (condition.lessThen) {
26+
true -> copy(aMax = condition.value - 1) to copy(aMin = condition.value)
27+
else -> copy(aMin = condition.value + 1) to copy(aMax = condition.value)
28+
}
29+
30+
's' -> when (condition.lessThen) {
31+
true -> copy(sMax = condition.value - 1) to copy(sMin = condition.value)
32+
else -> copy(sMin = condition.value + 1) to copy(sMax = condition.value)
33+
}
34+
35+
else -> error("Invalid state")
36+
}
37+
38+
39+
fun isValid() =
40+
xMin <= xMax
41+
&& mMin <= mMax
42+
&& aMin <= aMax
43+
&& sMin <= sMax
44+
45+
fun sum() =
46+
(xMax - xMin + 1) * (mMax - mMin + 1) * (aMax - aMin + 1) * (sMax - sMin + 1)
47+
}

0 commit comments

Comments
 (0)