Skip to content

Commit f59594f

Browse files
committed
[AoC 2023 Day 5] Solution 1
1 parent ced9b8f commit f59594f

File tree

2 files changed

+331
-0
lines changed

2 files changed

+331
-0
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package com.rox.adventofcode.y2023
2+
3+
import com.rox.adventofcode.puzzleInputFromFile
4+
import kotlin.math.min
5+
6+
private val inputSample = """
7+
seeds: 79 14 55 13
8+
9+
seed-to-soil map:
10+
50 98 2
11+
52 50 48
12+
13+
soil-to-fertilizer map:
14+
0 15 37
15+
37 52 2
16+
39 0 15
17+
18+
fertilizer-to-water map:
19+
49 53 8
20+
0 11 42
21+
42 0 7
22+
57 7 4
23+
24+
water-to-light map:
25+
88 18 7
26+
18 25 70
27+
28+
light-to-temperature map:
29+
45 77 23
30+
81 45 19
31+
68 64 13
32+
33+
temperature-to-humidity map:
34+
0 69 1
35+
1 0 69
36+
37+
humidity-to-location map:
38+
60 56 37
39+
56 93 4
40+
""".trimIndent()
41+
42+
fun main() {
43+
//println("Sample Input A: ${solutionA(inputSample)}")
44+
//println("Sample Input B: ${solutionB(inputSample)}")
45+
//println("Part A: ${solutionA(puzzleInputFromFile("src/main/kotlin/com/rox/adventofcode/y2023/Day5.input"))}")
46+
println("Part B: ${solutionB(puzzleInputFromFile("src/main/kotlin/com/rox/adventofcode/y2023/Day5.input"))}")
47+
}
48+
49+
data class CategoryConversion(val from: String, val to: String, val mappings: MutableList<RangeAndOffset>){
50+
fun getMappingFor(value: Long): Long {
51+
val mappedValue = mappings.firstOrNull { m -> value in m.range }
52+
if (mappedValue != null) return (value + mappedValue.offset) else return value
53+
}
54+
}
55+
56+
data class RangeAndOffset(val range: LongRange, val offset: Long)
57+
58+
/**
59+
* Answer: ???
60+
*/
61+
private fun solutionA(input: String): Any {
62+
val rows = input.split('\n')
63+
val seedsToPlant = rows[0].substring("seeds: ".length).split(" ").map { i -> i.toLong() }
64+
65+
val categories = mutableMapOf<String, CategoryConversion>()
66+
var parseIndex = 1
67+
for (block in 0 .. 6) {
68+
parseIndex++
69+
val (from, to) = rows[parseIndex++].split(" ")[0].split("-to-")
70+
val mappedRanges = mutableListOf<RangeAndOffset>()
71+
72+
while (parseIndex < rows.size && rows[parseIndex].isNotEmpty()){
73+
val (destination, source, count) = rows[parseIndex++].split(" ").map { entry -> entry.toLong() }
74+
val sourceRange = (source..(source + (count - 1)))
75+
mappedRanges.add(RangeAndOffset(sourceRange, destination - source))
76+
}
77+
78+
categories[from] = CategoryConversion(from, to, mappedRanges)
79+
}
80+
81+
var nearestSeed = Long.MAX_VALUE
82+
for (seed in seedsToPlant) {
83+
var seedProgress = seed
84+
for (stage in listOf("seed", "soil", "fertilizer", "water", "light", "temperature", "humidity")){
85+
seedProgress = categories[stage]?.getMappingFor(seedProgress)!!
86+
println("$stage: $seedProgress")
87+
}
88+
nearestSeed = min(nearestSeed, seedProgress)
89+
}
90+
91+
return nearestSeed
92+
}
93+
94+
/**
95+
*
96+
* Answer: ???
97+
*/
98+
private fun solutionB(input: String): Any {
99+
// val rows = input.split('\n')
100+
// val seedRanges = rows[0].substring("seeds: ".length).split(" ").map { i -> i.toLong() }
101+
// val seedsToPlant = mutableListOf<LongRange>()
102+
// for (i in seedRanges.indices step 2){
103+
// println("$i...")
104+
// seedsToPlant.add(seedRanges[i] until seedRanges[i]+seedRanges[i+1])
105+
// }
106+
//
107+
// val categories = mutableMapOf<String, CategoryConversion>()
108+
// var parseIndex = 1
109+
// for (block in 0 .. 6) {
110+
// parseIndex++
111+
// val (from, to) = rows[parseIndex++].split(" ")[0].split("-to-")
112+
// val mappedRanges = mutableListOf<RangeAndOffset>()
113+
//
114+
// while (parseIndex < rows.size && rows[parseIndex].isNotEmpty()){
115+
// val (destination, source, count) = rows[parseIndex++].split(" ").map { entry -> entry.toLong() }
116+
// val sourceRange = (source..(source + (count - 1)))
117+
// mappedRanges.add(RangeAndOffset(sourceRange, destination - source))
118+
// }
119+
//
120+
// categories[from] = CategoryConversion(from, to, mappedRanges)
121+
// }
122+
//
123+
// var nearestSeed = Long.MAX_VALUE
124+
// for (seed in seedsToPlant) {
125+
// var seedProgress = seed
126+
// for (stage in listOf("seed", "soil", "fertilizer", "water", "light", "temperature", "humidity")){
127+
// seedProgress = categories[stage]?.getMappingFor(seedProgress)!!
128+
// println("$stage: $seedProgress")
129+
// }
130+
// nearestSeed = min(nearestSeed, seedProgress)
131+
// }
132+
//
133+
// return nearestSeed
134+
}
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
seeds: 3127166940 109160474 3265086325 86449584 1581539098 205205726 3646327835 184743451 2671979893 17148151 305618297 40401857 2462071712 203075200 358806266 131147346 1802185716 538526744 635790399 705979250
2+
3+
seed-to-soil map:
4+
931304316 1786548802 232453384
5+
3500539319 2322065235 6421609
6+
496396007 147739714 266329192
7+
3169724489 768672891 39526579
8+
3689153715 1361862036 346985
9+
1936948751 3328259881 542896984
10+
3209251068 3154345676 173914205
11+
1163757700 2814318523 24125066
12+
2484210664 1362209021 231487475
13+
3991904247 2133571422 188493813
14+
1187882766 4045525873 83717994
15+
861951350 3084992710 69352966
16+
2715698139 2838443589 43714032
17+
3830303258 4025104215 20421658
18+
768672891 1268583577 93278459
19+
4180398060 2019002186 114569236
20+
3689500700 1593696496 10659519
21+
1271600760 808199470 460384107
22+
166497091 526585653 102729094
23+
3700160219 3894961176 130143039
24+
2966889400 2882157621 202835089
25+
147739714 414068906 18757377
26+
3850724916 4133608796 141179331
27+
2759412171 2328486844 183672918
28+
2479845735 4129243867 4364929
29+
3480360150 4274788127 20179169
30+
402636637 432826283 93759370
31+
3383165273 2717123646 97194877
32+
3506960928 1604356015 182192787
33+
269226185 629314747 133410452
34+
2943085089 3871156865 23804311
35+
1731984867 2512159762 204963884
36+
37+
soil-to-fertilizer map:
38+
3368312743 826425240 243745914
39+
1045038113 3682756471 174490549
40+
3931158487 1530223690 363808809
41+
1219528662 2460222182 131099318
42+
3020480207 1894032499 63879875
43+
121779694 248970341 36319877
44+
1993634034 2662348686 86667553
45+
3612058657 1323325837 196530127
46+
1531175223 2604354699 57993987
47+
158099571 121779694 127190647
48+
1867147432 3317666386 126486602
49+
2080301587 2768963716 548702670
50+
1402482267 1070171154 21180243
51+
2959841028 4051272297 60639179
52+
834756529 1966243663 128160296
53+
3911211010 2749016239 19947477
54+
962916825 3857247020 82121288
55+
2629004257 3444152988 238603483
56+
826425240 1957912374 8331289
57+
1350627980 3939368308 51854287
58+
1589169210 4214533702 80433594
59+
2867607740 2094403959 92233288
60+
1669602804 1125781209 197544628
61+
3084360082 1519855964 10367726
62+
1483712212 1091351397 34429812
63+
3094727808 2186637247 273584935
64+
1423662510 3991222595 60049702
65+
3808588784 4111911476 102622226
66+
1518142024 2591321500 13033199
67+
68+
fertilizer-to-water map:
69+
206818393 1973789958 18543481
70+
2641351404 1992333439 41420268
71+
58400970 2574944960 107826712
72+
3710426911 4065366707 42793360
73+
4217161704 4274048011 20919285
74+
1926695368 705931711 328031436
75+
1449580741 1210970895 50549447
76+
907984567 1421828853 15115545
77+
769748018 1108192216 102778679
78+
451427938 35457870 38201654
79+
2254726804 2033892789 137829519
80+
923239194 1513967644 270588891
81+
3753220271 4108160067 165887944
82+
499804857 310274559 109862756
83+
3061525238 3535532059 426476055
84+
1193828085 73659524 196024324
85+
872526697 0 35457870
86+
1766386857 1261520342 160308511
87+
4057593930 3283950856 159567774
88+
1389852409 646203379 59728332
89+
3919108215 3962008114 103358593
90+
1577153434 1784556535 189233423
91+
4022466808 3443518630 35127122
92+
489629592 1098016951 10175265
93+
923100112 2033753707 139082
94+
2392556323 2390203683 158894869
95+
1500130188 1436944398 77023246
96+
2577297600 1033963147 64053804
97+
609667613 2171722308 160080405
98+
3488001293 3061525238 222425618
99+
2551451192 2549098552 25846408
100+
4238080989 3478645752 56886307
101+
166227682 269683848 40590711
102+
0 2331802713 58400970
103+
225361874 420137315 226066064
104+
105+
water-to-light map:
106+
1833244152 0 764535859
107+
212138399 2132863085 224047237
108+
445686952 1600446740 163005122
109+
3322180377 2914685303 488586806
110+
2739726430 3712513349 582453947
111+
3946546331 3589340640 8839399
112+
1441711040 799272484 245821386
113+
1038755613 1763451862 6623730
114+
608692074 1587251997 13194743
115+
701103180 2356910322 39153476
116+
1687532426 1045093870 145711726
117+
2597780011 764535859 34736625
118+
740256656 1490869662 54307168
119+
0 1920724686 212138399
120+
2632516636 1545176830 9229765
121+
668257778 1554406595 32845402
122+
3955385730 2739726430 39179725
123+
4180633986 3598180039 114333310
124+
3810767183 2778906155 135779148
125+
1291061946 1770075592 150649094
126+
436185636 1481368346 9501316
127+
1045379343 2396063798 245682603
128+
794563824 1237176557 244191789
129+
621886817 1190805596 46370961
130+
3994565455 3403272109 186068531
131+
132+
light-to-temperature map:
133+
432141642 1268486741 19474646
134+
3617581823 3276436954 357008111
135+
3505110084 3786131308 49942802
136+
0 1287961387 432141642
137+
3096011130 1808659179 409098954
138+
1347993824 2675880000 161612192
139+
3019335150 3199760974 76675980
140+
3555052886 3137232037 62528937
141+
2778092757 1720103029 88556150
142+
451616288 2217758133 458121867
143+
1509606016 0 1268486741
144+
909738155 3836074110 138515824
145+
1048253979 2837492192 299739845
146+
2866648907 3633445065 152686243
147+
148+
temperature-to-humidity map:
149+
646729740 1519504972 559297346
150+
1894539176 2990410634 44298872
151+
232257988 972432123 414471752
152+
2277879451 278205785 108711195
153+
1775790220 132298732 118748956
154+
3371687162 2663455233 326955401
155+
1612056920 272509895 5695890
156+
1208383109 3703499740 147415518
157+
4070380190 4053129082 69974785
158+
4155541210 3305585510 139426086
159+
81956384 386916980 150301604
160+
3987543096 896459472 75972651
161+
2148980475 1386903875 128898976
162+
1617752810 3445011596 154599732
163+
4063515747 2078802318 6864443
164+
2392568787 3599611328 101532389
165+
2386590646 4123103867 5978141
166+
2494101176 2122546980 187027686
167+
2681128862 2085666761 36880219
168+
4140354975 2648268998 15186235
169+
1772352542 3051742077 3437678
170+
1355798627 3850915258 202213824
171+
3720104770 3055179755 250405755
172+
3032992830 2309574666 338694332
173+
1206027086 3701143717 2356023
174+
1938838048 537218584 44257139
175+
1558012451 81956384 50342348
176+
3970510525 3034709506 17032571
177+
1608354799 1515802851 3702121
178+
1983095187 4129082008 165885288
179+
3698642563 251047688 21462207
180+
2718009081 581475723 314983749
181+
182+
humidity-to-location map:
183+
971626884 4275486551 19480745
184+
1218249913 2090555906 502249162
185+
2914848039 2902831882 224865747
186+
3341591733 2819947352 82884530
187+
991107629 2592805068 227142284
188+
3424476263 606585628 95279547
189+
4279176998 2064757318 10971709
190+
3139713786 4068790015 201877947
191+
606585628 701865175 365041256
192+
3534582689 3291885426 744594309
193+
1916997152 1066906431 997850887
194+
1752809355 3127697629 164187797
195+
1720499075 4036479735 32310280
196+
4290148707 4270667962 4818589
197+
3519755810 2075729027 14826879

0 commit comments

Comments
 (0)