Skip to content

Commit 6dab8e4

Browse files
committed
add 2023/day5 solution 💀
1 parent 849a80d commit 6dab8e4

File tree

4 files changed

+367
-0
lines changed

4 files changed

+367
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package advent2023
2+
3+
private const val COLON_DELIMITER = ":"
4+
5+
class Day05(private val input: String) {
6+
7+
fun solvePart1(): Long {
8+
// Since the transformations are a chain, they can be overwritten
9+
var seeds = input
10+
.substringBefore("\n\n")
11+
.substringAfter(COLON_DELIMITER)
12+
.split(" ")
13+
.filter { it.isNotEmpty() }
14+
.map { it.toLong() }
15+
.toMutableList()
16+
17+
val transformationChunks = parseInput()
18+
19+
transformationChunks.forEach { transformationChunk ->
20+
seeds = seeds.map { seed ->
21+
transformationChunk.map { transformation ->
22+
val (destinationStart, sourceStart, rangeLength) = transformation
23+
if (seed in sourceStart until sourceStart + rangeLength) {
24+
destinationStart + (seed - sourceStart)
25+
} else {
26+
Long.MIN_VALUE
27+
}
28+
}.firstOrNull { it != Long.MIN_VALUE } ?: seed
29+
}.toMutableList()
30+
}
31+
return seeds.minOf { it }
32+
}
33+
34+
fun solvePart2(): Long {
35+
val inputs = input
36+
.substringBefore("\n\n")
37+
.substringAfter(COLON_DELIMITER)
38+
.split(" ")
39+
.filter { it.isNotEmpty() }
40+
.map { it.toLong() }
41+
.toMutableList()
42+
43+
var seeds = mutableListOf<List<Long>>()
44+
for (i in 0 until inputs.size step 2) {
45+
seeds.add(listOf(inputs[i], inputs[i] + inputs[i + 1]))
46+
}
47+
48+
val transformationChunks = parseInput()
49+
50+
transformationChunks.forEach { transformationChunk ->
51+
val updatedSeeds = mutableListOf<List<Long>>()
52+
while (seeds.size > 0) {
53+
val (start, end) = seeds.removeLast()
54+
var didBreak = false
55+
for (transformation in transformationChunk) {
56+
val (destinationStart, sourceStart, rangeLength) = transformation
57+
val overlapStart = maxOf(start, sourceStart)
58+
val overlapEnd = minOf(end, sourceStart + rangeLength)
59+
if (overlapStart < overlapEnd) {
60+
updatedSeeds.add(
61+
listOf(
62+
overlapStart - sourceStart + destinationStart,
63+
overlapEnd - sourceStart + destinationStart
64+
)
65+
)
66+
if (overlapStart > start) {
67+
seeds.add(listOf(start, overlapStart))
68+
}
69+
if (end > overlapEnd) {
70+
seeds.add(listOf(overlapEnd, end))
71+
}
72+
didBreak = true
73+
break
74+
}
75+
}
76+
if (!didBreak) {
77+
updatedSeeds.add(listOf(start, end))
78+
}
79+
}
80+
seeds = updatedSeeds
81+
}
82+
seeds.sortBy { it.first() }
83+
return seeds.first().first()
84+
}
85+
86+
private fun parseInput(): List<List<Triple<Long, Long, Long>>> {
87+
return input.split("\n\n").drop(1).map { transformationChunk ->
88+
transformationChunk.lines().drop(1).map { transformationLine ->
89+
val (destinationStart, sourceStart, rangeLength) = transformationLine
90+
.split(" ")
91+
.filter { it.isNotEmpty() }
92+
.map { it.toLong() }
93+
Triple(destinationStart, sourceStart, rangeLength)
94+
}
95+
}
96+
}
97+
}
98+
99+
fun main() {
100+
val input = Resources.resourceAsText("advent2023/day05.txt")
101+
val day05 = Day05(input = input)
102+
println("Part 1 Solution: ${day05.solvePart1()}")
103+
println("Part 2 Solution: ${day05.solvePart2()}")
104+
}
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
seeds: 4088478806 114805397 289354458 164506173 1415635989 166087295 1652880954 340945548 3561206012 483360452 35205517 252097746 1117825174 279314434 3227452369 145640027 2160384960 149488635 2637152665 236791935
2+
3+
seed-to-soil map:
4+
3333452986 2926455387 455063168
5+
3222292973 1807198589 111160013
6+
4073195028 1120843626 221772268
7+
3215232741 2255546991 7060232
8+
1658311530 2727928910 32644400
9+
2680271553 1918358602 337188389
10+
1690955930 3973557555 28589896
11+
2081345351 4046183137 248784159
12+
2374165196 3613106716 306106357
13+
1553535599 2504868379 49003335
14+
4018850546 3919213073 54344482
15+
2050713919 2287201502 30631432
16+
3183342019 1775307867 31890722
17+
3975551599 2553871714 43298947
18+
1120843626 1342615894 432691973
19+
2330129510 4002147451 44035686
20+
1719545826 2628348978 99579932
21+
1819125758 3381518555 231588161
22+
1627133213 2597170661 31178317
23+
3017459942 2760573310 165882077
24+
3788516154 2317832934 187035445
25+
1602538934 2262607223 24594279
26+
27+
soil-to-fertilizer map:
28+
2037529808 755544791 28492175
29+
786265521 51055407 490038659
30+
4209265112 3740304131 26706989
31+
1490754905 2631438697 34586718
32+
1525341623 2263058012 73974507
33+
1774661286 1921164897 262868522
34+
3007460847 4099468601 72419286
35+
3538443051 3849232192 250236409
36+
1276304180 541094066 214450725
37+
3913003454 3767011120 59428684
38+
1599316130 2497793999 133644698
39+
3079880133 3189897565 426082572
40+
716780020 784036966 69485501
41+
1732960828 0 41700458
42+
2066021983 41700458 9354949
43+
0 853522467 536962937
44+
3972432138 2920584245 236832974
45+
536962937 2337032519 100792490
46+
4272174908 3826439804 22792388
47+
3788679460 3615980137 124323994
48+
2135345922 1446730958 474433939
49+
637755427 2184033419 79024593
50+
2884381438 4171887887 123079409
51+
2075376932 2437825009 59968990
52+
4235972101 2884381438 36202807
53+
2609779861 1390485404 56245554
54+
3505962705 3157417219 32480346
55+
56+
fertilizer-to-water map:
57+
2573961911 2642757339 200829754
58+
422488483 458127884 156893128
59+
2774791665 2843587093 21928567
60+
579381611 0 138939
61+
1694098304 2191562282 184724096
62+
3947192080 3245704277 276559964
63+
972559115 1385376838 44883044
64+
3491468609 3875486597 235886348
65+
1207641311 1359799603 25577235
66+
2301984556 4185647450 96253759
67+
3943275635 3001826942 3916445
68+
901810658 387379427 70748457
69+
285678041 1226326727 103026012
70+
0 775143786 192216167
71+
3304976053 3522264241 95407569
72+
2086394179 1904419031 20334668
73+
2797747442 1694098304 113068417
74+
2398238315 1924753699 175723596
75+
4223752044 2376286378 71215252
76+
192216167 1132864853 93461874
77+
392041619 1329352739 30446864
78+
736305758 967359953 44331476
79+
3740421044 2865515660 136311282
80+
388704053 771806220 3337566
81+
2796720232 4111372945 1027210
82+
3727354957 4281901209 13066087
83+
1017442159 197180275 190199152
84+
1878822400 4112400155 6703986
85+
3400383622 2100477295 91084987
86+
780637234 1011691429 121173424
87+
1885526386 3674618804 200867793
88+
2910815859 3617671810 56946994
89+
2967762853 1807166721 97252310
90+
3065015163 3005743387 239960890
91+
1233218546 138939 197041336
92+
2106728847 2447501630 195255709
93+
579520550 615021012 156785208
94+
3876732326 4119104141 66543309
95+
96+
water-to-light map:
97+
1713604757 2608139445 8097487
98+
416889953 2083343492 58961768
99+
1343227622 1417788674 170490633
100+
2121243443 2549534549 51843959
101+
3366419885 3580448237 174948163
102+
2173087402 0 443149530
103+
3030450490 4139087067 155880229
104+
3186330719 3883823748 51661818
105+
1513718255 1282808215 134980459
106+
1075515973 1815631843 267711649
107+
967716956 443149530 107799017
108+
3237992537 3755396400 128427348
109+
3825109759 3262058246 318389991
110+
879862906 954959732 80165960
111+
6760937 1035125692 247682523
112+
2953403197 3185010953 77047293
113+
960028866 2541846459 7688090
114+
4143499750 3033543407 151467546
115+
2749801696 3935485566 203601501
116+
1648698714 1750725800 64906043
117+
254443460 1588279307 162446493
118+
475851721 550948547 404011185
119+
3541368048 2749801696 283741711
120+
1721702244 2142305260 399541199
121+
0 2601378508 6760937
122+
123+
light-to-temperature map:
124+
72585995 0 6987206
125+
3613700480 3337307014 262222114
126+
3107305066 2641039316 68521519
127+
1346057837 1130104209 332811266
128+
1789578875 2709560835 110508022
129+
1678869103 3335693412 1613602
130+
4221249401 2413595122 73717895
131+
1283830508 2273268756 62227329
132+
2654986608 3645211688 211881713
133+
1900086897 3599529128 45682560
134+
866613618 2487313017 153726299
135+
1945769457 1462915475 224589729
136+
2170359186 2335496085 78099037
137+
1020339917 866613618 263490591
138+
3175826585 3857093401 437873895
139+
1680482705 3226597242 109096170
140+
2866868321 1687505204 240436745
141+
3875922594 1927941949 345326807
142+
2248458223 2820068857 406528385
143+
0 6987206 72585995
144+
145+
temperature-to-humidity map:
146+
688557414 35571309 205276783
147+
1344556852 4212560627 61250968
148+
3617960922 3570339727 35153299
149+
1798596843 4122755682 89804945
150+
968601622 2840504203 289938389
151+
3674269922 3395595703 19024769
152+
2596309702 1911252584 80861179
153+
2438263727 2065549417 13406592
154+
1405807820 1518463561 392789023
155+
617995054 866387965 70562360
156+
1258540011 2078956009 86016841
157+
3395085595 2301904832 149439673
158+
4212818712 3130442592 82148584
159+
1888401788 968601622 549861939
160+
3005925897 2451344505 389159698
161+
3544525268 1992113763 73435654
162+
929405506 858843146 7544819
163+
3868591817 3605493026 188507640
164+
3653114221 4273811595 21155701
165+
893834197 0 35571309
166+
2588602301 3387888302 7707401
167+
2677170881 3794000666 328755016
168+
4057099457 3414620472 155719255
169+
0 240848092 617995054
170+
3693294691 3212591176 175297126
171+
2451670319 2164972850 136931982
172+
173+
humidity-to-location map:
174+
3586928302 2065932610 149219519
175+
709155282 323064563 19167863
176+
1359021687 3937987878 39697029
177+
4009761511 2966667063 138486244
178+
300370292 619673957 122108798
179+
1230327417 992326977 128694270
180+
1524755905 3624589590 105592616
181+
3736147821 2637585432 174385627
182+
1068077767 3730182206 162249650
183+
3120252652 1674438017 80200651
184+
434861301 1581164273 93273744
185+
3489613286 225749547 97315016
186+
2210476726 741782755 111642249
187+
1630348521 1811987842 123678973
188+
1456067890 2488658901 68688015
189+
2890604013 917374342 74952635
190+
2638888586 853425004 63949338
191+
528135045 2557346916 80238516
192+
2440002559 2215152129 198886027
193+
608373561 3105153307 100781721
194+
3910533448 342232426 99228063
195+
4148247755 3892431856 45556022
196+
2702837924 4107201207 187766089
197+
1754027494 1121021247 298601872
198+
2322118975 1948049026 117883584
199+
889864299 441460489 178213468
200+
3200453303 3559871086 36387444
201+
225749547 2414038156 74620745
202+
422479090 1935666815 12382211
203+
728323145 1419623119 161541154
204+
1398718716 1754638668 57349174
205+
2052629366 3977684907 129516300
206+
4193803777 3458707567 101163519
207+
2182145666 3596258530 28331060
208+
2965556648 2811971059 154696004
209+
3236840747 3205935028 252772539
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package advent2023
2+
3+
import org.assertj.core.api.Assertions.assertThat
4+
import org.junit.jupiter.api.Test
5+
6+
class Day05Test {
7+
8+
private val input = Resources.resourceAsText("advent2023/day05.txt")
9+
10+
@Test
11+
fun `test part 1`() {
12+
val sut = Day05(input = input)
13+
assertThat(sut.solvePart1()).isEqualTo(35)
14+
}
15+
16+
@Test
17+
fun `test part 2`() {
18+
val sut = Day05(input = input)
19+
assertThat(sut.solvePart2()).isEqualTo(46)
20+
}
21+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
seeds: 79 14 55 13
2+
3+
seed-to-soil map:
4+
50 98 2
5+
52 50 48
6+
7+
soil-to-fertilizer map:
8+
0 15 37
9+
37 52 2
10+
39 0 15
11+
12+
fertilizer-to-water map:
13+
49 53 8
14+
0 11 42
15+
42 0 7
16+
57 7 4
17+
18+
water-to-light map:
19+
88 18 7
20+
18 25 70
21+
22+
light-to-temperature map:
23+
45 77 23
24+
81 45 19
25+
68 64 13
26+
27+
temperature-to-humidity map:
28+
0 69 1
29+
1 0 69
30+
31+
humidity-to-location map:
32+
60 56 37
33+
56 93 4

0 commit comments

Comments
 (0)