diff --git a/src/main/kotlin/de/linkel/aoc/Day05.kt b/src/main/kotlin/de/linkel/aoc/Day05.kt index 457ca81..4a499a4 100644 --- a/src/main/kotlin/de/linkel/aoc/Day05.kt +++ b/src/main/kotlin/de/linkel/aoc/Day05.kt @@ -24,23 +24,40 @@ class Day05: AbstractLinesAdventDay() { 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, delimiter: String): Translation { @@ -71,6 +88,14 @@ class Day05: AbstractLinesAdventDay() { } 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( diff --git a/src/test/kotlin/de/linkel/aoc/Day05Test.kt b/src/test/kotlin/de/linkel/aoc/Day05Test.kt index 6af001f..1fb6ad5 100644 --- a/src/test/kotlin/de/linkel/aoc/Day05Test.kt +++ b/src/test/kotlin/de/linkel/aoc/Day05Test.kt @@ -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() }