Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package year_2023.day_5
- import kotlin.math.absoluteValue
- fun partOne() {
- println("[2023] Day five, part one...")
- val inputData = input()
- fun getMap(mapName: String): List<Pair<LongRange, LongRange>> {
- val map = inputData.split("\n\n")
- .single { it.contains(mapName) }
- .substringAfter("$mapName map:")
- .trim()
- .split("\n")
- .map { it.split(" ") }
- .map {
- val startSource = it[1].toLong()
- val startTarget = it[0].toLong()
- val range = it[2].toLong()
- (startSource until startSource + range) to (startTarget until startTarget + range)
- }
- return map
- }
- val seeds = inputData.split("\n\n")[0]
- .substringAfter(":")
- .trim()
- .split(" ")
- .map { it.toLong() }
- val seedToSoilRanges = getMap("seed-to-soil")
- val soilToFertilizerRanges = getMap("soil-to-fertilizer")
- val fertilizerToWaterRanges = getMap("fertilizer-to-water")
- val waterToLightRanges = getMap("water-to-light")
- val lightToTemperatureRanges = getMap("light-to-temperature")
- val temperatureToHumidityRanges = getMap("temperature-to-humidity")
- val humidityToLocationRanges = getMap("humidity-to-location")
- val seedToLocationMap = mutableMapOf<Long, Long>()
- fun getNext(map: List<Pair<LongRange, LongRange>>, source: Long): Long {
- val ranges = map.firstOrNull { it.first.contains(source) }
- ?: return source
- return ranges.second.first + (source - ranges.first.first).absoluteValue
- }
- seeds.forEach { seed ->
- val soil = getNext(seedToSoilRanges, seed)
- val fertilizer = getNext(soilToFertilizerRanges, soil)
- val water = getNext(fertilizerToWaterRanges, fertilizer)
- val light = getNext(waterToLightRanges, water)
- val temperature = getNext(lightToTemperatureRanges, light)
- val humidity = getNext(temperatureToHumidityRanges, temperature)
- val location = getNext(humidityToLocationRanges, humidity)
- seedToLocationMap[seed] = location
- }
- println(seedToLocationMap.minBy { it.value }.value)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement