Skip to content

Commit

Permalink
2023, Day 5 - refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jurisk committed Dec 5, 2023
1 parent fe00f1b commit 384aac0
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions scala2/src/main/scala/jurisk/adventofcode/y2023/Advent05.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,15 @@ object Advent05 extends IOApp.Simple {
def potentialIntervals(
intervalSet: NonOverlappingDiscreteIntervalSet[Long]
): NonOverlappingDiscreteIntervalSet[Long] = {
var convertedAll = NonOverlappingDiscreteIntervalSet.empty[Long]
var coveredAll = NonOverlappingDiscreteIntervalSet.empty[Long]

converters.foreach { converter =>
val conversionResult = converter.potentialIntervals(intervalSet)

convertedAll = convertedAll union conversionResult.converted
coveredAll = coveredAll union conversionResult.covered
val results = converters.foldLeft(ConversionResult.empty) {
case (acc, converter) =>
acc union converter.potentialIntervals(intervalSet)
}

val straightThrough = intervalSet subtract coveredAll
straightThrough union convertedAll
// The converters did not pick up some intervals, they stay as they were
val straightThrough = intervalSet subtract results.covered

straightThrough union results.converted
}
}

Expand Down Expand Up @@ -72,8 +69,6 @@ object Advent05 extends IOApp.Simple {
covered: NonOverlappingDiscreteIntervalSet[Long],
converted: NonOverlappingDiscreteIntervalSet[Long],
) {
assert(covered.size == converted.size)

def union(other: ConversionResult): ConversionResult =
ConversionResult(
uncovered = uncovered union other.uncovered,
Expand Down

0 comments on commit 384aac0

Please sign in to comment.