Skip to content

Commit

Permalink
day 5 in a more constant but still inefficient way
Browse files Browse the repository at this point in the history
  • Loading branch information
norganos committed Dec 5, 2023
1 parent 28f2359 commit 0e1e8d3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
47 changes: 36 additions & 11 deletions src/main/kotlin/de/linkel/aoc/Day05.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,40 @@ class Day05: AbstractLinesAdventDay<Long>() {
val temperatureToHumidity = consumeUntilDelimiter(iterator, "humidity-to-location map:")
val humidityToLocation = consumeUntilDelimiter(iterator, "EOF")

return seeds.minOf { seedRange ->
seedRange.minOf { seed ->
humidityToLocation.lookup(
temperatureToHumidity.lookup(
lightToTemperature.lookup(
waterToLight.lookup(
fertilizerToWater.lookup(
soilToFertilizer.lookup(
seedToSoil.lookup(seed)
)
return LongRange(0, Long.MAX_VALUE)
.first { loc ->
val seed = seedToSoil.reverseLookup(
soilToFertilizer.reverseLookup(
fertilizerToWater.reverseLookup(
waterToLight.reverseLookup(
lightToTemperature.reverseLookup(
temperatureToHumidity.reverseLookup(
humidityToLocation.reverseLookup(loc)
)
)
)
)
)
}
)
seeds.any { seed in it }
}
// return seeds.minOf { seedRange ->
// seedRange.minOf { seed ->
// humidityToLocation.lookup(
// temperatureToHumidity.lookup(
// lightToTemperature.lookup(
// waterToLight.lookup(
// fertilizerToWater.lookup(
// soilToFertilizer.lookup(
// seedToSoil.lookup(seed)
// )
// )
// )
// )
// )
// )
// }
// }
}

private fun consumeUntilDelimiter(iterator: Iterator<String>, delimiter: String): Translation {
Expand Down Expand Up @@ -71,6 +88,14 @@ class Day05: AbstractLinesAdventDay<Long>() {
}
return input
}
fun reverseLookup(input: Long): Long {
for (mapping in mappings) {
if (input in mapping.destRange) {
return mapping.sourceRange.first + (input - mapping.destRange.first)
}
}
return input
}
}

data class Mapping(
Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/de/linkel/aoc/Day05Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ humidity-to-location map:
override val solutionA = 107430936L

override val exampleSolutionB = 46L
override val solutionB = 0L
override val solutionB = 23738616L

override val implementation = Day05()
}

0 comments on commit 0e1e8d3

Please sign in to comment.