Complete day 5
This commit is contained in:
parent
5e518b74d4
commit
713bc2b47d
165
day5/day5.kt
Normal file
165
day5/day5.kt
Normal file
|
@ -0,0 +1,165 @@
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
typealias Map = Pair<UIntRange, UIntRange>
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
val input = File("input")
|
||||||
|
|
||||||
|
val (seeds, maps) = parseInput(input)
|
||||||
|
|
||||||
|
println(part1(seeds, maps))
|
||||||
|
|
||||||
|
val seedRanges = seeds.windowed(2, 2) { it.first() ..< it.first() + it.last() }
|
||||||
|
println(part2(seedRanges, maps))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun part1(seeds: List<UInt>, maps: List<List<Map>>): UInt {
|
||||||
|
return seeds.map { seed -> maps.fold(seed) { toFind, map -> findMapping(toFind, map) } }.min()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun findMapping(toFind: UInt, maps: List<Map>): UInt {
|
||||||
|
for (map in maps) {
|
||||||
|
if (toFind >= map.first.first && toFind <= map.first.last) {
|
||||||
|
val index = toFind - map.first.first
|
||||||
|
|
||||||
|
if (map.second.first + index <= map.second.last) {
|
||||||
|
return map.second.first + index
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Any source numbers that aren't mapped correspond to the same
|
||||||
|
// destination number. So, seed number 10 corresponds to soil
|
||||||
|
// number 10.
|
||||||
|
return toFind
|
||||||
|
}
|
||||||
|
|
||||||
|
fun part2(seedRanges: List<UIntRange>, maps: List<List<Map>>): UInt {
|
||||||
|
return seedRanges
|
||||||
|
.map { range ->
|
||||||
|
maps
|
||||||
|
.fold(listOf(range)) { toReduce, map -> findReduction(toReduce, map) }
|
||||||
|
.filter {
|
||||||
|
// TODO(tlater): Remove this cursed filter
|
||||||
|
//
|
||||||
|
// Sometimes the source and range match up
|
||||||
|
// perfectly, and the destination starts
|
||||||
|
// with 0.
|
||||||
|
//
|
||||||
|
// Maybe this is a bug in the puzzle (?!),
|
||||||
|
// or whenever this happens my logic
|
||||||
|
// breaks down and the resulting
|
||||||
|
// translated range always ends up in the
|
||||||
|
// result.
|
||||||
|
//
|
||||||
|
// Nonetheless, filtering these 0s out
|
||||||
|
// gives the right answer.
|
||||||
|
//
|
||||||
|
// More likely it's a booby trap for
|
||||||
|
// *exactly* the logical fallacy I'm
|
||||||
|
// committing?
|
||||||
|
//
|
||||||
|
// That, *or* it's just unlikely any one
|
||||||
|
// range is the lowest in the end, and
|
||||||
|
// these don't happen to contribute, while
|
||||||
|
// the rest is computed correctly.
|
||||||
|
it.first != 0u
|
||||||
|
}
|
||||||
|
.map { it.first }
|
||||||
|
.min()
|
||||||
|
}
|
||||||
|
.min()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun findReduction(toReduce: List<UIntRange>, maps: List<Map>): List<UIntRange> {
|
||||||
|
var result: MutableList<UIntRange> = mutableListOf()
|
||||||
|
var reducing: MutableList<UIntRange> = mutableListOf()
|
||||||
|
reducing.addAll(toReduce)
|
||||||
|
|
||||||
|
for (map in maps) {
|
||||||
|
var next: MutableList<UIntRange> = mutableListOf()
|
||||||
|
val source = map.first
|
||||||
|
val destination = map.second
|
||||||
|
|
||||||
|
for (currentRange in reducing) {
|
||||||
|
val (overlap, excess) = getOverlap(currentRange, source)
|
||||||
|
if (overlap != null) {
|
||||||
|
result.add(getDestinationForOverlap(overlap, source, destination))
|
||||||
|
}
|
||||||
|
next.addAll(excess)
|
||||||
|
}
|
||||||
|
reducing = next
|
||||||
|
}
|
||||||
|
|
||||||
|
result.addAll(reducing)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getDestinationForOverlap(
|
||||||
|
overlap: UIntRange,
|
||||||
|
source: UIntRange,
|
||||||
|
destination: UIntRange
|
||||||
|
): UIntRange {
|
||||||
|
val startDiff = overlap.first - source.first
|
||||||
|
val endDiff = source.last - overlap.last
|
||||||
|
|
||||||
|
return destination.first + startDiff..destination.last - endDiff
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getOverlap(range1: UIntRange, range2: UIntRange): Pair<UIntRange?, List<UIntRange>> {
|
||||||
|
val start =
|
||||||
|
when {
|
||||||
|
range1.first <= range2.first -> range2.first
|
||||||
|
range1.first > range2.first -> range1.first
|
||||||
|
else -> throw Exception("Unreachable")
|
||||||
|
}
|
||||||
|
|
||||||
|
val end =
|
||||||
|
when {
|
||||||
|
range1.last >= range2.last -> range2.last
|
||||||
|
range1.last < range2.last -> range1.last
|
||||||
|
else -> throw Exception("Unreachable")
|
||||||
|
}
|
||||||
|
|
||||||
|
if (start > end) {
|
||||||
|
return Pair(null, listOf(range1))
|
||||||
|
} else {
|
||||||
|
val excess: MutableList<UIntRange> = mutableListOf()
|
||||||
|
|
||||||
|
if (start > range1.first) {
|
||||||
|
excess.add(range1.first ..< start)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (range1.last > end) {
|
||||||
|
excess.add(end + 1u..range1.last)
|
||||||
|
}
|
||||||
|
|
||||||
|
return Pair(start..end, excess)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun parseInput(input: File): Pair<List<UInt>, List<List<Map>>> {
|
||||||
|
val text = input.bufferedReader()
|
||||||
|
|
||||||
|
val seeds = text.readLine().split(": ")[1].split(" ").map { it.toUInt() }
|
||||||
|
text.readLine()
|
||||||
|
|
||||||
|
var line = text.readLine()
|
||||||
|
var maps: MutableList<MutableList<Map>> = mutableListOf()
|
||||||
|
|
||||||
|
while (line != null) {
|
||||||
|
when {
|
||||||
|
line == "" -> Unit // Skip empty lines
|
||||||
|
line.endsWith(':') -> maps.add(mutableListOf())
|
||||||
|
else -> {
|
||||||
|
val (destination, source, length) = line.split(" ").map { it.toUInt() }
|
||||||
|
maps.last()
|
||||||
|
.add(Pair(source ..< source + length, destination ..< destination + length))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
line = text.readLine()
|
||||||
|
}
|
||||||
|
|
||||||
|
return Pair(seeds, maps)
|
||||||
|
}
|
277
day5/input
Normal file
277
day5/input
Normal file
|
@ -0,0 +1,277 @@
|
||||||
|
seeds: 858905075 56936593 947763189 267019426 206349064 252409474 660226451 92561087 752930744 24162055 75704321 63600948 3866217991 323477533 3356941271 54368890 1755537789 475537300 1327269841 427659734
|
||||||
|
|
||||||
|
seed-to-soil map:
|
||||||
|
155461856 2688731658 31130392
|
||||||
|
3624223750 2439220990 158522039
|
||||||
|
3782745789 828496007 83184667
|
||||||
|
1329508120 2383184162 2195641
|
||||||
|
1221066973 4005538912 108441147
|
||||||
|
3908459768 630332333 84015758
|
||||||
|
3174818941 2385379803 53841187
|
||||||
|
1929319035 442552891 126280229
|
||||||
|
2999332985 2077874265 61338040
|
||||||
|
4047976345 4113980059 50652162
|
||||||
|
3509978464 2319317936 63866226
|
||||||
|
2676242573 568833120 11879128
|
||||||
|
4146174193 3589840038 35847908
|
||||||
|
3865930456 2986517951 42529312
|
||||||
|
1737209176 3963710397 41828515
|
||||||
|
2545388005 3458985470 130854568
|
||||||
|
388171875 2946497456 40020495
|
||||||
|
3396281190 4164632221 79956015
|
||||||
|
186592248 1068698177 201579627
|
||||||
|
3060671025 714348091 114147916
|
||||||
|
2841183920 2139212305 158149065
|
||||||
|
2055599264 3290256747 27665085
|
||||||
|
2770917385 3029047263 24920289
|
||||||
|
4203978667 2597743029 90988629
|
||||||
|
2795837674 911680674 45346246
|
||||||
|
2083264349 2813972956 124101205
|
||||||
|
3573844690 4244588236 50379060
|
||||||
|
3228660128 274931829 167621062
|
||||||
|
1331703761 1270277804 23145153
|
||||||
|
123824510 3053967552 31637346
|
||||||
|
1354848914 63482157 211449672
|
||||||
|
1566298586 3085604898 170910590
|
||||||
|
63482157 3398643117 60342353
|
||||||
|
3476237205 3256515488 33741259
|
||||||
|
1212643678 2938074161 8423295
|
||||||
|
4182022101 2297361370 21956566
|
||||||
|
3992475526 2758472137 55500819
|
||||||
|
428192370 1293422957 784451308
|
||||||
|
1817647778 957026920 111671257
|
||||||
|
2690196100 3317921832 80721285
|
||||||
|
1779037691 2719862050 38610087
|
||||||
|
4098628507 582786647 47545686
|
||||||
|
2207365554 3625687946 338022451
|
||||||
|
2688121701 580712248 2074399
|
||||||
|
|
||||||
|
soil-to-fertilizer map:
|
||||||
|
2233530624 2071120505 422489795
|
||||||
|
3959649465 4044567411 67616189
|
||||||
|
457224864 3135270167 41569374
|
||||||
|
3394360972 4112183600 182783696
|
||||||
|
813178253 653702020 183734547
|
||||||
|
498794238 2963315974 171954193
|
||||||
|
3577144668 3394360972 216442995
|
||||||
|
996912800 2493610300 327275852
|
||||||
|
3309722439 3282071367 51304963
|
||||||
|
670748431 2820886152 142429822
|
||||||
|
1324188652 3273822993 8248374
|
||||||
|
3793587663 4006086952 11248169
|
||||||
|
2190085090 3176839541 43445534
|
||||||
|
2656020419 0 653702020
|
||||||
|
4267735006 4017335121 27232290
|
||||||
|
1396875915 3333376330 27651072
|
||||||
|
4027265654 3610803967 240469352
|
||||||
|
1424526987 1241123513 765558103
|
||||||
|
403686946 3220285075 53537918
|
||||||
|
3804835832 3851273319 154813633
|
||||||
|
1332437026 2006681616 64438889
|
||||||
|
0 837436567 403686946
|
||||||
|
|
||||||
|
fertilizer-to-water map:
|
||||||
|
490664090 841327865 158559818
|
||||||
|
3248785910 3009217852 86780947
|
||||||
|
3361383178 811709798 29618067
|
||||||
|
1492567300 999887683 10142868
|
||||||
|
708758546 1053715632 8318621
|
||||||
|
2922655819 2337575757 139997093
|
||||||
|
412870134 1010030551 43685081
|
||||||
|
825309561 1895146974 205741813
|
||||||
|
3832608203 4026085284 5525526
|
||||||
|
2833741007 722794986 88914812
|
||||||
|
1502710168 0 86876502
|
||||||
|
1091543314 2480502815 191523114
|
||||||
|
2191676162 1359275248 59328417
|
||||||
|
3724763033 3918240114 107845170
|
||||||
|
4132175069 4031610810 162792227
|
||||||
|
1589586670 3262683858 70888723
|
||||||
|
3932786977 4194403037 100564259
|
||||||
|
649223908 2280749089 56826668
|
||||||
|
717077167 268230878 108232394
|
||||||
|
4033351236 3819416281 98823833
|
||||||
|
2417689638 1418603665 416051369
|
||||||
|
3062652912 3333572581 186132998
|
||||||
|
1438604483 2100888787 28445966
|
||||||
|
398871700 1190738587 13998434
|
||||||
|
456555215 2975108977 34108875
|
||||||
|
2062848712 2151921639 128827450
|
||||||
|
1283066428 112692823 155538055
|
||||||
|
0 475753543 244333473
|
||||||
|
3838133729 3724763033 94653248
|
||||||
|
244333473 1204737021 154538227
|
||||||
|
1963558441 376463272 99290271
|
||||||
|
3335566857 86876502 25816321
|
||||||
|
3497674085 1062034253 22031494
|
||||||
|
706050576 720087016 2707970
|
||||||
|
1467050449 2129334753 22586886
|
||||||
|
1660475393 2672025929 303083048
|
||||||
|
3391001245 1084065747 106672840
|
||||||
|
1031051374 1834655034 60491940
|
||||||
|
1489637335 2477572850 2929965
|
||||||
|
2251004579 3095998799 166685059
|
||||||
|
|
||||||
|
water-to-light map:
|
||||||
|
2001282667 520918173 10302354
|
||||||
|
2377955968 3484205484 30954740
|
||||||
|
3394017453 2582587136 46684978
|
||||||
|
99573517 443623963 59355616
|
||||||
|
2759073879 2842283022 106219470
|
||||||
|
3153456782 2203695607 115107673
|
||||||
|
1088011149 228872916 37401521
|
||||||
|
823056142 1852553918 234443509
|
||||||
|
1892663777 1743935028 108618890
|
||||||
|
2223186841 3648924866 154769127
|
||||||
|
3440702431 3849149039 360654480
|
||||||
|
1125412670 266274437 146838028
|
||||||
|
2524006636 2432683670 149903466
|
||||||
|
86866324 508210980 12707193
|
||||||
|
3289576295 3621907778 27017088
|
||||||
|
659725308 531220527 163330834
|
||||||
|
336816212 1738401718 5533310
|
||||||
|
1057499651 413112465 30511498
|
||||||
|
1495475061 998625681 113056371
|
||||||
|
1277482099 1111682052 217992962
|
||||||
|
4022707650 2629272114 213010908
|
||||||
|
1611665062 1454269373 134219470
|
||||||
|
1272250698 502979579 5231401
|
||||||
|
3801356911 2996165168 221350739
|
||||||
|
2463542360 3803693993 45455046
|
||||||
|
3367828633 3458016664 26188820
|
||||||
|
3105794106 2948502492 47662676
|
||||||
|
0 1329675014 86866324
|
||||||
|
342349522 694551361 126187241
|
||||||
|
158929133 820738602 177887079
|
||||||
|
1608531432 1735268088 3133630
|
||||||
|
2673910102 4209803519 85163777
|
||||||
|
3316593383 2152460357 51235250
|
||||||
|
3268564455 2116439287 21011840
|
||||||
|
2508997406 2137451127 15009230
|
||||||
|
2011585021 153460510 75412406
|
||||||
|
1745884532 1588488843 146779245
|
||||||
|
621997273 1416541338 37728035
|
||||||
|
2865293349 3217515907 240500757
|
||||||
|
2408910708 2318803280 54631652
|
||||||
|
468536763 0 153460510
|
||||||
|
4235718558 2373434932 59248738
|
||||||
|
2116439287 3515160224 106747554
|
||||||
|
|
||||||
|
light-to-temperature map:
|
||||||
|
1588027222 1909294051 23480302
|
||||||
|
2215874455 2960213648 162391531
|
||||||
|
2129487527 3462600798 86386928
|
||||||
|
3484568571 1932774353 37497788
|
||||||
|
1911434001 1113198957 54894781
|
||||||
|
880849827 535842660 44062779
|
||||||
|
1450966888 2803663201 137060334
|
||||||
|
0 175732913 3492989
|
||||||
|
820881151 879323733 45588873
|
||||||
|
2593908227 3122605179 276522050
|
||||||
|
4086321665 3399127229 59629850
|
||||||
|
1267064668 2940723535 19490113
|
||||||
|
872717966 579905439 8131861
|
||||||
|
17372207 0 175732913
|
||||||
|
325280839 781495589 97828144
|
||||||
|
485661210 200622719 335219941
|
||||||
|
866470024 712695420 6247942
|
||||||
|
1113198957 1970272141 153865711
|
||||||
|
3384296082 2124137852 23097298
|
||||||
|
3492989 186743501 13879218
|
||||||
|
1966328782 1746135306 163158745
|
||||||
|
3522066359 3730711990 564255306
|
||||||
|
1611507524 2661100955 108644269
|
||||||
|
193105120 588037300 124658120
|
||||||
|
423108983 718943362 62552227
|
||||||
|
2870430277 2147235150 513865805
|
||||||
|
2559990250 2769745224 33917977
|
||||||
|
4145951515 1168093738 149015781
|
||||||
|
1720151793 1558696817 187438489
|
||||||
|
3407393380 1317109519 77175191
|
||||||
|
1286554781 1394284710 164412107
|
||||||
|
317763240 179225902 7517599
|
||||||
|
2378265986 3548987726 181724264
|
||||||
|
1907590282 3458757079 3843719
|
||||||
|
|
||||||
|
temperature-to-humidity map:
|
||||||
|
2368894659 1388350150 55368176
|
||||||
|
3669222180 3739392779 16773160
|
||||||
|
4199646729 3863166637 79873794
|
||||||
|
399562411 1508754364 89530529
|
||||||
|
355021451 346145384 44540960
|
||||||
|
3348191019 2804337123 32367820
|
||||||
|
4153047357 3630809755 46599372
|
||||||
|
801745354 3027789847 194064703
|
||||||
|
295043332 1932435496 59978119
|
||||||
|
2173824398 0 195070261
|
||||||
|
3586766138 3780710595 82456042
|
||||||
|
2063962300 390686344 109862098
|
||||||
|
2424813637 2262322190 16002572
|
||||||
|
694625758 2155376830 106945360
|
||||||
|
4279520523 3615362982 15446773
|
||||||
|
489092940 1655816946 66193390
|
||||||
|
0 3221854550 178998524
|
||||||
|
1882939638 1912164198 20271298
|
||||||
|
178998524 1187862900 104517087
|
||||||
|
288525818 1292379987 6517514
|
||||||
|
995810057 1598284893 57532053
|
||||||
|
3220600544 1992413615 127590475
|
||||||
|
1857243139 195070261 25696499
|
||||||
|
2424262835 1443718326 550802
|
||||||
|
3143673104 1375907946 12442204
|
||||||
|
3776575836 3943040431 220205828
|
||||||
|
1053342110 693558780 494304120
|
||||||
|
2630970071 2286623883 512703033
|
||||||
|
638901084 2284929071 1694812
|
||||||
|
801571118 2836704943 174236
|
||||||
|
640595896 2836879179 54029862
|
||||||
|
4128502701 3756165939 24544656
|
||||||
|
1684527036 520842677 172716103
|
||||||
|
1547646230 2890909041 136880806
|
||||||
|
3524782486 3677409127 61983652
|
||||||
|
3685995340 3524782486 90580496
|
||||||
|
3380558839 500548442 20294235
|
||||||
|
2440816209 1722010336 190153862
|
||||||
|
283515611 2799326916 5010207
|
||||||
|
1903210936 2120004090 35372740
|
||||||
|
561890639 1298897501 77010445
|
||||||
|
555286330 2278324762 6604309
|
||||||
|
1938583676 220766760 125378624
|
||||||
|
3996781664 4163246259 131721037
|
||||||
|
3156115308 1444269128 64485236
|
||||||
|
|
||||||
|
humidity-to-location map:
|
||||||
|
2420477851 2725678119 154870744
|
||||||
|
341010579 1968896618 161603351
|
||||||
|
2771047693 3040747240 63786420
|
||||||
|
1473264654 231223255 158065928
|
||||||
|
300743457 665216997 40267122
|
||||||
|
4266394999 3974648226 28572297
|
||||||
|
44642725 3145344681 187170981
|
||||||
|
3913829571 3489505645 352565428
|
||||||
|
2132898063 571123277 94093720
|
||||||
|
2945529238 1729589230 52970089
|
||||||
|
3205711977 3366573953 87052626
|
||||||
|
3077771609 1429791513 87129347
|
||||||
|
3840363983 3901182638 31190715
|
||||||
|
3674520209 3842071073 59111565
|
||||||
|
3733631774 4003220523 106732209
|
||||||
|
0 2326199067 44642725
|
||||||
|
1631330582 389289183 181834094
|
||||||
|
2226991783 2370841792 193486068
|
||||||
|
2911470947 3332515662 34058291
|
||||||
|
1999501975 1516920860 133396088
|
||||||
|
1813164676 1782559319 186337299
|
||||||
|
502613930 2880548863 91268626
|
||||||
|
1242041399 0 231223255
|
||||||
|
3586449755 4206896842 88070454
|
||||||
|
2834834113 1353642962 76148551
|
||||||
|
3164900956 3104533660 40811021
|
||||||
|
3489505645 4109952732 96944110
|
||||||
|
2910982664 2564327860 488283
|
||||||
|
3292764603 2564816143 160861976
|
||||||
|
231813706 2971817489 68929751
|
||||||
|
2575348595 2130499969 195699098
|
||||||
|
593882556 705484119 648158843
|
||||||
|
3871554698 3932373353 42274873
|
||||||
|
2998499327 1650316948 79272282
|
33
day5/test
Normal file
33
day5/test
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
seeds: 79 14 55 13
|
||||||
|
|
||||||
|
seed-to-soil map:
|
||||||
|
50 98 2
|
||||||
|
52 50 48
|
||||||
|
|
||||||
|
soil-to-fertilizer map:
|
||||||
|
0 15 37
|
||||||
|
37 52 2
|
||||||
|
39 0 15
|
||||||
|
|
||||||
|
fertilizer-to-water map:
|
||||||
|
49 53 8
|
||||||
|
0 11 42
|
||||||
|
42 0 7
|
||||||
|
57 7 4
|
||||||
|
|
||||||
|
water-to-light map:
|
||||||
|
88 18 7
|
||||||
|
18 25 70
|
||||||
|
|
||||||
|
light-to-temperature map:
|
||||||
|
45 77 23
|
||||||
|
81 45 19
|
||||||
|
68 64 13
|
||||||
|
|
||||||
|
temperature-to-humidity map:
|
||||||
|
0 69 1
|
||||||
|
1 0 69
|
||||||
|
|
||||||
|
humidity-to-location map:
|
||||||
|
60 56 37
|
||||||
|
56 93 4
|
Loading…
Reference in a new issue