Update solution
This commit is contained in:
parent
b636dd4c43
commit
36dc294e98
2 changed files with 59 additions and 40 deletions
|
@ -1,6 +1,6 @@
|
|||
class Solution05(lines: List<String>) {
|
||||
private var seeds: MutableList<ULong> = mutableListOf()
|
||||
private var maps: MutableMap<String, Ranges> = mutableMapOf()
|
||||
private var maps: MutableList<MapList> = mutableListOf()
|
||||
|
||||
init {
|
||||
// Parse seeds
|
||||
|
@ -8,24 +8,16 @@ class Solution05(lines: List<String>) {
|
|||
seeds.addAll(seedsStr.split(" ").map{it.toULong()})
|
||||
|
||||
// Parse all maps
|
||||
var currentSrc = ""
|
||||
for (line in lines.subList(2, lines.size)) {
|
||||
if (line.isEmpty()) continue
|
||||
|
||||
if (line.contains(" map:")) {
|
||||
val (mapTitle, _) = line.split(" ")
|
||||
val (src, dest) = mapTitle.split("-to-")
|
||||
currentSrc = src
|
||||
|
||||
if (!maps.containsKey(currentSrc)) {
|
||||
maps[currentSrc] = Ranges(dest)
|
||||
}
|
||||
|
||||
maps.add(MapList())
|
||||
continue
|
||||
}
|
||||
|
||||
val (destRangeStart, srcRangeStart, rangeLength) = line.split(" ")
|
||||
maps[currentSrc]?.addRange(
|
||||
maps.last().addRange(
|
||||
srcRangeStart.toULong(),
|
||||
destRangeStart.toULong(),
|
||||
rangeLength.toULong(),
|
||||
|
@ -33,52 +25,63 @@ class Solution05(lines: List<String>) {
|
|||
}
|
||||
}
|
||||
|
||||
// Part 1.
|
||||
fun getLowestLocationNumber(): ULong {
|
||||
return seeds
|
||||
.map{convertSeedNumber(it, "location")}
|
||||
.map{
|
||||
var num = it
|
||||
|
||||
for (map in maps) {
|
||||
num = map.mapSrcNumberToDest(num)
|
||||
}
|
||||
|
||||
num
|
||||
}
|
||||
.min()
|
||||
}
|
||||
|
||||
// Part 2.
|
||||
fun getLowestLocationNumberForRangeOfSeeds(): ULong {
|
||||
val allSeeds: MutableSet<ULong> = mutableSetOf()
|
||||
// Available location numbers.
|
||||
var currentRanges = maps
|
||||
.last()
|
||||
.mappers
|
||||
.map{RangeFromLoc(it.destRangeStart, it.rangeLength, it.destRangeStart)}
|
||||
|
||||
for (map in maps.reversed()) {
|
||||
val newList: MutableList<RangeFromLoc> = mutableListOf()
|
||||
|
||||
for (mapper in map.mappers) {
|
||||
// Find intersections
|
||||
currentRanges
|
||||
|
||||
|
||||
for (rng in currentRanges) {
|
||||
|
||||
for (i in 0..<seeds.size step 2) {
|
||||
for (j in seeds[i]..<seeds[i]+seeds[i+1]) {
|
||||
allSeeds.add(j)
|
||||
}
|
||||
}
|
||||
|
||||
return allSeeds
|
||||
.map{convertSeedNumber(it, "location")}
|
||||
.min()
|
||||
currentRanges = newList
|
||||
}
|
||||
|
||||
private fun convertSeedNumber(seed: ULong, target: String): ULong {
|
||||
var dest = "seed"
|
||||
|
||||
var num = seed
|
||||
|
||||
while (dest != target) {
|
||||
val ranges = maps[dest]!!
|
||||
dest = ranges.destName
|
||||
num = ranges.mapNumber(num)
|
||||
}
|
||||
|
||||
return num
|
||||
return 0UL
|
||||
}
|
||||
}
|
||||
|
||||
data class Ranges(val destName: String) {
|
||||
val ranges: MutableList<Range> = mutableListOf()
|
||||
// Map stores the rules for matching one number to another.
|
||||
class MapList() {
|
||||
val mappers: MutableList<Mapper> = mutableListOf()
|
||||
|
||||
fun addRange(sourceRangeStart: ULong, destRangeStart: ULong, rangeLength: ULong) {
|
||||
ranges.add(Range(sourceRangeStart, destRangeStart, rangeLength))
|
||||
mappers.add(
|
||||
Mapper(sourceRangeStart, destRangeStart, rangeLength),
|
||||
)
|
||||
}
|
||||
|
||||
fun mapNumber(src: ULong): ULong {
|
||||
for (range in ranges) {
|
||||
if (src >= range.sourceRangeStart && src < range.sourceRangeStart + range.rangeLength) {
|
||||
return range.destRangeStart + src - range.sourceRangeStart
|
||||
fun mapSrcNumberToDest(src: ULong): ULong {
|
||||
for (range in mappers) {
|
||||
if (range.isNumberInSrcRange(src)) {
|
||||
return range.srcValueToDest(src)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -86,4 +89,20 @@ data class Ranges(val destName: String) {
|
|||
}
|
||||
}
|
||||
|
||||
data class Range(val sourceRangeStart: ULong, val destRangeStart: ULong, val rangeLength: ULong)
|
||||
// Mapper contains one line of map.
|
||||
data class Mapper(val sourceRangeStart: ULong, val destRangeStart: ULong, val rangeLength: ULong) {
|
||||
fun srcValueToDest(v: ULong): ULong {
|
||||
return destRangeStart + v - sourceRangeStart
|
||||
}
|
||||
|
||||
fun isNumberInSrcRange(num: ULong): Boolean {
|
||||
return num >= sourceRangeStart && num < sourceRangeStart + rangeLength
|
||||
}
|
||||
}
|
||||
|
||||
// Range of numbers with the first number of corresponding location.
|
||||
data class RangeFromLoc(val rangeStart: ULong, val rangeLength: ULong, val locationStart: ULong) {
|
||||
fun getIntersection(newRangeStart: ULong, newRangeLength: ULong): RangeFromLoc {
|
||||
return RangeFromLoc(rangeStart, rangeLength, locationStart)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ class Solution05Test {
|
|||
fun solvePart1() {
|
||||
val text = ResourceReader().readFile("day-05/input.txt")
|
||||
val res = Solution05(text).getLowestLocationNumber()
|
||||
println(res)
|
||||
assertEquals(621354867UL, res)
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in a new issue