Skip to content

Commit 2524893

Browse files
committed
Add day09b
1 parent 567481c commit 2524893

File tree

5 files changed

+147
-2
lines changed

5 files changed

+147
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ scientifically) in [`bench.rs`](./runner/src/bin/bench.rs) on an
2727
| [day 6](https://adventofcode.com/2021/day/6) | [` 0.0027ms`](./day06a/src/main.rs) | [` 0.0028ms`](./day06b/src/main.rs) |
2828
| [day 7](https://adventofcode.com/2021/day/7) | [` 0.013ms`](./day07a/src/main.rs) | [` 0.013ms`](./day07b/src/main.rs) |
2929
| [day 8](https://adventofcode.com/2021/day/8) | [` 0.008ms`](./day08a/src/main.rs) | [` 0.026ms`](./day08b/src/main.rs) |
30-
| [day 9](https://adventofcode.com/2021/day/9) | [` 0.012ms`](./day09a/src/main.rs) | |
30+
| [day 9](https://adventofcode.com/2021/day/9) | [` 0.012ms`](./day09a/src/main.rs) | [` 0.036ms`](./day09b/src/main.rs) |
3131

3232
| | one-by-one (1 CPU core) | parallel |
3333
|:-------------|:----------------------------------------|:---------------------------------------|
34-
| _everything_ | [`0.769ms`](./runner/src/bin/runner.rs) | [`?`](./runner/src/bin/runner-par.rs) |
34+
| _everything_ | [`0.793ms`](./runner/src/bin/runner.rs) | [`?`](./runner/src/bin/runner-par.rs) |
3535

3636
## Run solutions
3737

day09b/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

day09b/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "day09b"
3+
version = "0.1.0"
4+
authors = ["Tim Visee <[email protected]>"]
5+
edition = "2021"
6+
7+
[lib]
8+
path = "src/main.rs"

day09b/input.txt

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
5796798621237995498765434567987542999765679987545679109878999877899789876532123456998999876887899921
2+
4645976434456789349654321298997679898654698987635678998767897656789698765432012347897899865676798799
3+
3234987545978993298795410989998989789543256897646789498756789546896579877842123456976789954345985678
4+
4356798679989999019987329878999798679765345689856991296545890134789467998956899967895698643239874579
5+
6467999789999898934976598967987676568996457999967999987636789345691346789769987898934987651098763456
6+
7598997999987796899989987959876543456789569898998998765125678956910298899898776799123998862987652345
7+
8789876789865685578999876645987665567998689656789876554034899999891989998987564679099789879876543456
8+
9898765498974324456799865534598786788998798645889985432126954987789878987654323567988698989987664678
9+
9989997987543212367987654323459897899549895434569876556437899876599967996543212459876587898998798789
10+
9877989997662101456798765434567998967932987624778989787548999989679459987687301349875456967899899893
11+
8765678998983232367899896765788939346891295434999999899659879699894349899796532656954344756910923964
12+
9874789019894343456789949878999321235799987545678945998778964578942139769898543769843212345891949765
13+
7965678998765764567893433989876542346987898656789236799889653989943298652999678989754523456789898976
14+
6598789109976975698921012398999763457896559867997647989998799899874569541098799798765676579896687897
15+
5439898929989989789962123457899894568989435998998799878999989798765798432149898649987787899965456989
16+
5212987898999999899854336568999989879979326569109989567898879689878987544234995434599898999874345679
17+
4309875987999876998765687679989878998765412458929875467987854599989698955679989323459979899993214589
18+
5996954676799984329877788989878969899874324567899765359876743489996549877789878912398767789984323456
19+
9875432545789743212989899998967659789985595678998868249965312679865435998898767899598954698765467567
20+
2994321036897654101996936987654543679999989789997854198764301569979423459999954678997643789978578978
21+
0987432128998763219875424698763212478998978999876543298773212458998901267898767889598732345988789989
22+
9876544346789979329876512349998401569897569234987994987654343457897892478999878993349891234899998695
23+
9989875679899898939984701459886212398786456946799889998795464567896789989896999321234989345678987544
24+
9997987889998797998743212598765323987654397899989778999989978679945698999765678930349878956999898433
25+
8986798999987676789655329679876439876541289998878656899867898791239956799954345959499867897899765321
26+
7845679219878565678976598999987556998732378997667545798656789892398745999765237898987656789998654310
27+
6434798998765454567897987898798677899543467896553234987545878989499636878992156987975345899219964324
28+
0123987999874343456789876799549789987654598998421056989434767879976521767989249876543234998909878434
29+
4339876798955102367898985678939891099967899876542159875323854568965410156978956997684456797899989545
30+
5498765986543234488987894799012999129878999987653249986210123467894321249769767898795867956789199656
31+
6599754397654545589996789899993998949999998799767998975351237998995432398758998989986788946999098789
32+
7988732198765657678975698999879876898946999544979877989876456789987643459647899876097899534878999899
33+
9876546019878767889984567899968765667939896532989966592987887893298754598756789965198997323456799989
34+
0998757898989899992099789988754543457898789540199854301298998984129866789998999954249986412345899878
35+
1299898987699954954299999876543212348987698921598763212349999873234977995679439765356897601234988767
36+
2989999876569999895989986987654103467896567892349854334568989964345698934678921976897996532349875756
37+
9879899988698988789878995498976215989995456789498999965689879899456789323567890197998987656756994345
38+
8765677899987677697667896329865423499989997995987878897798768778968895438678991298969398967899873201
39+
8654546789766563459548994219876534578977789104986567789949654567899976657899789349543219988932965412
40+
8643437899854312998435689101987645689665679323975345678959868778957987798934679959654301299549876723
41+
6532126778969409876424578912498756796553568939863203456899979989646799899012567898976432358956987894
42+
8544434567998912998535689843569867989432459949954212568999989594535798989123467987976563567969799985
43+
9655566789897893479697897654567979879421267898767343467898795443123497679935679876899674789997659876
44+
9776789998656799567989998765678998967992349929876556578987654321012989567896798765678985999876642989
45+
9988899875434678979878999879889987657889458919997967689798865452199867456987987654567896798765431096
46+
8799943989545789999868799989999876545678967898789898797659979874987654347899999753459987979876532145
47+
7679969998656789987657678994323965432789979987679789896535989995998321234568939894567899764987687236
48+
7567898959767893976543467995909876645678989996545678965423599989899754359679421986789968973199875345
49+
3467987644978912987784989879899987856789999987434569653213679876789866468789439898993459792012989466
50+
6569999533989999798896797867789898987897999876424678962102398765898977578996598769322375679933496578
51+
8678998921296789679987896545698769399986789764312398993923499654567897689398999954201234567894987689
52+
9799867890145679542998987896789954234995698765101256789894987543458999791249899895312347899995798789
53+
2988656891237789869879998998996895999876789864212345996789987652377899910299756789436556799989999890
54+
3977545789347899998767899989434999879989898987423467895678993210466789321987645679987867989767899921
55+
9865437896556789999946799876545998767999987976534789954589997621245679459876437898798979876545978943
56+
9976545789697899987834689989699767656899876989675678932679876432556789598943218997659989765434567894
57+
9989867899989999986545678995987654545998965498797889321996997543457898797654323789943596986546789965
58+
8896978999879878997668789213986543234987894349898996549875698665678909998965534567892345697656798987
59+
7645989898765655689779898901987662129876789234989998698954539987899919879877645689931257898767967899
60+
9869998789964334578995937892397654398945689345678979987643012398967898767998776798890234999898957898
61+
6998789679892123457894325943498765987834568956889467898952134579656899656549887897789656799999546457
62+
5987654597651012349975434799579989865423567897992378999763245678947999743435998996549767987989432347
63+
4398753299843123457896545678992099654512455789209989398754356789439879842124999987638989876678954456
64+
1239854987653238768998956999989298743101234678998994298765768899598765431029892196547898965466795567
65+
0123995799768545678969869878979349543213455789997899109976779998789987532139789987656987654345789979
66+
3235986999879656789756999867668956975434696899986987912987899999894697643298678999987898543234567898
67+
9945799876998798897645987654456899876595989989765976899998999886989987659987567998998999864365788977
68+
8799895995439899986534598732367968988989878979954695678919998785468998798766456997899898765479899766
69+
5678923994323999876545987543478957999976556767893254589101987674378789899854349876799649876678987645
70+
4567919876214899987689998954989546898765432356789123678919986543245679998765467965678999987789298756
71+
3467898765436789998998999879995434999896521237895019789998997532135998899977569876789988798992109867
72+
2369929876545699999987899999876545689943210235994298999997989949239876789989778989899976569993298989
73+
3458910997676789899876989212987676899656432346789987889886979898949965679999899998998665478989987897
74+
4567899998787895798765678923598789998789545497899996779765765787898764567878921987999543289567976546
75+
6878978999898934989894569654589890249899876569999884568954654656989843458967899876998992123458997997
76+
7989569899919129878989678979678931239964998978998763477943212349879932123458998765876789012567919989
77+
8991356779909019767678989989899842498643239989987542356799302345965431014568919954345692139898929879
78+
9410124567898998654567891294998753679654134699876521245678913459876432323589109765457789256799598767
79+
4321267898987549876899910123987654598763245698776410234569865567997943434578999988767899767895349654
80+
6532356789876434987898943235698765679854659987654321345678976778999894565689989999878949878943298765
81+
7656467893987545998967896545699887998767998998765432456889988999998789689789878989999432989752129976
82+
8767589954899756899459987756989998919879897899876548668994599989987678999898769878997643496543234988
83+
9879678975798967987598999899878959102998766999988767899123678978986567894987653967898765789759449999
84+
2989789996987898998987899954969643213459945789799878943239989865454478923986542656949896899898998931
85+
1299899989776999129976899769878965425678996999656989965398798754342349435965421248956997899987687899
86+
0467999876545789098865689879989876566789987898768997897987659983210456949878543367899898999876546797
87+
2378988965434678987674778998999998987893598999879876798965434975672569898987665456789789998695435896
88+
4499877994323569876523467897988999999912459999989965689896323497883479767999787578995679876564326345
89+
9987656789212489986313568966767897899106598989999984878789212598965998956799898989664868975432101234
90+
8798878994323478965423459954456986798919987678999873165678943679879876545689939996543459876875212345
91+
9659989765434568976796567893299765987898765569898762054567894578998765434696549987654667998994323476
92+
8934599879876899997898879954987654496987654346789943123489965679539854323589998998767898989889456567
93+
7895678998987899998999998769876543345798765767898894344567896789329875434567897689998999878778967678
94+
6796989987698969799998789878998632134899878998946799465679987898919987645679976578999098767669898799
95+
5789995799549347689987678999986721023999999769434878987989998967898998767989787459989298653456789910
96+
4567894698432134567988566789875432335698798653212967999997859456987679878998643212578987832345679891
97+
3779992976553235679977455698998765487987689864343458999876543237897569989797652101459876543456789789
98+
9889689987664346798765323456789887569876598765454569986987652145789698795698768892368987956567895678
99+
4994567899865457999974313345699998798765439879875678955698767234999987654229878765456799767878934589
100+
2123456999876567899875101256789109999984321989989899543249878945678996542101989877667899878989123699

day09b/src/main.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const NEXT: [(isize, isize); 4] = [(0, -1), (0, 1), (-1, 0), (1, 0)];
2+
3+
pub fn main() {
4+
let mut input = include_bytes!("../input.txt")
5+
.split(|&b| b == b'\n')
6+
.map(|l| l.to_vec())
7+
.collect::<Vec<Vec<u8>>>();
8+
9+
let mut basins = vec![];
10+
for y in 0..input.len() {
11+
for x in 0..input[0].len() {
12+
(input[y][x] < b'9').then(|| basins.push(basin(&mut input, x, y)));
13+
}
14+
}
15+
16+
basins.sort_unstable();
17+
println!("{}", basins.iter().rev().take(3).product::<usize>());
18+
}
19+
20+
fn basin(map: &mut Vec<Vec<u8>>, x: usize, y: usize) -> usize {
21+
map[y][x] = b'9';
22+
NEXT.iter()
23+
.map(|(xx, yy)| ((x as isize + xx) as usize, (y as isize + yy) as usize))
24+
.fold(1, |acc, (x, y)| {
25+
match map.get(y).and_then(|l| l.get(x)).map(|&n| n < b'9') {
26+
Some(true) => acc + basin(map, x, y),
27+
_ => acc,
28+
}
29+
})
30+
}

0 commit comments

Comments
 (0)