Skip to content

Commit f1257c0

Browse files
authored
Merge pull request #8 from palinkiewicz/2023_d05
Change 2023 Day 5 Part 2 solution to the faster one
2 parents 1bc4c61 + daf75b9 commit f1257c0

File tree

1 file changed

+42
-46
lines changed

1 file changed

+42
-46
lines changed

adventofcode2023/Day05.kt

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package adventofcode2023
22

33
import java.io.File
4+
import kotlin.math.max
5+
import kotlin.math.min
46

57
object Day05 {
68
fun part1(inputs: List<String>) {
@@ -24,63 +26,57 @@ object Day05 {
2426
}
2527

2628
fun part2(inputs: List<String>) {
27-
fun getRangesMaps(): Array<MutableList<LongArray>> {
28-
val rangesMaps = Array<MutableList<LongArray>>(7) { mutableListOf() }
29-
var mapIndex = 0
30-
31-
for (i in inputs.indices.drop(2)) {
32-
if (inputs[i] == "")
33-
mapIndex++
34-
else if (inputs[i].split(" ").size == 3)
35-
rangesMaps[mapIndex].add(inputs[i].split(" ").map { it.toLong() }.toLongArray())
29+
fun getRangesMaps(): List<List<LongArray>> = inputs
30+
.joinToString("\n")
31+
.split("\n\n")
32+
.drop(1)
33+
.map { map ->
34+
map.split("\n").drop(1).map { line ->
35+
line.split(" ").map { it.toLong() }.toLongArray()
36+
}
3637
}
3738

38-
return rangesMaps
39-
}
40-
41-
fun getSeedRanges(): List<Pair<Long, Long>> {
42-
val rangesLine = inputs[0].split(" ").drop(1)
43-
val ranges: MutableList<Pair<Long, Long>> = mutableListOf()
44-
45-
for (i in rangesLine.indices) {
46-
if (i % 2 == 0) ranges.add(
47-
Pair(
48-
rangesLine[i].toLong(),
49-
rangesLine[i].toLong() + rangesLine[i + 1].toLong()
50-
)
51-
)
39+
fun getSeedRanges(): List<Pair<Long, Long>> = inputs[0]
40+
.split(" ")
41+
.drop(1)
42+
.windowed(2, 2)
43+
.map {
44+
Pair(it[0].toLong(), it[0].toLong() + it[1].toLong())
5245
}
5346

54-
return ranges
55-
}
5647

5748
val rangesMaps = getRangesMaps()
58-
val seedRanges = getSeedRanges()
59-
60-
fun getSeedFromLocation(location: Long): Long {
61-
var number = location
62-
var mapIndex = rangesMaps.size - 1
63-
64-
while (mapIndex >= 0) {
65-
for (line in rangesMaps[mapIndex]) {
66-
if (line[0] <= number && line[0] + line[2] > number) {
67-
number = line[1] + number - line[0]
68-
break
49+
var ranges = getSeedRanges().toMutableList()
50+
51+
for (rangesMap in rangesMaps) {
52+
val newRanges = mutableListOf<Pair<Long, Long>>()
53+
for (filterRange in rangesMap) {
54+
var i = 0
55+
while (i < ranges.size) {
56+
val range = ranges[i]
57+
val offset = filterRange[0] - filterRange[1]
58+
val overlapStart = max(filterRange[1], range.first)
59+
val overlapEnd = min(filterRange[1] + filterRange[2], range.second)
60+
61+
if (overlapStart < overlapEnd) {
62+
newRanges.add(Pair(offset + overlapStart, offset + overlapEnd))
63+
ranges.removeAt(i)
64+
65+
if (range.second > overlapEnd) {
66+
ranges.add(Pair(overlapEnd, range.second))
67+
}
68+
if (range.first < overlapStart) {
69+
ranges.add(Pair(range.first, overlapStart))
70+
}
71+
} else {
72+
i++
6973
}
7074
}
71-
mapIndex--
7275
}
73-
74-
return number
76+
ranges = (newRanges + ranges).toMutableList()
7577
}
7678

77-
for (location in 0L..Long.MAX_VALUE) {
78-
val number = getSeedFromLocation(location)
79-
if (seedRanges.any { number >= it.first && number < it.second }) {
80-
println(location)
81-
break
82-
}
83-
}
79+
println(ranges.minOf { it.first })
8480
}
8581
}
8682

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy