Fixed day 5 :-/
Change-Id: I927bfc1e9e16be8a30f5a9b6d639fc92392db9a2
This commit is contained in:
parent
ac0c0015d3
commit
c33d708760
@ -131,7 +131,15 @@ humidity-to-location map:
|
||||
|
||||
|
||||
fun part1(input: List<String>): Long {
|
||||
val typeMap = mapOf("seed" to 0, "soil" to 1, "fertilizer" to 2, "water" to 3, "light" to 4, "temperature" to 5, "humidity" to 6)
|
||||
val typeMap = mapOf(
|
||||
"seed" to 0,
|
||||
"soil" to 1,
|
||||
"fertilizer" to 2,
|
||||
"water" to 3,
|
||||
"light" to 4,
|
||||
"temperature" to 5,
|
||||
"humidity" to 6
|
||||
)
|
||||
val mapOfMaps = HashMap<Int, MutableList<Pair<LongRange, Long>>>()
|
||||
var seeds = LongArray(0)
|
||||
var fromId = 0
|
||||
@ -168,7 +176,15 @@ humidity-to-location map:
|
||||
}
|
||||
|
||||
fun part2(input: List<String>): Long {
|
||||
val typeMap = mapOf("seed" to 0, "soil" to 1, "fertilizer" to 2, "water" to 3, "light" to 4, "temperature" to 5, "humidity" to 6)
|
||||
val typeMap = mapOf(
|
||||
"seed" to 0,
|
||||
"soil" to 1,
|
||||
"fertilizer" to 2,
|
||||
"water" to 3,
|
||||
"light" to 4,
|
||||
"temperature" to 5,
|
||||
"humidity" to 6
|
||||
)
|
||||
val mapOfMaps = HashMap<Int, MutableList<Pair<LongRange, Long>>>()
|
||||
var seeds = ArrayDeque<LongRange>()
|
||||
var fromId = 0
|
||||
@ -176,7 +192,9 @@ humidity-to-location map:
|
||||
if (i.contains(":")) {
|
||||
val (type, data) = i.split(":")
|
||||
when (type) {
|
||||
"seeds" -> seeds = ArrayDeque(data.trim().split(" ").map { it.toLong() }.chunked(2).map { LongRange(it[0], it[0] + it[1] - 1) })
|
||||
"seeds" -> seeds = ArrayDeque(data.trim().split(" ").map { it.toLong() }.chunked(2)
|
||||
.map { LongRange(it[0], it[0] + it[1] - 1) })
|
||||
|
||||
else -> {
|
||||
val from = type.split("-")[0]
|
||||
fromId = typeMap[from]!!
|
||||
@ -200,30 +218,44 @@ humidity-to-location map:
|
||||
var found = false
|
||||
for ((mapRange, destPos) in rangeLists) {
|
||||
if (sourceRange.first in mapRange && sourceRange.last in mapRange) {
|
||||
val dest = destPos + (sourceRange.first - mapRange.start)
|
||||
newRanges.add(LongRange(dest, dest + sourceRange.count() - 1))
|
||||
val dest = destPos + (sourceRange.first - mapRange.first)
|
||||
val destLength = sourceRange.last + 1 - sourceRange.first
|
||||
newRanges.add(LongRange(dest, dest + destLength - 1))
|
||||
found = true
|
||||
break
|
||||
} else if (sourceRange.first in mapRange) {
|
||||
val dest = destPos + (sourceRange.first - mapRange.start)
|
||||
val destLength = mapRange.endInclusive - sourceRange.first + 1
|
||||
newRanges.add(LongRange(dest, dest + destLength))
|
||||
val newSource = mapRange.endInclusive + 1
|
||||
val newSourceLength = sourceRange.endInclusive - mapRange.endInclusive
|
||||
sourceRanges.addFirst(LongRange(newSource, newSource + newSourceLength))
|
||||
val dest = destPos + (sourceRange.first - mapRange.first)
|
||||
val destLength = mapRange.last + 1 - sourceRange.first
|
||||
newRanges.add(LongRange(dest, dest + destLength - 1))
|
||||
val newSource = mapRange.last + 1
|
||||
val newSourceLength = sourceRange.last - mapRange.last
|
||||
sourceRanges.addFirst(LongRange(newSource, newSource + newSourceLength - 1))
|
||||
found = true
|
||||
break
|
||||
} else if (sourceRange.last in mapRange) {
|
||||
val destLength = sourceRange.last - mapRange.first
|
||||
val dest = destPos + mapRange.first - sourceRange.first
|
||||
newRanges.add(LongRange(dest, dest + destLength))
|
||||
val newSource = mapRange.start
|
||||
val newSourceLength = sourceRange.start - mapRange.start
|
||||
sourceRanges.addFirst(LongRange(newSource, newSource + newSourceLength))
|
||||
val destLength = sourceRange.last + 1 - mapRange.first
|
||||
val dest = destPos
|
||||
newRanges.add(LongRange(dest, dest + destLength - 1))
|
||||
val newSource = sourceRange.first
|
||||
val newSourceLength = mapRange.first - sourceRange.first
|
||||
sourceRanges.addFirst(LongRange(newSource, newSource + newSourceLength - 1))
|
||||
found = true
|
||||
break
|
||||
// funnily, this is unused, but it should also be a possibility
|
||||
// } else if (sourceRange.first < mapRange.first && sourceRange.last > mapRange.last) {
|
||||
// val destLength = mapRange.last + 1 - mapRange.first
|
||||
// val dest = destPos + mapRange.first - sourceRange.first
|
||||
// newRanges.add(LongRange(dest, dest + destLength - 1))
|
||||
// val newSource1 = mapRange.first
|
||||
// val newSource1Length = mapRange.first - sourceRange.first - 1
|
||||
// sourceRanges.addFirst(LongRange(newSource1, newSource1 + newSource1Length - 1))
|
||||
// val newSource2 = mapRange.last + 1
|
||||
// val newSource2Length = sourceRange.last - mapRange.last + 1
|
||||
// sourceRanges.addFirst(LongRange(newSource2, newSource2 + newSource2Length - 1))
|
||||
// found = true
|
||||
// break
|
||||
}
|
||||
if (mapRange.first > sourceRange.first) break
|
||||
//if (mapRange.first > sourceRange.first) break
|
||||
}
|
||||
if (!found) {
|
||||
newRanges.add(sourceRange)
|
||||
|
Loading…
Reference in New Issue
Block a user