5 changed files with 498 additions and 2 deletions
@ -0,0 +1,121 @@ |
|||
--- Day 5: If You Give A Seed A Fertilizer --- |
|||
|
|||
You take the boat and find the gardener right where you were told he would be: managing a giant "garden" that looks more to you like a farm. |
|||
|
|||
"A water source? Island Island is the water source!" You point out that Snow Island isn't receiving any water. |
|||
|
|||
"Oh, we had to stop the water because we ran out of sand to filter it with! Can't make snow with dirty water. Don't worry, I'm sure we'll get more sand soon; we only turned off the water a few days... weeks... oh no." His face sinks into a look of horrified realization. |
|||
|
|||
"I've been so busy making sure everyone here has food that I completely forgot to check why we stopped getting more sand! There's a ferry leaving soon that is headed over in that direction - it's much faster than your boat. Could you please go check it out?" |
|||
|
|||
You barely have time to agree to this request when he brings up another. "While you wait for the ferry, maybe you can help us with our food production problem. The latest Island Island Almanac just arrived and we're having trouble making sense of it." |
|||
|
|||
The almanac (your puzzle input) lists all of the seeds that need to be planted. It also lists what type of soil to use with each kind of seed, what type of fertilizer to use with each kind of soil, what type of water to use with each kind of fertilizer, and so on. Every type of seed, soil, fertilizer and so on is identified with a number, but numbers are reused by each category - that is, soil 123 and fertilizer 123 aren't necessarily related to each other. |
|||
|
|||
For example: |
|||
|
|||
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 |
|||
|
|||
The almanac starts by listing which seeds need to be planted: seeds 79, 14, 55, and 13. |
|||
|
|||
The rest of the almanac contains a list of maps which describe how to convert numbers from a source category into numbers in a destination category. That is, the section that starts with seed-to-soil map: describes how to convert a seed number (the source) to a soil number (the destination). This lets the gardener and his team know which soil to use with which seeds, which water to use with which fertilizer, and so on. |
|||
|
|||
Rather than list every source number and its corresponding destination number one by one, the maps describe entire ranges of numbers that can be converted. Each line within a map contains three numbers: the destination range start, the source range start, and the range length. |
|||
|
|||
Consider again the example seed-to-soil map: |
|||
|
|||
50 98 2 |
|||
52 50 48 |
|||
|
|||
The first line has a destination range start of 50, a source range start of 98, and a range length of 2. This line means that the source range starts at 98 and contains two values: 98 and 99. The destination range is the same length, but it starts at 50, so its two values are 50 and 51. With this information, you know that seed number 98 corresponds to soil number 50 and that seed number 99 corresponds to soil number 51. |
|||
|
|||
The second line means that the source range starts at 50 and contains 48 values: 50, 51, ..., 96, 97. This corresponds to a destination range starting at 52 and also containing 48 values: 52, 53, ..., 98, 99. So, seed number 53 corresponds to soil number 55. |
|||
|
|||
Any source numbers that aren't mapped correspond to the same destination number. So, seed number 10 corresponds to soil number 10. |
|||
|
|||
So, the entire list of seed numbers and their corresponding soil numbers looks like this: |
|||
|
|||
seed soil |
|||
0 0 |
|||
1 1 |
|||
... ... |
|||
48 48 |
|||
49 49 |
|||
50 52 |
|||
51 53 |
|||
... ... |
|||
96 98 |
|||
97 99 |
|||
98 50 |
|||
99 51 |
|||
|
|||
With this map, you can look up the soil number required for each initial seed number: |
|||
|
|||
Seed number 79 corresponds to soil number 81. |
|||
Seed number 14 corresponds to soil number 14. |
|||
Seed number 55 corresponds to soil number 57. |
|||
Seed number 13 corresponds to soil number 13. |
|||
|
|||
The gardener and his team want to get started as soon as possible, so they'd like to know the closest location that needs a seed. Using these maps, find the lowest location number that corresponds to any of the initial seeds. To do this, you'll need to convert each seed number through other categories until you can find its corresponding location number. In this example, the corresponding types are: |
|||
|
|||
Seed 79, soil 81, fertilizer 81, water 81, light 74, temperature 78, humidity 78, location 82. |
|||
Seed 14, soil 14, fertilizer 53, water 49, light 42, temperature 42, humidity 43, location 43. |
|||
Seed 55, soil 57, fertilizer 57, water 53, light 46, temperature 82, humidity 82, location 86. |
|||
Seed 13, soil 13, fertilizer 52, water 41, light 34, temperature 34, humidity 35, location 35. |
|||
|
|||
So, the lowest location number in this example is 35. |
|||
|
|||
What is the lowest location number that corresponds to any of the initial seed numbers? |
|||
|
|||
Your puzzle answer was 178159714. |
|||
--- Part Two --- |
|||
|
|||
Everyone will starve if you only plant such a small number of seeds. Re-reading the almanac, it looks like the seeds: line actually describes ranges of seed numbers. |
|||
|
|||
The values on the initial seeds: line come in pairs. Within each pair, the first value is the start of the range and the second value is the length of the range. So, in the first line of the example above: |
|||
|
|||
seeds: 79 14 55 13 |
|||
|
|||
This line describes two ranges of seed numbers to be planted in the garden. The first range starts with seed number 79 and contains 14 values: 79, 80, ..., 91, 92. The second range starts with seed number 55 and contains 13 values: 55, 56, ..., 66, 67. |
|||
|
|||
Now, rather than considering four seed numbers, you need to consider a total of 27 seed numbers. |
|||
|
|||
In the above example, the lowest location number can be obtained from seed number 82, which corresponds to soil 84, fertilizer 84, water 84, light 77, temperature 45, humidity 46, and location 46. So, the lowest location number is 46. |
|||
|
|||
Consider all of the initial seed numbers listed in the ranges on the first line of the almanac. What is the lowest location number that corresponds to any of the initial seed numbers? |
|||
|
|||
Your puzzle answer was 100165128. |
|||
|
|||
Both parts of this puzzle are complete! They provide two gold stars: ** |
@ -0,0 +1,83 @@ |
|||
#!/usr/bin/env python3 |
|||
|
|||
import sys, re |
|||
|
|||
|
|||
def key_dest(key, mp): |
|||
for src, (length,dest) in mp.items(): |
|||
if key in range(src, src+length): |
|||
return dest+(key-src) |
|||
return key |
|||
|
|||
|
|||
def rang_dest(rang, mp): |
|||
sols = [] |
|||
for (key_s, key_l) in rang: |
|||
for src, (length,dest) in dict(sorted(mp.items())).items(): |
|||
if key_s in range(src,src+length): |
|||
if key_s + key_l -1 in range(src,src+length): |
|||
sols += [(dest-src+key_s,key_l)] |
|||
key_l = 0 |
|||
break |
|||
else: |
|||
sols += [(dest-src+key_s, src+length-key_s)] |
|||
key_l -= (length + src - key_s) |
|||
key_s = src+length |
|||
if key_l > 0: |
|||
sols += [(key_s, key_l)] |
|||
return list(set(sols)) |
|||
|
|||
|
|||
if __name__ == '__main__': |
|||
lines = [line.strip('\n') for line in open(sys.argv[1])] |
|||
seeds = [int(n) for n in re.findall(r'(\d+)',lines[0])] |
|||
maps = { |
|||
'seed-to-soil': {}, |
|||
'soil-to-fertilizer': {}, |
|||
'fertilizer-to-water': {}, |
|||
'water-to-light': {}, |
|||
'light-to-temperature': {}, |
|||
'temperature-to-humidity': {}, |
|||
'humidity-to-location': {}} |
|||
|
|||
key = '' |
|||
for line in lines[2:]: |
|||
if re.match(r'(.*) map:', line): |
|||
key = re.search(r'(?P<name>.*) map:', line).group('name') |
|||
elif re.match(r'\d+', line): |
|||
dest, src, length = [int(i) for i in re.findall(r'\d+',line)] |
|||
maps[key][src] = (length,dest) |
|||
res1 = 10000000000000000 |
|||
for seed in seeds: |
|||
soil = key_dest(seed, maps['seed-to-soil']) |
|||
fert = key_dest(soil, maps['soil-to-fertilizer']) |
|||
water = key_dest(fert, maps['fertilizer-to-water']) |
|||
light = key_dest(water, maps['water-to-light']) |
|||
temp = key_dest(light, maps['light-to-temperature']) |
|||
hum = key_dest(temp, maps['temperature-to-humidity']) |
|||
loc = key_dest(hum, maps['humidity-to-location']) |
|||
if loc < res1: res1 = loc |
|||
|
|||
res2 = 10000000000000000 |
|||
for i in range(int(len(seeds)/2)): |
|||
src, length = seeds[i*2], seeds[i*2+1] |
|||
seed = [(src,length)] |
|||
soil = rang_dest(seed, maps['seed-to-soil']) |
|||
fert = rang_dest(soil, maps['soil-to-fertilizer']) |
|||
water = rang_dest(fert, maps['fertilizer-to-water']) |
|||
light = rang_dest(water, maps['water-to-light']) |
|||
temp = rang_dest(light, maps['light-to-temperature']) |
|||
hum = rang_dest(temp, maps['temperature-to-humidity']) |
|||
loc = rang_dest(hum, maps['humidity-to-location']) |
|||
for i,_ in loc: |
|||
if i < res2: res2 = i |
|||
|
|||
|
|||
# challenge 1 |
|||
res1 = str(res1) |
|||
print(f"challenge 1:\n{res1}\n") |
|||
|
|||
# challenge 2 |
|||
res2 = str(res2) |
|||
print(f"challenge 2:\n{res2}\n") |
|||
|
@ -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 |
@ -0,0 +1,258 @@ |
|||
seeds: 1347397244 12212989 2916488878 1034516675 2821376423 8776260 2240804122 368941186 824872000 124877531 1597965637 36057332 4091290431 159289722 1875817275 106230212 998513229 159131132 2671581775 4213184 |
|||
|
|||
seed-to-soil map: |
|||
2988689842 4194451945 100515351 |
|||
2936009234 3353543976 52680608 |
|||
588295233 2638661119 66434163 |
|||
3932833115 2936009234 88315480 |
|||
3525561241 3331695912 21848064 |
|||
1622262003 1969921080 210668061 |
|||
2160566101 909457337 162053391 |
|||
1832930064 1887384181 82536899 |
|||
3625461917 3024324714 307371198 |
|||
3547409305 3680043285 78052612 |
|||
1915466963 588295233 240773057 |
|||
3089205193 3758095897 436356048 |
|||
4021148595 3406224584 273818701 |
|||
2156240020 2705095282 4326081 |
|||
1164190025 2180589141 458071978 |
|||
2477360206 829068290 80389047 |
|||
2322619492 2709421363 154740714 |
|||
654729396 1377923552 509460629 |
|||
2557749253 1211672006 166251546 |
|||
2724000799 1071510728 140161278 |
|||
|
|||
soil-to-fertilizer map: |
|||
3961802244 3774724750 90737174 |
|||
3164426550 3931513861 70563571 |
|||
147221566 1279409424 704464 |
|||
1394834067 2074132435 40845148 |
|||
3795834030 2142537807 47621185 |
|||
4083197470 4095560143 199407153 |
|||
2722903919 2876212954 93296050 |
|||
3467494732 2775293966 100918988 |
|||
1809650294 1815421878 66426374 |
|||
505665614 275280169 12031240 |
|||
2142537807 4002077432 60985377 |
|||
1577608496 331482268 177958690 |
|||
2590855103 2196397738 132048816 |
|||
1942888942 1978207624 24340152 |
|||
756722275 120895382 4815243 |
|||
3435289775 3899308904 32204957 |
|||
1967229094 715059087 147748489 |
|||
3955563498 2190158992 6238746 |
|||
356096070 125710625 149569544 |
|||
2520387506 2704826369 70467597 |
|||
517696854 2041922514 32209921 |
|||
1755567186 0 54083108 |
|||
348569226 1280113888 7526844 |
|||
761537518 1597328127 34415327 |
|||
1114796271 1631743454 128592301 |
|||
0 1494456232 102871895 |
|||
1070625412 287311409 44170859 |
|||
549906775 1287640732 206815500 |
|||
102871895 710084154 4974933 |
|||
1298474695 1881848252 96359372 |
|||
3843455215 2549697361 112108283 |
|||
3761987050 3865461924 33846980 |
|||
4052539418 2674168317 30658052 |
|||
1435679215 862807576 141929281 |
|||
795952845 1004736857 274672567 |
|||
3729489716 4063062809 32497334 |
|||
2203523184 3085216895 316864322 |
|||
2931907860 3402081217 11267883 |
|||
107846828 2002547776 39374738 |
|||
2816199969 2969509004 115707891 |
|||
3568413720 3413349100 161075996 |
|||
2943175743 2328446554 221250807 |
|||
3234990121 3574425096 200299654 |
|||
4282604623 2661805644 12362673 |
|||
147926030 509440958 200643196 |
|||
1876076668 54083108 66812274 |
|||
1243388572 1760335755 55086123 |
|||
|
|||
fertilizer-to-water map: |
|||
2460553918 850437816 63304366 |
|||
1259757436 1986466040 193004355 |
|||
2879827793 2638634287 61837387 |
|||
39629536 0 3143529 |
|||
2160922553 2535779758 68016930 |
|||
2523858284 922523353 36811379 |
|||
52449107 1199799263 207670511 |
|||
2692884203 2603796688 34837599 |
|||
3755186617 147251641 492169035 |
|||
3266515480 3620937477 292130997 |
|||
1596851845 4077877285 217090011 |
|||
2727721802 913742182 8781171 |
|||
3055087322 3913068474 164808811 |
|||
2228939483 2179470395 229525704 |
|||
3668162112 1052896909 87024505 |
|||
3219896133 1539684314 30399976 |
|||
1510550909 1679599925 86300936 |
|||
0 3143529 39629536 |
|||
2560669663 1407469774 132214540 |
|||
1452761791 1142010145 57789118 |
|||
260119618 2700471674 920465803 |
|||
3250296109 1036677538 16219371 |
|||
1813941856 1026924408 9753130 |
|||
4247355652 2488168114 47611644 |
|||
1823694986 959334732 67589676 |
|||
2831305507 639420676 48522286 |
|||
2736502973 52449107 94802534 |
|||
1180585421 2408996099 79172015 |
|||
2941665180 1765900861 113422142 |
|||
2458465187 1139921414 2088731 |
|||
3558646477 1570084290 109515635 |
|||
1891284662 687942962 162494854 |
|||
2053779516 1879323003 107143037 |
|||
|
|||
water-to-light map: |
|||
2196302869 3170532562 121192468 |
|||
3065704582 2916528129 254004433 |
|||
2858667310 1154274853 9085577 |
|||
3789349818 1163360430 70779786 |
|||
2064226029 1434838179 90165206 |
|||
1448515654 725716988 103420445 |
|||
2690533041 2124509945 168134269 |
|||
347894075 3345882022 38285799 |
|||
3966625235 2618593488 35838159 |
|||
4186823059 4134088017 89981817 |
|||
1701108140 1088713231 65561622 |
|||
2589948930 2518009377 100584111 |
|||
4283393470 4230658428 11573826 |
|||
0 3384167821 347894075 |
|||
1638541363 2061943168 62566777 |
|||
1296314418 573515752 152201236 |
|||
1065865126 194284013 230449292 |
|||
1766669762 1991838394 70104774 |
|||
830054797 2292644214 110248534 |
|||
3595088014 3291725030 12245358 |
|||
1978385607 1905997972 85840422 |
|||
3607333372 424733305 148782447 |
|||
3511869770 2833309885 83218244 |
|||
4134088017 4242232254 52735042 |
|||
2154391235 3303970388 41911634 |
|||
494720868 3732061896 335333929 |
|||
1836774536 1234140216 141611071 |
|||
2317495337 1633544379 272453593 |
|||
4002463394 2402892748 64932431 |
|||
940303331 829137433 125561795 |
|||
1551936099 954699228 27518372 |
|||
4276804876 4224069834 6588594 |
|||
3756115819 2467825179 33233999 |
|||
2867752887 0 19073457 |
|||
386179874 1525003385 108540994 |
|||
1579454471 1375751287 59086892 |
|||
3319709015 2501059178 16950199 |
|||
3336659214 19073457 175210556 |
|||
3860129604 982217600 106495631 |
|||
2886826344 2654431647 178878238 |
|||
|
|||
light-to-temperature map: |
|||
977891457 1797846421 453265654 |
|||
3607226990 3913974738 161345346 |
|||
2303244644 3266224873 12707372 |
|||
1537599301 3278932245 264559714 |
|||
354466514 3168465761 62294113 |
|||
747844586 3543491959 55668994 |
|||
2982698313 3599160953 269886589 |
|||
2067998119 2251112075 27763866 |
|||
59336731 230685734 266868096 |
|||
3768572336 1500157846 31849471 |
|||
3856743875 939537646 438223421 |
|||
2095761985 59336731 146190926 |
|||
326204827 4075320084 28261687 |
|||
1502134302 3230759874 35464999 |
|||
1431157111 868560455 70977191 |
|||
2315952016 2579115195 227102616 |
|||
1802159015 1532007317 265839104 |
|||
2241952911 807268722 61291733 |
|||
3297512098 497553830 309714892 |
|||
852851251 4103581771 2643427 |
|||
3252584902 3869047542 44927196 |
|||
855494678 1377761067 122396779 |
|||
2543054632 4106225198 139404427 |
|||
2682459059 2278875941 300239254 |
|||
803513580 4245629625 49337671 |
|||
416760627 205527657 25158077 |
|||
3800421807 2806217811 56322068 |
|||
441918704 2862539879 305925882 |
|||
|
|||
temperature-to-humidity map: |
|||
3507573 490548898 11693081 |
|||
545755853 699222305 569882925 |
|||
3794976513 167435410 77260251 |
|||
0 1526297837 3507573 |
|||
1335234764 1766508370 36536350 |
|||
2131780538 502241979 64264976 |
|||
3707588652 1679120509 87387861 |
|||
96082543 2288930706 220305732 |
|||
1371771114 1529805410 106547120 |
|||
481810045 3044354609 63945808 |
|||
15200654 1426594789 7560739 |
|||
3206337878 109359655 58075755 |
|||
2445677382 2019348918 269581788 |
|||
1909096745 3571407035 4209780 |
|||
3138678479 4049712539 66833109 |
|||
3400069156 3490205314 81201721 |
|||
4084645800 3846924477 65868498 |
|||
2353535073 1434155528 92142309 |
|||
1716598669 3935506457 114206082 |
|||
3264413633 4116545648 30901597 |
|||
1913306525 3628450464 218474013 |
|||
3205511588 3461720938 826290 |
|||
2748703371 2770603565 195642711 |
|||
2715259170 244990096 33149766 |
|||
1478318234 3108300417 88629505 |
|||
22761393 0 73321150 |
|||
3295315230 3575616815 45887125 |
|||
3872236764 278139862 212409036 |
|||
2196045514 1269105230 157489559 |
|||
2972004168 2509236438 123906332 |
|||
316388275 3296299168 165421770 |
|||
1685215763 3025113077 18894296 |
|||
2748408936 244695661 294435 |
|||
3704521599 4147447245 3067053 |
|||
1215008024 566506955 120226740 |
|||
1830804751 3912792975 22713482 |
|||
1704110059 686733695 12488610 |
|||
3488217401 1803044720 216304198 |
|||
1872711004 3044007373 347236 |
|||
1566947739 2652335541 118268024 |
|||
3481270877 3621503940 6946524 |
|||
2944346082 3462547228 27658086 |
|||
3095910500 1636352530 42767979 |
|||
1115638778 3196929922 99369246 |
|||
3341202355 2966246276 58866801 |
|||
1853518233 2633142770 19192771 |
|||
1873058240 73321150 36038505 |
|||
|
|||
humidity-to-location map: |
|||
336906655 0 11018487 |
|||
4177510177 2085057023 105144397 |
|||
1299579245 2985741466 175347598 |
|||
643133711 2270603056 161424888 |
|||
2404489601 1000033728 105953201 |
|||
4282654574 2864154964 12312722 |
|||
3409171342 3327025690 30826088 |
|||
2119751049 2190201420 80401636 |
|||
3393269098 3357851778 15902244 |
|||
82121354 319849190 39107402 |
|||
1953814423 3161089064 165936626 |
|||
64524116 385149760 17597238 |
|||
3439997430 2057119912 27937111 |
|||
0 358956592 26193168 |
|||
347925142 11018487 93152804 |
|||
1484466972 2432027944 360604841 |
|||
2510442802 643133711 347359888 |
|||
26193168 402746998 38330948 |
|||
1190305465 2946259551 39481915 |
|||
2200152685 1105986929 204336916 |
|||
3467934541 2792632785 71522179 |
|||
804558599 3373754022 385746866 |
|||
3539456720 1310323845 638053457 |
|||
2857802690 3759500888 535466408 |
|||
121228756 104171291 215677899 |
|||
1229787380 2876467686 69791865 |
|||
1474926843 990493599 9540129 |
|||
1845071813 1948377302 108742610 |
Loading…
Reference in new issue