Compare commits

...

6 commits

Author SHA1 Message Date
ee317153e6 day 5 part 2 2023-12-07 01:17:13 -05:00
89734cdd86 day 5 part 1 2023-12-06 12:46:52 -05:00
9e92b66cbe day 4 part 2 (in Rust) 2023-12-04 16:18:09 -05:00
7d234e0fc2 day 4 part 1 (in Rust) 2023-12-04 16:18:09 -05:00
8ff5e7f9c9 day 4 part 2 (in Python) 2023-12-04 16:18:05 -05:00
5aa6f8235d day 4 part 1 (in Python) 2023-12-04 16:18:02 -05:00
4 changed files with 451 additions and 246 deletions

View file

@ -1,10 +1,33 @@
467..114.. seeds: 79 14 55 13
...*......
..35..633. seed-to-soil map:
......#... 50 98 2
617*...... 52 50 48
.....+.58.
..592..... soil-to-fertilizer map:
......755. 0 15 37
...$.*.... 37 52 2
.664.598.. 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

312
input.txt
View file

@ -1,140 +1,172 @@
...............776..............552........968..................589...26...........484..............958......186....546.........484......... seeds: 5844012 110899473 1132285750 58870036 986162929 109080640 3089574276 100113624 2693179996 275745330 2090752257 201704169 502075018 396653347 1540050181 277513792 1921754120 26668991 3836386950 66795009
.........*.........778....................*....124...................................*...............*........%..26.........................
......194..380....@....900..........639....467........478*..............582...........798.............326...........894.........#........... seed-to-soil map:
904...........*2.......#......259.....*..........801......464................597.569.............794+................$..218....502.......... 3547471595 1239929038 174680800
...*.....................-...$.....%..431.........*...810.....840+.668..........*.......144=.............................../...........%627. 3052451552 758183681 481745357
...890...497.........829.643....504..........465..502..............*........488...................787.184...601....215........-450.......... 0 1427884524 1775655006
.......................*............684.....*.............466...970....646..%...........399...........*.............=....88...........=415.. 2844087171 549819300 208364381
....................139........415...+......854.....683....*.........+.=.......107.676.....*451......83..416....856.....%....765............ 3767989253 4004864866 5194940
..............163..............*...................*......796.....992.............*...............................#.798.....*......819...... 3534196909 1414609838 13274686
.../....678..*...........429..351..........937......629.................*59.677..........652$.292....................*..431..184..*......688 1775655006 114264781 435554519
.79......*...694.......-...........492.247......26.......399/.=....................113.........*...................789....*.......469....... 4148908402 4010059806 146058894
.......94............667.............*..*...933...*684........861...687...567*927.*.....869..509.......................423.................. 2729822390 0 114264781
.596............................561.271.349....-.........139.........@............278......*............@...942*....36...................... 3773184193 4156118700 138848596
.....475...........*268.........*...................695.................850..#.........233.876......@..768......425......85*734.......*349.. 2211209525 3203539530 518612865
.......*..119.96........287.....177.............940*......................*..445.........@.........577..........................39..30...... 3912032789 3767989253 236875613
.....26......*.......................1=.685..........3...120..81.......533...................................718................*...........
.....................306.........335.................*......+...*..................563..240..................*.......349*708...244...319.... soil-to-fertilizer map:
..........30..197...........639......................7.../.....471.......-.......@..*..%........895*680.....635.......................*..... 912405184 1056091028 152837752
.....*116...*..*..............*...........445.............643.........75.29.....42.586..........................41.....@.....323.....860.... 194471272 1208928780 200072008
.511.......105.71.......765...670.....489*.........426.........758.....................139............94.793......*....63.....*............. 136115250 240819204 58356022
...*.....................$..................470......*..........*.....69.......590.730*...........553...........814..........604............ 3502815281 3536983174 299994001
.680..............................443..............598...-.....318.....=........*........845..215..*.............................-.......... 2321814552 2458149869 18748048
............237.......180...........#.691...276...........909...............$..527...881.*.....*...585....224....762.....389..193........... 3173949445 2623931701 9591555
.....@.....=.........@....789.................*.......857........&.450...716.............149..885............*............*............504.. 394543280 888648379 167442649
993...906................*.....685..688........385...*........925..*................................700*...811..=......295.....$311.....-... 1990258415 3891640206 212931291
...@.................956..809.......*..............................577....965.......%846.....$...@.............839.......................... 1068754270 54862533 153774684
.........704.393%....*............1..562..............716.................$..................387.645........................571.342./....... 1222528954 660792432 186471834
......@..*.........226...........*.........526*914...*......172....-..........32............................211.......990......*....695..... 3340878967 3405750148 131233026
.....272..481.................288.....................370.....*.....740...........281..............430..13..$............................... 3472111993 3836977175 30703288
397............#............................65................677....................*.586..758....*......*.................49.#973....409.. 561985929 299175226 225501956
.............818.........103...........524.*.........150...........781...236.../891.....*.........456.416.591.691-...849....+...........&... 2942828492 4104571497 88527954
..................457@......*...........&..813...979*.....453*181....*.....-..........286.....565.....................&.......624........... 874532405 850775600 37872779
......#....................221..464..................................604.....978.........................................%412.*............. 3031356446 3867680463 23959743
619%..176.........=...280......*......*......................493...............*.......................570+....................961..233@.... 842350418 208637217 32181987
..............52..50....*.....26......518.207....%.465.........*.............871...817.............384........792.....@754.................. 0 524677182 136115250
............3*.......703............%..........554..............20.........................879.......-....646*...............415...814...... 1065242936 847264266 3511334
........944......946..............766.477.492......835......806...........214.........*......*..............................*......%....633. 2203189706 2339525023 118624846
....266*......*.....-........................#.783*........*..................900.....852....271..+567.452..249..........850................ 1929736108 2563409394 60522307
...........938.661....770...87.841..................@......739../.....230.....*...916....................*..*...636..49.........18..*20..4.. 3802809282 1847367009 492158014
....110................*......*......#341.-.......*.75..........660..........117.....$.803@.780........172.......*....*.160/.....+.......... 1847367009 2790861223 82369099
....+..................92.693.............952..988..................670.....................@...972...........760..194...............24..... 2442430445 2873230322 500398047
........479*...880..=........&..475&..677..........%283.....642....*............359...154................755.............690...........&.... 3183541000 2633523256 157337967
...../.........*.....590..=..........&.......&....................765......./...*....................419........214-.......*...324.......... 3141827666 3373628369 32121779
......934...470...........10.......*....-....412............750...........496..651...+701...........*....................698......*217...... 787487885 0 54862533
............................./228...484.854...........627....%......795.......................*.............450...66........................ 3055316189 2476897917 86511477
............978$...274.......................822.........*.....-760............700.........235.346................*.............+.....885... 2340562600 4193099451 101867845
...................%...582..........338.@.....*...274..252.....................*...720....................700.18.41...........503.......+...
........4..960....................../....722..897.&..........987..........510..104....+..416.....549.......*...*...................407...... fertilizer-to-water map:
.......*......*211............=725...................$574.......*.........................+......*.......788..350...432..............*...... 798315344 439687669 1930292
.244...921.........601....36*..................222..............918...344..733.....109.........610....66..............*...............937... 1174979421 2966258900 475289790
...*.........464...*.../.....991................*....................+....*....285*......538..........*.......914......526...465.722........ 439687669 778614573 55925503
............*.....362.460........49............66.121...=...............373.............=.........530..337....=.................*........606 3743699694 3453541232 155280637
154...713...611............$725....................*..29...425..74..............920.................@.................389...........715..... 2989334775 1659556189 96021468
......*........../.....143.......................652.........*..*..711...69.26........406.....................37*175........122.....#....... 1650269211 3441548690 11992542
.....588........39....*......224.........*................868..800...&../...*....407.......46..387.728......................*............... 3898980331 3608821869 395986965
............-........39.........*.541.595.516...............................484.*.................*...........206.........636..807.......... 1705125292 2123762646 842496254
.............858.............716....*..........810*290..372........@949.........921...................428........*..+..........*............ 3375514705 1755577657 368184989
........486........301*307.........135.................*................264.178......838.....224..-......*......499..816.......227.......... 646363825 441617961 94777173
.......*........................................802...608............24*.......*.........940......98....960............................685.. 1662261753 1616692650 42863539
.......425.76.........+94..556...........699.......*.......................%....857.266.*.....................8................624*....=.... 495613172 627863920 150750653
............*.................*735......&....$..345....361%.803..856....732.........*....520....*..............%.......269...=.....286...... 2777061135 1174979421 212273640
...........73.901.../..............384.....520......#.........-./...........447......930......279.......634.............#..207.754......473. 741140998 834540076 57174346
..............@...538.936*484.....*....665...........630...............236...@.....%.....................*...985..406............-.......... 800245636 536395134 91468786
..............................328..531...%.616...........................*........64..497..105........844.....*......*......207............. 2547621546 1387253061 229439589
.....*730......................*.......................478..701........+..567........*...........110.......175.......6.737.......+....192... 3085356243 4004808834 290158462
..363......119....6....942......892....171..358.981..........*........731.........455....#..........*993.......383......*.........505..@....
..............*....%.%....*............*......*..../..%.....................995........850...264..........82%.....=......735.../............ water-to-light map:
..207...413/.349.@...628.685.........930......231....921.......................................$..142...........-............713....260..... 541719462 212840988 165903288
.................384.........527..........373.............@946....&...............948...949......*......349...838......................*.... 3437755571 1615831015 672632835
.......................210....*.....................*241.........333.......................*..723........*..........632....916-...164..63... 1051033542 2678450187 510773217
....789....../..859......@....770............286.881.................867.........218......346.........691..368......*....................... 243353905 378744276 104057369
......$...411.....@.781............857................*912.135.&27...........885....*.942.........%............436..264.449.577..92#........ 1561806759 3801474127 134575711
...............9....................*..............391.....*................*......54..=.........108.......................*................ 707622750 677109833 258560892
..............$..106........767..290...751................977............990................499...................466.........545........... 2580483557 1051033542 195631857
...............................=..........*..................................345............./....=.../...266.847....*605....*....219....... 0 935670725 30512917
..................984.....735..........761......727.........814.481.....737...*......936...........76.599.*....=...........247...=.......... 347411274 482801645 194308188
.....................*697...*.595.................=...........*........*.......480.........................822.....287..................941. 30512917 0 127836567
.....182*875...477#..............*...108...................390.......266...........678.593.......-..............$....&.649...../..../...*... 2190497220 2288463850 389986337
....................279..............*...136........@..........................513.=...*.........332...510.....337............534..236..101. 3068589955 1246665399 369165616
.......................*538........453......=.......390....235...................*......741............*.................792@............... 1754579039 3936049838 66442917
..492......639.650.........................................@.....................21........./.......170............................+..@..... 4110388406 3616895237 184578890
...*..942..=................794.........@..........456............185.395....437...........721..........206..+231...............638...424... 158349484 127836567 85004421
479......*....................*.........251....243*....387........=..................520.............$...../................................ 2776115414 4002492755 292474541
..........971.................995.........................................116.......-.....*950...*....731.....543.......+..............748.. 1821021956 3247419973 369475264
......522................852=..............380..848......932........482......*644......514........40.........#..........869........710..-... 1696382470 3189223404 58196569
.........*......................#955......./...*....../.....*46.......*.160#.................652..................218..........478*.........
.........870...254.........309.......106......214....840..#.........29.......%........111.....$.........20*865..........&.............694... light-to-temperature map:
..................*316........*323......*................734.881.......33...84.97$..4*..........*135...............772.993............*..... 338228166 2812162941 77503977
.......................208...............793.....157............*597....*....................818.........-...260.....*........933....384.... 3123877206 693964345 40932068
..........106......34......358*263.................+...457*...$.......866.583....................439=..677......@................=.......... 3939438903 614787731 633466
......-...*........#.......................................87.68...............#.......186%..................&.....=626..................... 3301169239 2299402886 215156012
...191...136..453*.............#../537...............%.612........89..417......50.........................429..................859.......... 3516325251 499164007 115623724
..................432....*89.20...................387..../....../.......*.........121...108...................................&............. 3852145506 3365439095 87293397
...........768........................134...793................339....206.........*......$.......394.......@.........974..........132*863... 875755064 3565078024 729889272
......./...*................252.355.....#......................................335...169...332...*.......896....894..*...................... 2849390436 734896413 11914872
....629....191..........369*.................109.............326..312*....&...........*.....*....478...........*......432...&......104...... 2861305308 215265512 77518872
670............................&....967*........*.....................516..92..855...685.712..............936...............88....$......... 4220385528 2056242491 68417151
.....583......974....26*24......782.....806...160...............669...............@..........606*........%......89.......................... 2221674563 3167682469 34693717
786.............*................................................&..................=............742...........+...................%........ 2765973467 292784384 83416969
......#....@.939......375..............332-..........575...546*........*....196.628.581....@...............288.........645...467.246..125... 3198884011 1699097031 22283517
....46...872.....414.....*..=......+........677................342..883.301..*...@.........699.261...796......*....809....-..*.......*...... 3164809274 2889666918 34074737
................$.....828....940..400.........$.40............................39.........*..............=.......-....*........780....925.... 809366712 746811285 52314986
.....*375.................39.............418....*................................../..279.367../676..........982.....216....=............... 4096970661 1633036395 58945085
..703......$....../............703*.......*..904.....465.........*182..92.........173...............610...65................685.....55$..... 3221167528 2699611924 80001711
.........904...650.................274...6.....................&.........*635...............787.306*.......*.............*.................. 697021180 3452732492 112345532
..136........................................933......783.724.320...344..................../...........+....51...108..597.41.....809........ 2059345064 799126271 162329499
...+........715.663............$.....821.....*...........*............*.....@657..354..411..........811..........*.......................... 4155915746 629494563 64469782
......633.....*............*...925.........991...852........=...=......184..........*.........589.................788...191................. 2431111524 1721380548 334861943
.........*....574........368.....................@.......373..657.701........411..575.954&......*.......$.....748.........*....758.......... 684283267 2779613635 12737913
........787........770.............................879............*......88....*..............119.....817.....*.........914....*......751... 440342453 2923741655 243940814
.14.173...............*........................189*................509..%......29.............................87................683..*...... 3634265609 961455770 217879897
...*........../.....563..270*876...123................................................../64.....216........................977......316..... 4288802679 3202376186 6164617
.......612..516.50...................*................762..23/.........26.........608.............$..466*214.......455...#..#..352......928. 415732143 1691981480 4798917
........*.........*......296..@.......970..513.........*.........584..................233.....945................./.....846...+............. 1605644336 1179335667 453700728
......918.......640..585.....600..550.....*.......=....723.......*.............-.......-..418.....15.180...969.......................703.... 420531060 2792351548 19811393
...@.......-63...........=........*.......839...769............76........671...268........+..........*.........965....#.....#.568...*....... 3631948975 1696780397 2316634
.194.....................453......530...................*675......487.=....+.........309....224..507.209.........@.138....300..*..$......... 2938824180 2514558898 185053026
.......@..@....846.............%.........714...468...150......88......510..............*...*.......*...........................9.816........ 215265512 376201353 122962654
.....234.140....*..............903..789.....*.*..............*..............631........702.80....33.........513........276.................. 2256368280 2124659642 174743244
.............381.......................*.803...474......267...296.436.......*........................679....*............*........939...784. 3940072369 3208540803 156898292
.....-..........................33&.158....................*......*.......569........487...871/............510......274.542.........*....... 861681698 615421197 14073366
......883.....*163...782..-.............379.764@.....912=..796...91.....................&.......979....907.....*......*........167....+.+873
...........110........*..577........&....*............................*778...................$............@.241.428..949...680*.....513..... temperature-to-humidity map:
....................924........471.470.268..486......../....#......567...........470..469.794....%...............................+.......... 841576398 2731200418 60836938
..725*532......$........@.......*..........*........926......213............405.....*...*......355...615...#....73........151.....896....... 1860695540 395011682 292982985
............980......969....812..28.936.....882..............................&...107.....377...........*.611.......+.......&..737........... 1518037021 3432774193 53954373
....$594...................*............156.....210....585...........234.821.........................403........837..........*....652....... 1786573987 2461900019 55353430
...........................247.@881..7.....*....*.....................*.....*.......*........140.351..........................510....*...... 2940303448 1995108352 6974538
...........790*776...................*...270...778......707.110.....487..486...460.70........*.....#...585.702..430..141...........34....... 3014069287 1664464874 53916789
...52.......................&216..788......................*...................*............199...........*....=......................989... 753139746 2190061656 88436652
....$..445...498.254.508*.............*860..711.48......./.............596....305.....618..........337.............198....287............... 2351822957 2925825589 209990361
.........#....*....@.....352....*..645......*.............328......50.*..................=...-........*584..........*..........*275..986*527 3079884326 3498626816 37609115
.............560..............815.........931......400............*...592......824.........807...591*................311.....82............. 4067060121 4186346579 108620717
.........%.......678.926........................19...*......545..649...........*...............*.....393....=896............................ 4175680838 4102322798 3778367
......834...........*............................/....386.....*......=290...772.............545.969...................679.820......704...... 1571991394 2415899186 46000833
..........122...........202....693.................*..............................555*826..............458......691......*.........*........ 2841627583 1157034722 22534242
....#.......&..%692........+......#..333............41...............=204..................868............/.164..................316........ 1841927417 2517253449 18768123
...346..................+.......$...*......396..315....119..@755...............942..918........................*........631.651............. 2153678525 47765849 108568164
.................=....836......402.755............=.......*.......*....181.62...*.....%........................596......*............#591... 3251281674 1179568964 226737882
...............764.....................................488.....880.209........416..................183...156..........917................... 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

