generated from public/aoc_template
Day 5 - brute force edition
This commit is contained in:
parent
de449fb682
commit
16b657786a
97
day05.py
Normal file
97
day05.py
Normal file
@ -0,0 +1,97 @@
|
||||
from tools.aoc import AOCDay
|
||||
from typing import Any
|
||||
|
||||
|
||||
def lookup(the_map: dict, source: int) -> int:
|
||||
for k, v in the_map.items():
|
||||
if k <= source < k + v['length']:
|
||||
return source - k + v['dest']
|
||||
return source
|
||||
|
||||
|
||||
class Day(AOCDay):
|
||||
inputs = [
|
||||
[
|
||||
(35, "input5_test"),
|
||||
(346433842, "input5"),
|
||||
],
|
||||
[
|
||||
(46, "input5_test"),
|
||||
(60294664, "input5"),
|
||||
]
|
||||
]
|
||||
|
||||
def __init__(self, year: int, day: int):
|
||||
super().__init__(year, day)
|
||||
self.seed_locations = {}
|
||||
|
||||
def get_seed_location(self, seed: int) -> int:
|
||||
soil = lookup(self.seed2soil, seed)
|
||||
fertilizer = lookup(self.soil2fertilizer, soil)
|
||||
water = lookup(self.fertilizer2water, fertilizer)
|
||||
light = lookup(self.water2light, water)
|
||||
temperature = lookup(self.light2temperature, light)
|
||||
humidity = lookup(self.temperature2humidity, temperature)
|
||||
location = lookup(self.humidity2location, humidity)
|
||||
return location
|
||||
|
||||
def parse_input(self, part2: bool = False) -> list:
|
||||
seeds = []
|
||||
self.seed2soil = {}
|
||||
self.soil2fertilizer = {}
|
||||
self.fertilizer2water = {}
|
||||
self.water2light = {}
|
||||
self.light2temperature = {}
|
||||
self.temperature2humidity = {}
|
||||
self.humidity2location = {}
|
||||
the_map = None
|
||||
for line in self.getInput():
|
||||
if not line:
|
||||
continue
|
||||
elif line.startswith('seeds:'):
|
||||
_, s = line.split(": ")
|
||||
seeds = list(map(int, s.split()))
|
||||
elif line.startswith('seed-to-soil'):
|
||||
the_map = self.seed2soil
|
||||
elif line.startswith('soil-to-fertilizer'):
|
||||
the_map = self.soil2fertilizer
|
||||
elif line.startswith('fertilizer-to-water'):
|
||||
the_map = self.fertilizer2water
|
||||
elif line.startswith('water-to-light'):
|
||||
the_map = self.water2light
|
||||
elif line.startswith('light-to-temperature'):
|
||||
the_map = self.light2temperature
|
||||
elif line.startswith('temperature-to-humidity'):
|
||||
the_map = self.temperature2humidity
|
||||
elif line.startswith('humidity-to-location'):
|
||||
the_map = self.humidity2location
|
||||
else:
|
||||
dest, source, length = map(int, line.split())
|
||||
the_map[source] = {'dest': dest, 'length': length}
|
||||
|
||||
if part2:
|
||||
self.seed_locations = {}
|
||||
|
||||
|
||||
return seeds
|
||||
|
||||
def part1(self) -> Any:
|
||||
seeds = self.parse_input()
|
||||
return min(self.get_seed_location(s) for s in seeds)
|
||||
|
||||
def part2(self) -> Any:
|
||||
seeds = self.parse_input()
|
||||
min_loc = 2**32
|
||||
for i in range(0, len(seeds), 2):
|
||||
for s in range(seeds[i], seeds[i] + seeds[i+1]):
|
||||
x = self.get_seed_location(s)
|
||||
if x < min_loc:
|
||||
min_loc = x
|
||||
print(i, "=>", min_loc)
|
||||
|
||||
return min_loc
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
day = Day(2023, 5)
|
||||
day.run(verbose=True)
|
||||
206
inputs/input5
Normal file
206
inputs/input5
Normal file
@ -0,0 +1,206 @@
|
||||
seeds: 919339981 562444630 3366006921 67827214 1496677366 101156779 4140591657 5858311 2566406753 71724353 2721360939 35899538 383860877 424668759 3649554897 442182562 2846055542 49953829 2988140126 256306471
|
||||
|
||||
seed-to-soil map:
|
||||
627617777 1691901751 235673208
|
||||
2425244517 2483951770 157286279
|
||||
1339042890 1549225044 142676707
|
||||
481294110 381503165 89539853
|
||||
863290985 1007717708 39103521
|
||||
570833963 324719351 56783814
|
||||
3953140805 3714151881 155523737
|
||||
902394506 1941176275 61481385
|
||||
963875891 675869083 331848625
|
||||
1922840702 1046821229 502403815
|
||||
1481719597 1927574959 13601316
|
||||
1820040264 573068645 102800438
|
||||
1295724516 2641238049 43318374
|
||||
0 2002657660 481294110
|
||||
1495320913 0 324719351
|
||||
2582530796 471043018 102025627
|
||||
3714151881 3869675618 238988924
|
||||
|
||||
soil-to-fertilizer map:
|
||||
1288462652 3191328122 309853381
|
||||
3216116191 1774097401 151922673
|
||||
1739360920 2276789875 44162492
|
||||
220941763 1080382821 325206789
|
||||
2416141229 3949354107 345613189
|
||||
1783523412 765967805 117439234
|
||||
2876214366 1010215130 70167691
|
||||
2761754418 3142514478 48813644
|
||||
3840090062 3541735844 44042641
|
||||
2946382057 1417076798 106871618
|
||||
3750331405 3501181503 29948437
|
||||
1105413398 1405589610 11487188
|
||||
2867709608 3585778485 8504758
|
||||
3053253675 3594283243 162862516
|
||||
15940804 0 90040993
|
||||
2192246779 537548320 223894450
|
||||
210335859 3531129940 10605904
|
||||
546148552 2351877346 559264846
|
||||
1598316033 883407039 110119908
|
||||
2851021425 993526947 16688183
|
||||
0 90040993 15940804
|
||||
4134281688 3757145759 160685608
|
||||
3718808665 3917831367 31522740
|
||||
1116900586 2970952412 171562066
|
||||
2810568062 210335859 40453363
|
||||
3780279842 2911142192 59810220
|
||||
1900962646 761442770 4525035
|
||||
1905487681 250789222 286759098
|
||||
3368038864 1926020074 350769801
|
||||
1708435941 2320952367 30924979
|
||||
3884132703 1523948416 250148985
|
||||
|
||||
fertilizer-to-water map:
|
||||
2450598719 3993777626 178688420
|
||||
990387307 2284751995 36035279
|
||||
1479519873 1430606124 25388869
|
||||
2928263979 2233553333 51198662
|
||||
1825810145 1093816339 30855096
|
||||
3131362216 1499369679 38622365
|
||||
2629287139 1980229176 243592551
|
||||
2979462641 1827354243 146328983
|
||||
937194740 3188384434 9817881
|
||||
2069360741 1973683226 975358
|
||||
1856665241 3781082126 212695500
|
||||
642830371 945238436 148577903
|
||||
4114154511 1124671435 95554104
|
||||
3777484412 483834992 305990165
|
||||
3264059981 2427531551 296117669
|
||||
272170557 2723649220 370659814
|
||||
262438951 2223821727 9731606
|
||||
1615429560 1220225539 210380585
|
||||
1028349507 226445100 83554339
|
||||
4209708615 3198202315 85258681
|
||||
3169984581 3094309034 94075400
|
||||
4083474577 309999439 30679934
|
||||
2872879690 115495593 55384289
|
||||
2138362191 340679373 4164957
|
||||
947012621 1455994993 43374686
|
||||
791408274 1606018136 145786466
|
||||
1026422586 1825427322 1926921
|
||||
1578531462 789825157 8469921
|
||||
3670740135 2320787274 106744277
|
||||
2272532251 170879882 55565218
|
||||
3125791624 1974658584 5570592
|
||||
1111903846 3283460996 367616027
|
||||
2070336099 1537992044 68026092
|
||||
1504908742 1751804602 73622720
|
||||
2142527148 3651077023 130005103
|
||||
2328097469 4172466046 122501250
|
||||
1587001383 344844330 28428177
|
||||
3560177650 373272507 110562485
|
||||
115495593 798295078 146943358
|
||||
|
||||
water-to-light map:
|
||||
220561404 643114856 123713527
|
||||
1087373312 4123526389 171440907
|
||||
0 766828383 220561404
|
||||
2907464644 3955968868 167557521
|
||||
376259478 31984547 611130309
|
||||
344274931 0 31984547
|
||||
4209967986 1266450159 84999310
|
||||
3234177104 1246528251 19921908
|
||||
4075482989 2729348487 134484997
|
||||
1258814219 1870560966 858787521
|
||||
3075022165 1087373312 159154939
|
||||
2439602258 3906331676 49637192
|
||||
2297013316 3685217461 142588942
|
||||
2567764723 1530861045 339699921
|
||||
3254099012 2863833484 821383977
|
||||
2489239450 3827806403 78525273
|
||||
2117601740 1351449469 179411576
|
||||
|
||||
light-to-temperature map:
|
||||
83647742 560398800 311449275
|
||||
4201716419 3337900402 93250877
|
||||
1071565427 3024001249 4392221
|
||||
3424420254 3520028211 161472091
|
||||
1227070419 2828368402 116428819
|
||||
0 2744720660 83647742
|
||||
4199359339 3431151279 2357080
|
||||
1807547949 2359072262 384093366
|
||||
3336003529 3486377667 33650544
|
||||
1075957648 871848075 149557739
|
||||
3585892345 3681500302 613466994
|
||||
955495817 2243002652 116069610
|
||||
2191641315 1406250497 836752155
|
||||
3369654073 3433508359 52869308
|
||||
1343499238 2944797221 79204028
|
||||
3422523381 3336003529 1896873
|
||||
395097017 0 560398800
|
||||
1225515387 2743165628 1555032
|
||||
1422703266 1021405814 384844683
|
||||
|
||||
temperature-to-humidity map:
|
||||
3340701300 3627322367 174281926
|
||||
3514983226 4012096602 282870694
|
||||
403969772 2344934648 125307793
|
||||
356249525 1299750763 47720247
|
||||
529277565 0 19150241
|
||||
2425473363 606907612 15105367
|
||||
1622667408 1693081570 188915118
|
||||
1858084350 1347471010 47053630
|
||||
4223873328 2752952198 71093968
|
||||
2749048741 3035669808 188955792
|
||||
0 2134178305 190659232
|
||||
1112200615 96440819 510466793
|
||||
4148180263 2749048741 3903457
|
||||
1811582526 1113827741 11131074
|
||||
548427806 2324837537 20097111
|
||||
1905137980 2051821050 82357255
|
||||
3116507691 3224625600 224193609
|
||||
1822713600 1657710820 35370750
|
||||
568524917 622012979 491814762
|
||||
2440578730 66777108 29663711
|
||||
2250681415 1124958815 174791948
|
||||
4009477562 3873393901 138702701
|
||||
190659232 1881996688 165590293
|
||||
1987495235 1394524640 263186180
|
||||
1107966546 2047586981 4234069
|
||||
1060339679 19150241 47626867
|
||||
4210822392 3801604293 13050936
|
||||
2938004533 3448819209 178503158
|
||||
3797853920 2824046166 211623642
|
||||
4152083720 3814655229 58738672
|
||||
|
||||
humidity-to-location map:
|
||||
2102802203 2756269781 87468599
|
||||
2877112183 2931341663 22549647
|
||||
3494879649 1477788233 182041959
|
||||
2436026725 2953891310 81993792
|
||||
2899661830 373999642 244663414
|
||||
2190270802 850802545 169763790
|
||||
3866824018 90439080 13573105
|
||||
320139638 1659830192 113214403
|
||||
1131501111 1773044595 246249128
|
||||
2417984929 828638896 16505804
|
||||
3839766923 845144700 5657845
|
||||
4161134012 2843738380 87603283
|
||||
3731370785 3801964390 108396138
|
||||
1003659815 3442085055 127841296
|
||||
918426975 3986208955 85232840
|
||||
433354041 618663056 209975840
|
||||
2811371438 0 65740745
|
||||
2518020517 1329719948 148068285
|
||||
651651860 3035885102 266775115
|
||||
0 2256642811 320139638
|
||||
2666088802 228717006 145282636
|
||||
4136435677 65740745 24698335
|
||||
3144325244 1046598285 188874270
|
||||
1657153777 2576782449 179487332
|
||||
1844732214 3569926351 232038039
|
||||
643329881 2077244060 8321979
|
||||
1471997632 4071441795 175759508
|
||||
2076770253 1020566335 26031950
|
||||
3676921608 3931759778 54449177
|
||||
3997010839 3302660217 139424838
|
||||
3845424768 3910360528 21399250
|
||||
1836641109 104012185 8091105
|
||||
3333199514 2085566039 161680135
|
||||
3880397123 112103290 116613716
|
||||
2360034592 2019293723 57950337
|
||||
1377750239 1235472555 94247393
|
||||
1647757140 2247246174 9396637
|
||||
2434490733 4247201303 1535992
|
||||
33
inputs/input5_test
Normal file
33
inputs/input5_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 New Issue
Block a user