Skip to content

Commit a56b325

Browse files
committed
added 2023/day05
1 parent 2df37d1 commit a56b325

File tree

2 files changed

+295
-0
lines changed

2 files changed

+295
-0
lines changed

2023/day05/input.txt

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
seeds: 1310704671 312415190 1034820096 106131293 682397438 30365957 2858337556 1183890307 665754577 13162298 2687187253 74991378 1782124901 3190497 208902075 226221606 4116455504 87808390 2403629707 66592398
2+
3+
seed-to-soil map:
4+
2879792625 0 201678008
5+
2425309256 1035790247 296756276
6+
2722065532 1759457739 157727093
7+
400354950 1917184832 1164285801
8+
0 201678008 400354950
9+
1564640751 602032958 433757289
10+
1998398040 1332546523 426911216
11+
12+
soil-to-fertilizer map:
13+
3434127746 3670736129 29685965
14+
1809924203 1168707872 308179
15+
2108903682 1437989162 44479258
16+
237181023 2915565442 27901445
17+
1173998623 2434447796 13633544
18+
75539025 740516241 29278225
19+
41104738 706081954 34434287
20+
3279397405 3488165796 12149874
21+
3463813711 3827946213 157129363
22+
1810232382 769794466 15695437
23+
877824710 677909236 28172718
24+
2215709448 1746651561 307558709
25+
1825927819 1692597620 54053941
26+
104817250 420198730 132363773
27+
2916210208 392942051 27256679
28+
1022591555 2448081340 151407068
29+
3925105941 3985075576 182313682
30+
1897186025 2212065968 211717657
31+
2198981202 1304666789 16728246
32+
850656807 2054210270 27167903
33+
3766599721 3500315670 158506220
34+
3419071398 3279397405 15056348
35+
7830088 2126976435 33274650
36+
3620943074 3658821890 11914239
37+
1264213180 2599488408 138420934
38+
811586355 2160251085 12020898
39+
3632857313 3354423388 133742408
40+
1612763314 1169016051 108601184
41+
1721364498 2172271983 39793985
42+
1187632167 601328223 76581013
43+
823607253 1277617235 27049554
44+
728944387 2737909342 82641968
45+
0 2426617708 7830088
46+
3291547279 3700422094 127524119
47+
1402634114 1482468420 210129200
48+
905997428 1321395035 107714902
49+
4107419623 3294453753 59969635
50+
1879981760 785489903 17204265
51+
2153382940 2081378173 45598262
52+
277361019 802694168 366013704
53+
1761158483 552562503 48765720
54+
646208806 2832829861 82735581
55+
2523268157 0 392942051
56+
1013712330 1429109937 8879225
57+
643374723 2423783625 2834083
58+
265082468 2820551310 12278551
59+
60+
fertilizer-to-water map:
61+
4253122607 1473424614 41844689
62+
3040447798 2659805568 46237011
63+
0 146022665 42081460
64+
55436822 188104125 65067713
65+
42081460 132667303 13355362
66+
2429043181 3587614447 54605699
67+
888256662 672288214 24436041
68+
4064969883 1978094070 95324589
69+
3086684809 977403736 339965972
70+
120504535 253171838 93494065
71+
2810558403 2603914183 55891385
72+
3898695123 2901215107 166274760
73+
2483648880 4002918707 103777141
74+
1300545784 2848997109 52217998
75+
2418717938 1463099371 10325243
76+
1022681665 808998429 30429585
77+
2866449788 1411682577 4750813
78+
1181605510 4172708724 118940274
79+
2078503930 2466708865 42530000
80+
1105548530 1545561518 76056980
81+
978705579 2573458117 30456066
82+
2324405069 1317369708 94312869
83+
1991848966 3429793336 22435712
84+
4190586687 2706042579 43180396
85+
1352763782 1416433390 46665981
86+
3760606255 1683093685 138088868
87+
1399429763 3452229048 135385399
88+
2121033930 839428014 137975722
89+
2940673664 2749222975 99774134
90+
1053111250 2073418659 52437280
91+
3426650781 1821182553 152991287
92+
1534815162 2195329002 252024339
93+
730962658 3067489867 157294004
94+
3579642068 710244275 98754154
95+
1786839501 3224783871 205009465
96+
2259009652 1974173840 3920230
97+
2587426021 370264097 223132382
98+
2871200601 2125855939 69473063
99+
213998600 44701447 87965856
100+
4233767083 2447353341 19355524
101+
2262929882 1621618498 61475187
102+
1009161645 696724255 13520020
103+
3678396222 593396479 78891735
104+
912692703 4106695848 66012876
105+
3757287957 4291648998 3318298
106+
301964456 0 44701447
107+
2014284678 2509238865 64219252
108+
370264097 3642220146 360698561
109+
4160294472 1515269303 30292215
110+
111+
water-to-light map:
112+
4066036887 2992193346 95912236
113+
531075515 493316918 162009008
114+
3260565192 854248031 437396028
115+
1341316194 4205924684 89042612
116+
1879858967 2058162578 692895326
117+
452475911 655325926 78599604
118+
2997176790 1690328655 208783332
119+
2731804884 3324847814 265371906
120+
355611136 0 96864775
121+
2572754293 1899111987 159050591
122+
1081338600 3590219720 138271571
123+
1430358806 2779435417 212757929
124+
3234337635 4179697127 26227557
125+
854248031 3728491291 227090569
126+
4161949123 3955581860 102409244
127+
3205960122 2751057904 28377513
128+
50952557 147817332 304658579
129+
1219610171 4057991104 121706023
130+
4264358367 1291644059 30608929
131+
3697961220 1322252988 368075667
132+
1643116735 3088105582 236742232
133+
693084523 452475911 40841007
134+
0 96864775 50952557
135+
136+
light-to-temperature map:
137+
2756401132 2384899493 13749631
138+
1163093625 0 117407544
139+
3603435593 3599927411 262731037
140+
2081436411 2089913126 119300659
141+
693703633 117407544 395383894
142+
1672621164 1405157690 24997208
143+
3873714258 2780774148 107551276
144+
3355072403 2593861641 186912507
145+
1953100586 3862658448 62069331
146+
143286272 672639421 194814248
147+
1562062673 1010739941 110558491
148+
2869050867 2888325424 31673634
149+
3159859886 2398649124 195212517
150+
2900724501 3298674599 34708838
151+
2243940568 4059045429 56605170
152+
691405879 1193483066 2297754
153+
2300545738 2005749676 25248062
154+
3541984910 3924727779 61450683
155+
2200737070 3986178462 43203498
156+
3981265534 2030997738 58915388
157+
2530829166 4276276595 18690701
158+
621411866 641250212 31389209
159+
1784026205 4037549491 21495938
160+
1519774068 1362869085 42288605
161+
3866166630 3584674072 7547628
162+
652801075 1430154898 38604804
163+
2015169917 4029381960 8167531
164+
2770150763 2936555750 98900104
165+
1813227854 2258880377 123316040
166+
3032290681 1784026205 127569205
167+
0 867453669 143286272
168+
1805522143 3592221700 7705711
169+
4040180922 3043888225 254786374
170+
2023337448 3035455854 8432371
171+
3029587605 2382196417 2703076
172+
392553196 1468759702 228858670
173+
2710145863 3538418803 46255269
174+
1089087527 567244114 74006098
175+
2325793800 3333383437 205035366
176+
2549519867 4115650599 160625996
177+
338100520 512791438 54452676
178+
2935433339 1911595410 94154266
179+
1280501169 1121298432 72184634
180+
1352685803 1195780820 167088265
181+
2031769819 2209213785 49666592
182+
1936543894 2919999058 16556692
183+
184+
temperature-to-humidity map:
185+
1606220966 2958863752 268926464
186+
2994413958 1467440292 348583188
187+
1347324773 3453966662 171497865
188+
3342997146 3227790216 188948930
189+
0 211826810 113744983
190+
1875147430 1816023480 774831860
191+
699941162 0 211826810
192+
443679044 325571793 256262118
193+
3531946076 1280528675 186911617
194+
1280528675 4228171198 66796098
195+
113744983 581833911 329934061
196+
1518822638 2590855340 50170812
197+
1568993450 3416739146 37227516
198+
2967816890 4201574130 26597068
199+
3718857693 3625464527 576109603
200+
2649979290 2641026152 317837600
201+
202+
humidity-to-location map:
203+
3244927 955737016 9389705
204+
380524056 2531586403 38604778
205+
3713586211 965126721 158937945
206+
3122843287 1406574654 236795236
207+
776685423 1643369890 534268825
208+
2053493196 0 55930434
209+
582662115 695344450 194023308
210+
3885666529 3855399097 320692779
211+
88096722 283368340 98672354
212+
1901561222 3703467123 151931974
213+
1317500428 2570191181 151780331
214+
3872524156 3690324750 13142373
215+
2109423630 249685414 30437999
216+
1310954248 4199813128 6546180
217+
1751790747 382040694 149770475
218+
3056474029 889367758 66369258
219+
2139861629 4176091876 23721252
220+
12634632 2721971512 75462090
221+
186769076 55930434 193754980
222+
419128834 531811169 163533281
223+
3359638523 2177638715 353947688
224+
2163582881 2797433602 892891148
225+
1469280759 1124064666 282509988
226+
0 280123413 3244927

