Compare commits
No commits in common. "ee317153e619967a01412d634b2059ef862d22f5" and "3c0e91576018bc9fe1cae0b4516038b56afcd27a" have entirely different histories.
ee317153e6
...
3c0e915760
4 changed files with 246 additions and 451 deletions
43
example.txt
43
example.txt
|
@ -1,33 +1,10 @@
|
|||
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
|
||||
467..114..
|
||||
...*......
|
||||
..35..633.
|
||||
......#...
|
||||
617*......
|
||||
.....+.58.
|
||||
..592.....
|
||||
......755.
|
||||
...$.*....
|
||||
.664.598..
|
||||
|
|
312
input.txt
312
input.txt
|
@ -1,172 +1,140 @@
|
|||
seeds: 5844012 110899473 1132285750 58870036 986162929 109080640 3089574276 100113624 2693179996 275745330 2090752257 201704169 502075018 396653347 1540050181 277513792 1921754120 26668991 3836386950 66795009
|
||||
|
||||
seed-to-soil map:
|
||||
3547471595 1239929038 174680800
|
||||
3052451552 758183681 481745357
|
||||
0 1427884524 1775655006
|
||||
2844087171 549819300 208364381
|
||||
3767989253 4004864866 5194940
|
||||
3534196909 1414609838 13274686
|
||||
1775655006 114264781 435554519
|
||||
4148908402 4010059806 146058894
|
||||
2729822390 0 114264781
|
||||
3773184193 4156118700 138848596
|
||||
2211209525 3203539530 518612865
|
||||
3912032789 3767989253 236875613
|
||||
|
||||
soil-to-fertilizer map:
|
||||
912405184 1056091028 152837752
|
||||
194471272 1208928780 200072008
|
||||
136115250 240819204 58356022
|
||||
3502815281 3536983174 299994001
|
||||
2321814552 2458149869 18748048
|
||||
3173949445 2623931701 9591555
|
||||
394543280 888648379 167442649
|
||||
1990258415 3891640206 212931291
|
||||
1068754270 54862533 153774684
|
||||
1222528954 660792432 186471834
|
||||
3340878967 3405750148 131233026
|
||||
3472111993 3836977175 30703288
|
||||
561985929 299175226 225501956
|
||||
2942828492 4104571497 88527954
|
||||
874532405 850775600 37872779
|
||||
3031356446 3867680463 23959743
|
||||
842350418 208637217 32181987
|
||||
0 524677182 136115250
|
||||
1065242936 847264266 3511334
|
||||
2203189706 2339525023 118624846
|
||||
1929736108 2563409394 60522307
|
||||
3802809282 1847367009 492158014
|
||||
1847367009 2790861223 82369099
|
||||
2442430445 2873230322 500398047
|
||||
3183541000 2633523256 157337967
|
||||
3141827666 3373628369 32121779
|
||||
787487885 0 54862533
|
||||
3055316189 2476897917 86511477
|
||||
2340562600 4193099451 101867845
|
||||
|
||||
fertilizer-to-water map:
|
||||
798315344 439687669 1930292
|
||||
1174979421 2966258900 475289790
|
||||
439687669 778614573 55925503
|
||||
3743699694 3453541232 155280637
|
||||
2989334775 1659556189 96021468
|
||||
1650269211 3441548690 11992542
|
||||
3898980331 3608821869 395986965
|
||||
1705125292 2123762646 842496254
|
||||
3375514705 1755577657 368184989
|
||||
646363825 441617961 94777173
|
||||
1662261753 1616692650 42863539
|
||||
495613172 627863920 150750653
|
||||
2777061135 1174979421 212273640
|
||||
741140998 834540076 57174346
|
||||
800245636 536395134 91468786
|
||||
2547621546 1387253061 229439589
|
||||
3085356243 4004808834 290158462
|
||||
|
||||
water-to-light map:
|
||||
541719462 212840988 165903288
|
||||
3437755571 1615831015 672632835
|
||||
1051033542 2678450187 510773217
|
||||
243353905 378744276 104057369
|
||||
1561806759 3801474127 134575711
|
||||
707622750 677109833 258560892
|
||||
2580483557 1051033542 195631857
|
||||
0 935670725 30512917
|
||||
347411274 482801645 194308188
|
||||
30512917 0 127836567
|
||||
2190497220 2288463850 389986337
|
||||
3068589955 1246665399 369165616
|
||||
1754579039 3936049838 66442917
|
||||
4110388406 3616895237 184578890
|
||||
158349484 127836567 85004421
|
||||
2776115414 4002492755 292474541
|
||||
1821021956 3247419973 369475264
|
||||
1696382470 3189223404 58196569
|
||||
|
||||
light-to-temperature map:
|
||||
338228166 2812162941 77503977
|
||||
3123877206 693964345 40932068
|
||||
3939438903 614787731 633466
|
||||
3301169239 2299402886 215156012
|
||||
3516325251 499164007 115623724
|
||||
3852145506 3365439095 87293397
|
||||
875755064 3565078024 729889272
|
||||
2849390436 734896413 11914872
|
||||
2861305308 215265512 77518872
|
||||
4220385528 2056242491 68417151
|
||||
2221674563 3167682469 34693717
|
||||
2765973467 292784384 83416969
|
||||
3198884011 1699097031 22283517
|
||||
3164809274 2889666918 34074737
|
||||
809366712 746811285 52314986
|
||||
4096970661 1633036395 58945085
|
||||
3221167528 2699611924 80001711
|
||||
697021180 3452732492 112345532
|
||||
2059345064 799126271 162329499
|
||||
4155915746 629494563 64469782
|
||||
2431111524 1721380548 334861943
|
||||
684283267 2779613635 12737913
|
||||
440342453 2923741655 243940814
|
||||
3634265609 961455770 217879897
|
||||
4288802679 3202376186 6164617
|
||||
415732143 1691981480 4798917
|
||||
1605644336 1179335667 453700728
|
||||
420531060 2792351548 19811393
|
||||
3631948975 1696780397 2316634
|
||||
2938824180 2514558898 185053026
|
||||
215265512 376201353 122962654
|
||||
2256368280 2124659642 174743244
|
||||
3940072369 3208540803 156898292
|
||||
861681698 615421197 14073366
|
||||
|
||||
temperature-to-humidity map:
|
||||
841576398 2731200418 60836938
|
||||
1860695540 395011682 292982985
|
||||
1518037021 3432774193 53954373
|
||||
1786573987 2461900019 55353430
|
||||
2940303448 1995108352 6974538
|
||||
3014069287 1664464874 53916789
|
||||
753139746 2190061656 88436652
|
||||
2351822957 2925825589 209990361
|
||||
3079884326 3498626816 37609115
|
||||
4067060121 4186346579 108620717
|
||||
4175680838 4102322798 3778367
|
||||
1571991394 2415899186 46000833
|
||||
2841627583 1157034722 22534242
|
||||
1841927417 2517253449 18768123
|
||||
2153678525 47765849 108568164
|
||||
3251281674 1179568964 226737882
|
||||
3067986076 3486728566 11898250
|
||||
1308101633 1785172964 209935388
|
||||
2262246689 1406306846 89576268
|
||||
902413336 2278498308 137400878
|
||||
1039814214 3765380846 268287419
|
||||
45422022 156334013 238677669
|
||||
3617672799 2612163195 119037223
|
||||
2947277986 1718381663 66791301
|
||||
1617992227 1495883114 168581760
|
||||
284099691 687994667 469040055
|
||||
4214721882 4106101165 80245414
|
||||
2839283756 45422022 2343827
|
||||
3117493441 2792037356 133788233
|
||||
2864161825 2536021572 76141623
|
||||
4179459205 4067060121 35262677
|
||||
3736710022 3135815950 296958243
|
||||
3478019556 2002082890 139653243
|
||||
2610138841 3536235931 229144915
|
||||
2561813318 2141736133 48325523
|
||||
|
||||
humidity-to-location map:
|
||||
608325534 0 231346900
|
||||
222429954 453776854 385895580
|
||||
3710263359 3540956206 563631409
|
||||
1193511298 1116937854 38719102
|
||||
1702450793 1176729484 153572024
|
||||
1694669826 1109156887 7780967
|
||||
0 231346900 222429954
|
||||
1856022817 1686715664 1854240542
|
||||
1588644556 4104587615 106025270
|
||||
1109156887 4210612885 84354411
|
||||
4273894768 1155656956 21072528
|
||||
1232230400 1330301508 356414156
|
||||
...............776..............552........968..................589...26...........484..............958......186....546.........484.........
|
||||
.........*.........778....................*....124...................................*...............*........%..26.........................
|
||||
......194..380....@....900..........639....467........478*..............582...........798.............326...........894.........#...........
|
||||
904...........*2.......#......259.....*..........801......464................597.569.............794+................$..218....502..........
|
||||
...*.....................-...$.....%..431.........*...810.....840+.668..........*.......144=.............................../...........%627.
|
||||
...890...497.........829.643....504..........465..502..............*........488...................787.184...601....215........-450..........
|
||||
.......................*............684.....*.............466...970....646..%...........399...........*.............=....88...........=415..
|
||||
....................139........415...+......854.....683....*.........+.=.......107.676.....*451......83..416....856.....%....765............
|
||||
..............163..............*...................*......796.....992.............*...............................#.798.....*......819......
|
||||
.../....678..*...........429..351..........937......629.................*59.677..........652$.292....................*..431..184..*......688
|
||||
.79......*...694.......-...........492.247......26.......399/.=....................113.........*...................789....*.......469.......
|
||||
.......94............667.............*..*...933...*684........861...687...567*927.*.....869..509.......................423..................
|
||||
.596............................561.271.349....-.........139.........@............278......*............@...942*....36......................
|
||||
.....475...........*268.........*...................695.................850..#.........233.876......@..768......425......85*734.......*349..
|
||||
.......*..119.96........287.....177.............940*......................*..445.........@.........577..........................39..30......
|
||||
.....26......*.......................1=.685..........3...120..81.......533...................................718................*...........
|
||||
.....................306.........335.................*......+...*..................563..240..................*.......349*708...244...319....
|
||||
..........30..197...........639......................7.../.....471.......-.......@..*..%........895*680.....635.......................*.....
|
||||
.....*116...*..*..............*...........445.............643.........75.29.....42.586..........................41.....@.....323.....860....
|
||||
.511.......105.71.......765...670.....489*.........426.........758.....................139............94.793......*....63.....*.............
|
||||
...*.....................$..................470......*..........*.....69.......590.730*...........553...........814..........604............
|
||||
.680..............................443..............598...-.....318.....=........*........845..215..*.............................-..........
|
||||
............237.......180...........#.691...276...........909...............$..527...881.*.....*...585....224....762.....389..193...........
|
||||
.....@.....=.........@....789.................*.......857........&.450...716.............149..885............*............*............504..
|
||||
993...906................*.....685..688........385...*........925..*................................700*...811..=......295.....$311.....-...
|
||||
...@.................956..809.......*..............................577....965.......%846.....$...@.............839..........................
|
||||
.........704.393%....*............1..562..............716.................$..................387.645........................571.342./.......
|
||||
......@..*.........226...........*.........526*914...*......172....-..........32............................211.......990......*....695.....
|
||||
.....272..481.................288.....................370.....*.....740...........281..............430..13..$...............................
|
||||
397............#............................65................677....................*.586..758....*......*.................49.#973....409..
|
||||
.............818.........103...........524.*.........150...........781...236.../891.....*.........456.416.591.691-...849....+...........&...
|
||||
..................457@......*...........&..813...979*.....453*181....*.....-..........286.....565.....................&.......624...........
|
||||
......#....................221..464..................................604.....978.........................................%412.*.............
|
||||
619%..176.........=...280......*......*......................493...............*.......................570+....................961..233@....
|
||||
..............52..50....*.....26......518.207....%.465.........*.............871...817.............384........792.....@754..................
|
||||
............3*.......703............%..........554..............20.........................879.......-....646*...............415...814......
|
||||
........944......946..............766.477.492......835......806...........214.........*......*..............................*......%....633.
|
||||
....266*......*.....-........................#.783*........*..................900.....852....271..+567.452..249..........850................
|
||||
...........938.661....770...87.841..................@......739../.....230.....*...916....................*..*...636..49.........18..*20..4..
|
||||
....110................*......*......#341.-.......*.75..........660..........117.....$.803@.780........172.......*....*.160/.....+..........
|
||||
....+..................92.693.............952..988..................670.....................@...972...........760..194...............24.....
|
||||
........479*...880..=........&..475&..677..........%283.....642....*............359...154................755.............690...........&....
|
||||
...../.........*.....590..=..........&.......&....................765......./...*....................419........214-.......*...324..........
|
||||
......934...470...........10.......*....-....412............750...........496..651...+701...........*....................698......*217......
|
||||
............................./228...484.854...........627....%......795.......................*.............450...66........................
|
||||
............978$...274.......................822.........*.....-760............700.........235.346................*.............+.....885...
|
||||
...................%...582..........338.@.....*...274..252.....................*...720....................700.18.41...........503.......+...
|
||||
........4..960....................../....722..897.&..........987..........510..104....+..416.....549.......*...*...................407......
|
||||
.......*......*211............=725...................$574.......*.........................+......*.......788..350...432..............*......
|
||||
.244...921.........601....36*..................222..............918...344..733.....109.........610....66..............*...............937...
|
||||
...*.........464...*.../.....991................*....................+....*....285*......538..........*.......914......526...465.722........
|
||||
............*.....362.460........49............66.121...=...............373.............=.........530..337....=.................*........606
|
||||
154...713...611............$725....................*..29...425..74..............920.................@.................389...........715.....
|
||||
......*........../.....143.......................652.........*..*..711...69.26........406.....................37*175........122.....#.......
|
||||
.....588........39....*......224.........*................868..800...&../...*....407.......46..387.728......................*...............
|
||||
............-........39.........*.541.595.516...............................484.*.................*...........206.........636..807..........
|
||||
.............858.............716....*..........810*290..372........@949.........921...................428........*..+..........*............
|
||||
........486........301*307.........135.................*................264.178......838.....224..-......*......499..816.......227..........
|
||||
.......*........................................802...608............24*.......*.........940......98....960............................685..
|
||||
.......425.76.........+94..556...........699.......*.......................%....857.266.*.....................8................624*....=....
|
||||
............*.................*735......&....$..345....361%.803..856....732.........*....520....*..............%.......269...=.....286......
|
||||
...........73.901.../..............384.....520......#.........-./...........447......930......279.......634.............#..207.754......473.
|
||||
..............@...538.936*484.....*....665...........630...............236...@.....%.....................*...985..406............-..........
|
||||
..............................328..531...%.616...........................*........64..497..105........844.....*......*......207.............
|
||||
.....*730......................*.......................478..701........+..567........*...........110.......175.......6.737.......+....192...
|
||||
..363......119....6....942......892....171..358.981..........*........731.........455....#..........*993.......383......*.........505..@....
|
||||
..............*....%.%....*............*......*..../..%.....................995........850...264..........82%.....=......735.../............
|
||||
..207...413/.349.@...628.685.........930......231....921.......................................$..142...........-............713....260.....
|
||||
.................384.........527..........373.............@946....&...............948...949......*......349...838......................*....
|
||||
.......................210....*.....................*241.........333.......................*..723........*..........632....916-...164..63...
|
||||
....789....../..859......@....770............286.881.................867.........218......346.........691..368......*.......................
|
||||
......$...411.....@.781............857................*912.135.&27...........885....*.942.........%............436..264.449.577..92#........
|
||||
...............9....................*..............391.....*................*......54..=.........108.......................*................
|
||||
..............$..106........767..290...751................977............990................499...................466.........545...........
|
||||
...............................=..........*..................................345............./....=.../...266.847....*605....*....219.......
|
||||
..................984.....735..........761......727.........814.481.....737...*......936...........76.599.*....=...........247...=..........
|
||||
.....................*697...*.595.................=...........*........*.......480.........................822.....287..................941.
|
||||
.....182*875...477#..............*...108...................390.......266...........678.593.......-..............$....&.649...../..../...*...
|
||||
....................279..............*...136........@..........................513.=...*.........332...510.....337............534..236..101.
|
||||
.......................*538........453......=.......390....235...................*......741............*.................792@...............
|
||||
..492......639.650.........................................@.....................21........./.......170............................+..@.....
|
||||
...*..942..=................794.........@..........456............185.395....437...........721..........206..+231...............638...424...
|
||||
479......*....................*.........251....243*....387........=..................520.............$...../................................
|
||||
..........971.................995.........................................116.......-.....*950...*....731.....543.......+..............748..
|
||||
......522................852=..............380..848......932........482......*644......514........40.........#..........869........710..-...
|
||||
.........*......................#955......./...*....../.....*46.......*.160#.................652..................218..........478*.........
|
||||
.........870...254.........309.......106......214....840..#.........29.......%........111.....$.........20*865..........&.............694...
|
||||
..................*316........*323......*................734.881.......33...84.97$..4*..........*135...............772.993............*.....
|
||||
.......................208...............793.....157............*597....*....................818.........-...260.....*........933....384....
|
||||
..........106......34......358*263.................+...457*...$.......866.583....................439=..677......@................=..........
|
||||
......-...*........#.......................................87.68...............#.......186%..................&.....=626.....................
|
||||
...191...136..453*.............#../537...............%.612........89..417......50.........................429..................859..........
|
||||
..................432....*89.20...................387..../....../.......*.........121...108...................................&.............
|
||||
...........768........................134...793................339....206.........*......$.......394.......@.........974..........132*863...
|
||||
......./...*................252.355.....#......................................335...169...332...*.......896....894..*......................
|
||||
....629....191..........369*.................109.............326..312*....&...........*.....*....478...........*......432...&......104......
|
||||
670............................&....967*........*.....................516..92..855...685.712..............936...............88....$.........
|
||||
.....583......974....26*24......782.....806...160...............669...............@..........606*........%......89..........................
|
||||
786.............*................................................&..................=............742...........+...................%........
|
||||
......#....@.939......375..............332-..........575...546*........*....196.628.581....@...............288.........645...467.246..125...
|
||||
....46...872.....414.....*..=......+........677................342..883.301..*...@.........699.261...796......*....809....-..*.......*......
|
||||
................$.....828....940..400.........$.40............................39.........*..............=.......-....*........780....925....
|
||||
.....*375.................39.............418....*................................../..279.367../676..........982.....216....=...............
|
||||
..703......$....../............703*.......*..904.....465.........*182..92.........173...............610...65................685.....55$.....
|
||||
.........904...650.................274...6.....................&.........*635...............787.306*.......*.............*..................
|
||||
..136........................................933......783.724.320...344..................../...........+....51...108..597.41.....809........
|
||||
...+........715.663............$.....821.....*...........*............*.....@657..354..411..........811..........*..........................
|
||||
......633.....*............*...925.........991...852........=...=......184..........*.........589.................788...191.................
|
||||
.........*....574........368.....................@.......373..657.701........411..575.954&......*.......$.....748.........*....758..........
|
||||
........787........770.............................879............*......88....*..............119.....817.....*.........914....*......751...
|
||||
.14.173...............*........................189*................509..%......29.............................87................683..*......
|
||||
...*........../.....563..270*876...123................................................../64.....216........................977......316.....
|
||||
.......612..516.50...................*................762..23/.........26.........608.............$..466*214.......455...#..#..352......928.
|
||||
........*.........*......296..@.......970..513.........*.........584..................233.....945................./.....846...+.............
|
||||
......918.......640..585.....600..550.....*.......=....723.......*.............-.......-..418.....15.180...969.......................703....
|
||||
...@.......-63...........=........*.......839...769............76........671...268........+..........*.........965....#.....#.568...*.......
|
||||
.194.....................453......530...................*675......487.=....+.........309....224..507.209.........@.138....300..*..$.........
|
||||
.......@..@....846.............%.........714...468...150......88......510..............*...*.......*...........................9.816........
|
||||
.....234.140....*..............903..789.....*.*..............*..............631........702.80....33.........513........276..................
|
||||
.............381.......................*.803...474......267...296.436.......*........................679....*............*........939...784.
|
||||
.....-..........................33&.158....................*......*.......569........487...871/............510......274.542.........*.......
|
||||
......883.....*163...782..-.............379.764@.....912=..796...91.....................&.......979....907.....*......*........167....+.+873
|
||||
...........110........*..577........&....*............................*778...................$............@.241.428..949...680*.....513.....
|
||||
....................924........471.470.268..486......../....#......567...........470..469.794....%...............................+..........
|
||||
..725*532......$........@.......*..........*........926......213............405.....*...*......355...615...#....73........151.....896.......
|
||||
............980......969....812..28.936.....882..............................&...107.....377...........*.611.......+.......&..737...........
|
||||
....$594...................*............156.....210....585...........234.821.........................403........837..........*....652.......
|
||||
...........................247.@881..7.....*....*.....................*.....*.......*........140.351..........................510....*......
|
||||
...........790*776...................*...270...778......707.110.....487..486...460.70........*.....#...585.702..430..141...........34.......
|
||||
...52.......................&216..788......................*...................*............199...........*....=......................989...
|
||||
....$..445...498.254.508*.............*860..711.48......./.............596....305.....618..........337.............198....287...............
|
||||
.........#....*....@.....352....*..645......*.............328......50.*..................=...-........*584..........*..........*275..986*527
|
||||
.............560..............815.........931......400............*...592......824.........807...591*................311.....82.............
|
||||
.........%.......678.926........................19...*......545..649...........*...............*.....393....=896............................
|
||||
......834...........*............................/....386.....*......=290...772.............545.969...................679.820......704......
|
||||
..........122...........202....693.................*..............................555*826..............458......691......*.........*........
|
||||
....#.......&..%692........+......#..333............41...............=204..................868............/.164..................316........
|
||||
...346..................+.......$...*......396..315....119..@755...............942..918........................*........631.651.............
|
||||
.................=....836......402.755............=.......*.......*....181.62...*.....%........................596......*............#591...
|
||||
...............764.....................................488.....880.209........416..................183...156..........917...................
|
||||
|
|
46
solve.py
46
solve.py
|
@ -1,31 +1,37 @@
|
|||
#!/usr/bin/env python3
|
||||
import sys
|
||||
from dataclasses import dataclass
|
||||
import re
|
||||
|
||||
@dataclass
|
||||
class Row:
|
||||
cards: int
|
||||
matches: int
|
||||
num_values = dict(
|
||||
on="1",
|
||||
tw="2",
|
||||
thre="3",
|
||||
four="4",
|
||||
fiv="5",
|
||||
six="6",
|
||||
seve="7",
|
||||
eigh="8",
|
||||
nin="9",
|
||||
)
|
||||
|
||||
nums_re = r"on(?=e)|tw(?=o)|thre(?=e)|four|fiv(?=e)|six|seve(?=n)|eigh(?=t)|nin(?=e)|1|2|3|4|5|6|7|8|9"
|
||||
|
||||
sum = 0
|
||||
rows = []
|
||||
for line in sys.stdin:
|
||||
_, nums = line.split(":")
|
||||
winning, mine = nums.split("|")
|
||||
first = len(line)
|
||||
last = 0
|
||||
nums = []
|
||||
for m in re.finditer(nums_re, line):
|
||||
nums.append((m.start(), m.group(0)))
|
||||
|
||||
winning = set(winning.split())
|
||||
mine = set(mine.split())
|
||||
nums.sort()
|
||||
first_word = nums[0][1]
|
||||
last_word = nums[-1][1]
|
||||
|
||||
both = winning.intersection(mine)
|
||||
rows.append(Row(matches=len(both), cards=1))
|
||||
first_word = num_values.get(first_word, first_word)
|
||||
last_word = num_values.get(last_word, last_word)
|
||||
|
||||
for i in range(len(rows)):
|
||||
m = rows[i]
|
||||
for j in range(m.matches):
|
||||
rows[i + 1 + j].cards += m.cards
|
||||
|
||||
for row in rows:
|
||||
sum += row.cards
|
||||
print(row)
|
||||
value = int(first_word + last_word)
|
||||
sum += value
|
||||
|
||||
print(sum)
|
||||
|
|
284
src/main.rs
284
src/main.rs
|
@ -1,242 +1,86 @@
|
|||
use std::cmp::{max, min};
|
||||
use anyhow::Result;
|
||||
use fancy_regex::Regex;
|
||||
use std::{collections::HashMap, io::stdin};
|
||||
|
||||
use anyhow::Result;
|
||||
use regex::Regex;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
struct Range {
|
||||
start: i64,
|
||||
len: i64,
|
||||
fn is_gear(x: i32, y: i32, lines: &[&[u8]]) -> bool {
|
||||
if x < 0 {
|
||||
return false;
|
||||
}
|
||||
if y < 0 {
|
||||
return false;
|
||||
}
|
||||
if y as usize >= lines.len() {
|
||||
return false;
|
||||
}
|
||||
let line = &lines[y as usize];
|
||||
if x as usize >= line.len() {
|
||||
return false;
|
||||
}
|
||||
if line[x as usize] == b'*' {
|
||||
return true;
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
impl Range {
|
||||
fn intersection(&self, other: &Range) -> Option<Range> {
|
||||
let i1 = max(self.start, other.start);
|
||||
let i2 = min(self.start + self.len, other.start + other.len);
|
||||
|
||||
if i1 >= i2 {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(Range {
|
||||
start: i1,
|
||||
len: i2 - i1,
|
||||
})
|
||||
fn find_gears(x: i32, y: i32, len: i32, lines: &[&[u8]]) -> Vec<(i32, i32)> {
|
||||
let mut v = vec![];
|
||||
for cx in (x - 1)..(x + len) {
|
||||
if is_gear(cx, y - 1, lines) {
|
||||
v.push((cx, y - 1));
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
struct RangeMap {
|
||||
from: i64,
|
||||
to: i64,
|
||||
len: i64,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct SeedsMap {
|
||||
range_maps: Vec<RangeMap>,
|
||||
}
|
||||
|
||||
impl SeedsMap {
|
||||
fn new() -> SeedsMap {
|
||||
SeedsMap { range_maps: vec![] }
|
||||
}
|
||||
|
||||
fn add_range(&mut self, range: (i64, i64, i64)) {
|
||||
self.range_maps.push(RangeMap {
|
||||
from: range.1,
|
||||
to: range.0,
|
||||
len: range.2,
|
||||
});
|
||||
self.range_maps.sort_by_key(|r| r.from);
|
||||
}
|
||||
|
||||
fn map(&self, ranges: &[Range]) -> Vec<Range> {
|
||||
let mut result = vec![];
|
||||
for r in ranges {
|
||||
result.extend(self.map_single_range(r));
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn map_single_range(&self, range: &Range) -> Vec<Range> {
|
||||
if self.range_maps.is_empty() {
|
||||
return vec![*range];
|
||||
}
|
||||
|
||||
let mut result = vec![];
|
||||
let first = self.range_maps.first().unwrap();
|
||||
let last = self.range_maps.last().unwrap();
|
||||
let left = min(range.start, first.from);
|
||||
let right = max(range.start + range.len, last.from + last.len);
|
||||
|
||||
let mut last = left;
|
||||
for rm in &self.range_maps {
|
||||
// first, a range from last to rm.start, with no mapping
|
||||
let r = Range {
|
||||
start: last,
|
||||
len: rm.from - last,
|
||||
};
|
||||
if let Some(i) = r.intersection(range) {
|
||||
result.push(i);
|
||||
}
|
||||
// second, a range from rm.start, with mapping
|
||||
let r = Range {
|
||||
start: rm.from,
|
||||
len: rm.len,
|
||||
};
|
||||
if let Some(i) = r.intersection(range) {
|
||||
result.push(Range {
|
||||
start: rm.to + i.start - rm.from,
|
||||
len: i.len,
|
||||
});
|
||||
last = i.start + i.len;
|
||||
for cy in (y - 1)..=y {
|
||||
if is_gear(x + len, cy, lines) {
|
||||
v.push((x + len, cy));
|
||||
}
|
||||
}
|
||||
|
||||
let r = Range {
|
||||
start: last,
|
||||
len: right - last,
|
||||
};
|
||||
if let Some(i) = r.intersection(range) {
|
||||
result.push(i);
|
||||
}
|
||||
|
||||
result
|
||||
for cx in x..=(x + len) {
|
||||
if is_gear(cx, y + 1, lines) {
|
||||
v.push((cx, y + 1));
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_input(seed_ranges: &mut Vec<Range>, maps: &mut HashMap<String, SeedsMap>) -> Result<()> {
|
||||
let numbers = Regex::new(r"(\d+)")?;
|
||||
let pairs = Regex::new(r"(\d+) (\d+)")?;
|
||||
let mut current_map = String::new();
|
||||
for line in stdin().lines().map_while(Result::ok) {
|
||||
if line.starts_with("seeds:") {
|
||||
*seed_ranges = pairs
|
||||
.captures_iter(&line)
|
||||
.map(|c| -> Result<Range> {
|
||||
Ok(Range {
|
||||
start: c.get(1).unwrap().as_str().parse()?,
|
||||
len: c.get(2).unwrap().as_str().parse()?,
|
||||
})
|
||||
})
|
||||
.map_while(Result::ok)
|
||||
.collect();
|
||||
} else if line.ends_with(" map:") {
|
||||
current_map = line
|
||||
.split_whitespace()
|
||||
.next()
|
||||
.unwrap_or_default()
|
||||
.to_owned();
|
||||
} else {
|
||||
let nums: Vec<i64> = numbers
|
||||
.captures_iter(&line)
|
||||
.map(|c| c.get(1).unwrap().as_str().parse())
|
||||
.map_while(Result::ok)
|
||||
.collect();
|
||||
if nums.len() == 3 {
|
||||
let map = maps
|
||||
.entry(current_map.clone())
|
||||
.or_insert_with(SeedsMap::new);
|
||||
map.add_range((nums[0], nums[1], nums[2]));
|
||||
for cy in y..=(y + 1) {
|
||||
if is_gear(x - 1, cy, lines) {
|
||||
v.push((x - 1, cy));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
v
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let mut maps: HashMap<String, SeedsMap> = HashMap::new();
|
||||
let mut seed_ranges: Vec<Range> = vec![];
|
||||
let num_re = Regex::new(r"(\d+)")?;
|
||||
|
||||
parse_input(&mut seed_ranges, &mut maps)?;
|
||||
let mut sum = 0;
|
||||
let lines: Vec<String> = stdin().lines().map_while(Result::ok).collect();
|
||||
let raw_lines: Vec<&[u8]> = lines.iter().map(|s| s.as_bytes()).collect();
|
||||
|
||||
let map_names = [
|
||||
"seed-to-soil",
|
||||
"soil-to-fertilizer",
|
||||
"fertilizer-to-water",
|
||||
"water-to-light",
|
||||
"light-to-temperature",
|
||||
"temperature-to-humidity",
|
||||
"humidity-to-location",
|
||||
];
|
||||
let mut gear_map: HashMap<(i32, i32), Vec<i32>> = HashMap::new();
|
||||
|
||||
let mut locations = vec![];
|
||||
for range in seed_ranges {
|
||||
let mut mapped_ranges = vec![range];
|
||||
for map_name in map_names {
|
||||
let mapped = maps.get(map_name).unwrap().map(&mapped_ranges);
|
||||
mapped_ranges = mapped;
|
||||
for (lineno, line) in lines.iter().enumerate() {
|
||||
for m in num_re.captures_iter(line) {
|
||||
let m = m?;
|
||||
let num_match = m.get(1).unwrap();
|
||||
let num_text = num_match.as_str();
|
||||
let num: i32 = num_text.parse()?;
|
||||
let gears = find_gears(
|
||||
num_match.start() as i32,
|
||||
lineno as i32,
|
||||
num_text.len() as i32,
|
||||
&raw_lines,
|
||||
);
|
||||
for gear in gears {
|
||||
let e = gear_map.entry(gear).or_default();
|
||||
e.push(num);
|
||||
}
|
||||
locations.extend(mapped_ranges.iter().map(|r| r.start));
|
||||
}
|
||||
println!("{}", locations.iter().min().unwrap());
|
||||
}
|
||||
for v in gear_map.values() {
|
||||
if v.len() == 2 {
|
||||
sum += v.iter().product::<i32>();
|
||||
}
|
||||
}
|
||||
|
||||
println!("{sum}");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_intersections() {
|
||||
let l = |r1: &Range, r2: &Range| r1.intersection(r2);
|
||||
assert_eq!(
|
||||
l(&Range { start: 1, len: 3 }, &Range { start: 1, len: 3 }),
|
||||
Some(Range { start: 1, len: 3 })
|
||||
);
|
||||
assert_eq!(
|
||||
l(&Range { start: 1, len: 2 }, &Range { start: 1, len: 3 }),
|
||||
Some(Range { start: 1, len: 2 })
|
||||
);
|
||||
assert_eq!(
|
||||
l(&Range { start: 2, len: 4 }, &Range { start: 0, len: 3 }),
|
||||
Some(Range { start: 2, len: 1 })
|
||||
);
|
||||
assert_eq!(
|
||||
l(&Range { start: 0, len: 3 }, &Range { start: 2, len: 4 }),
|
||||
Some(Range { start: 2, len: 1 })
|
||||
);
|
||||
assert_eq!(
|
||||
l(&Range { start: 3, len: 4 }, &Range { start: 0, len: 3 }),
|
||||
None
|
||||
);
|
||||
assert_eq!(
|
||||
l(&Range { start: 0, len: 10 }, &Range { start: 4, len: 4 }),
|
||||
Some(Range { start: 4, len: 4 })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_range_map() {
|
||||
let mut m = SeedsMap::new();
|
||||
m.add_range((52, 50, 48));
|
||||
m.add_range((50, 98, 2));
|
||||
let r = m.map_single_range(&Range { start: 79, len: 14 });
|
||||
assert_eq!(r, [Range { start: 81, len: 14 }]);
|
||||
|
||||
let mut m = SeedsMap::new();
|
||||
m.add_range((39, 0, 15));
|
||||
m.add_range((0, 15, 37));
|
||||
m.add_range((37, 52, 2));
|
||||
let r = m.map_single_range(&Range { start: 81, len: 14 });
|
||||
assert_eq!(r, [Range { start: 81, len: 14 }]);
|
||||
|
||||
let mut m = SeedsMap::new();
|
||||
m.add_range((88, 18, 7));
|
||||
m.add_range((18, 25, 70));
|
||||
let r = m.map_single_range(&Range { start: 81, len: 14 });
|
||||
assert_eq!(r, [Range { start: 74, len: 14 }]);
|
||||
|
||||
let mut m = SeedsMap::new();
|
||||
m.add_range((81, 45, 19));
|
||||
m.add_range((68, 64, 13));
|
||||
m.add_range((45, 77, 23));
|
||||
let r = m.map_single_range(&Range { start: 74, len: 14 });
|
||||
assert_eq!(
|
||||
r,
|
||||
[Range { start: 78, len: 3 }, Range { start: 45, len: 11 }]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue