Skip to content

Commit f0ecf89

Browse files
committed
code refactoring
1 parent 497ccea commit f0ecf89

File tree

1 file changed

+9
-11
lines changed
  • src/main/kotlin/tr/emreone/adventofcode/days

1 file changed

+9
-11
lines changed

src/main/kotlin/tr/emreone/adventofcode/days/Day19.kt

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ fun PotentialPart.patch(category: Category, newRange: IntRange?): PotentialPart?
2121
}
2222
}
2323

24+
fun PotentialPart.combinations(): Long {
25+
return this.values.fold(1) { acc, i ->
26+
acc * (i.last - i.first + 1L)
27+
}
28+
}
29+
2430
class Day19 : Day(19, 2023, "Aplenty") {
2531

2632
sealed interface Rule {
@@ -128,12 +134,7 @@ class Day19 : Day(19, 2023, "Aplenty") {
128134
}
129135

130136
override fun part2(): Long {
131-
val potentialParts = mapOf(
132-
Category.X to 1..4000,
133-
Category.M to 1..4000,
134-
Category.A to 1..4000,
135-
Category.S to 1..4000,
136-
)
137+
val potentialParts = Category.entries.associateWith { 1..4000 }
137138

138139
fun countAccepted(wf: Workflow, parts: PotentialPart?): Long =
139140
wf.rules.fold(parts to 0L) { (remaining, count), rule ->
@@ -144,15 +145,12 @@ class Day19 : Day(19, 2023, "Aplenty") {
144145
"R" -> 0
145146
else -> countAccepted(workflows[rule.next]!!, matching)
146147
}
147-
} else
148+
} else {
148149
null to count
150+
}
149151
}.second
150152

151153
return countAccepted(workflows["in"]!!, potentialParts)
152154
}
153155

154-
private fun PotentialPart.combinations(): Long =
155-
this.values.fold(1) { acc, i ->
156-
acc * (i.last - i.first + 1L)
157-
}
158156
}

0 commit comments

Comments
 (0)