2023/day05/run.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#! /usr/bin/env python3
2+
3+
def load_data(filename):
4+
with open(filename, 'r') as f:
5+
maps = {}
6+
current_map = []
7+
for line in f:
8+
line = line.rstrip('\n')
9+
if line.startswith('seeds:'):
10+
seeds = list(map(int, line.split(': ')[1].split()))
11+
elif line.endswith(':'):
12+
map_from, _, map_to = line[:-len(" map:")].split('-')
13+
elif line == '':
14+
if len(current_map) > 0:
15+
maps[ (map_from, map_to) ] = current_map
16+
current_map = []
17+
else:
18+
current_map.append( list(map(int, line.split())) )
19+
if len(current_map) > 0:
20+
maps[ (map_from, map_to) ] = current_map
21+
return seeds, maps
22+
23+
# Part One
24+
25+
seeds, maps = load_data('input.txt')
26+
27+
min_location = float('inf')
28+
29+
for value in seeds:
30+
key = 'seed'
31+
while key != 'location':
32+
next_key = [ map_to for map_from, map_to in maps.keys() if map_from == key ][0]
33+
ranges = maps[ (key, next_key) ]
34+
for dst, src, r in ranges:
35+
if src <= value < src + r:
36+
value = dst + value - src
37+
break
38+
else:
39+
pass
40+
key = next_key
41+
min_location = min(min_location, value)
42+
43+
print(min_location)
44+
45+
# Part Two
46+
47+
# pip3 install portion
48+
import portion as P
49+
50+
min_location = float('inf')
51+
52+
for i in range(0, len(seeds), 2):
53+
ranges_from = P.closed(seeds[i], seeds[i] + seeds[i+1] - 1)
54+
key = 'seed'
55+
while key != 'location':
56+
translated = P.empty()
57+
next_key = [ map_to for map_from, map_to in maps.keys() if map_from == key ][0]
58+
ranges = maps[ (key, next_key) ]
59+
for dst, src, r in ranges:
60+
intersection = ranges_from & P.closed(src, src + r - 1)
61+
for interval in intersection:
62+
translated |= P.closed(dst + interval.lower - src, dst + interval.upper - src)
63+
ranges_from -= interval
64+
translated |= ranges_from
65+
key = next_key
66+
ranges_from = translated
67+
min_location = min(min_location, ranges_from.lower)
68+
69+
print(min_location)

0 commit comments

Comments
 (0)