View file

@ -1,37 +1,31 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import sys import sys
import re from dataclasses import dataclass
num_values = dict( @dataclass
on="1", class Row:
tw="2", cards: int
thre="3", matches: int
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 sum = 0
rows = []
for line in sys.stdin: for line in sys.stdin:
first = len(line) _, nums = line.split(":")
last = 0 winning, mine = nums.split("|")
nums = []
for m in re.finditer(nums_re, line):
nums.append((m.start(), m.group(0)))
nums.sort() winning = set(winning.split())
first_word = nums[0][1] mine = set(mine.split())
last_word = nums[-1][1]
first_word = num_values.get(first_word, first_word) both = winning.intersection(mine)
last_word = num_values.get(last_word, last_word) rows.append(Row(matches=len(both), cards=1))
value = int(first_word + last_word) for i in range(len(rows)):
sum += value 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)
print(sum) print(sum)

View file

@ -1,86 +1,242 @@
use anyhow::Result; use std::cmp::{max, min};
use fancy_regex::Regex;
use std::{collections::HashMap, io::stdin}; use std::{collections::HashMap, io::stdin};
fn is_gear(x: i32, y: i32, lines: &[&[u8]]) -> bool { use anyhow::Result;
if x < 0 { use regex::Regex;
return false;
} #[derive(Debug, Clone, Copy, PartialEq, Eq)]
if y < 0 { struct Range {
return false; start: i64,
} len: i64,
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
} }
fn find_gears(x: i32, y: i32, len: i32, lines: &[&[u8]]) -> Vec<(i32, i32)> { impl Range {
let mut v = vec![]; fn intersection(&self, other: &Range) -> Option<Range> {
for cx in (x - 1)..(x + len) { let i1 = max(self.start, other.start);
if is_gear(cx, y - 1, lines) { let i2 = min(self.start + self.len, other.start + other.len);
v.push((cx, y - 1));
if i1 >= i2 {
return None;
} }
Some(Range {
start: i1,
len: i2 - i1,
})
} }
for cy in (y - 1)..=y {
if is_gear(x + len, cy, lines) {
v.push((x + len, cy));
}
}
for cx in x..=(x + len) {
if is_gear(cx, y + 1, lines) {
v.push((cx, y + 1));
}
}
for cy in y..=(y + 1) {
if is_gear(x - 1, cy, lines) {
v.push((x - 1, cy));
}
}
v
} }
fn main() -> Result<()> { #[derive(Debug, Clone, Copy)]
let num_re = Regex::new(r"(\d+)")?; struct RangeMap {
from: i64,
to: i64,
len: i64,
}
let mut sum = 0; #[derive(Debug)]
let lines: Vec<String> = stdin().lines().map_while(Result::ok).collect(); struct SeedsMap {
let raw_lines: Vec<&[u8]> = lines.iter().map(|s| s.as_bytes()).collect(); range_maps: Vec<RangeMap>,
}
let mut gear_map: HashMap<(i32, i32), Vec<i32>> = HashMap::new(); impl SeedsMap {
fn new() -> SeedsMap {
SeedsMap { range_maps: vec![] }
}
for (lineno, line) in lines.iter().enumerate() { fn add_range(&mut self, range: (i64, i64, i64)) {
for m in num_re.captures_iter(line) { self.range_maps.push(RangeMap {
let m = m?; from: range.1,
let num_match = m.get(1).unwrap(); to: range.0,
let num_text = num_match.as_str(); len: range.2,
let num: i32 = num_text.parse()?; });
let gears = find_gears( self.range_maps.sort_by_key(|r| r.from);
num_match.start() as i32, }
lineno as i32,
num_text.len() as i32, fn map(&self, ranges: &[Range]) -> Vec<Range> {
&raw_lines, let mut result = vec![];
); for r in ranges {
for gear in gears { result.extend(self.map_single_range(r));
let e = gear_map.entry(gear).or_default(); }
e.push(num); 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;
}
}
let r = Range {
start: last,
len: right - last,
};
if let Some(i) = r.intersection(range) {
result.push(i);
}
result
}
}
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 v in gear_map.values() {
if v.len() == 2 {
sum += v.iter().product::<i32>();
}
}
println!("{sum}");
Ok(()) Ok(())
} }
fn main() -> Result<()> {
let mut maps: HashMap<String, SeedsMap> = HashMap::new();
let mut seed_ranges: Vec<Range> = vec![];
parse_input(&mut seed_ranges, &mut maps)?;
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 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;
}
locations.extend(mapped_ranges.iter().map(|r| r.start));
}
println!("{}", locations.iter().min().unwrap());
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 }]
);
}
}