From 3ac09dfcbd7384a0a2d2a2daf7c8e99355fec0e2 Mon Sep 17 00:00:00 2001 From: Lars Martens Date: Thu, 9 Dec 2021 13:44:11 +0100 Subject: [PATCH] Day 9 --- input/09-test.txt | 5 +++ input/09.txt | 100 ++++++++++++++++++++++++++++++++++++++++++++++ src/bin/09.rs | 92 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 197 insertions(+) create mode 100644 input/09-test.txt create mode 100644 input/09.txt create mode 100644 src/bin/09.rs diff --git a/input/09-test.txt b/input/09-test.txt new file mode 100644 index 0000000..6dee4a4 --- /dev/null +++ b/input/09-test.txt @@ -0,0 +1,5 @@ +2199943210 +3987894921 +9856789892 +8767896789 +9899965678 diff --git a/input/09.txt b/input/09.txt new file mode 100644 index 0000000..a073da8 --- /dev/null +++ b/input/09.txt @@ -0,0 +1,100 @@ +6546798789123567965567895323943212345892129763235999432345678976432345679987654524567998765325689236 +5435799654012459943456789519894301234679098654569898921234589765321234589878543213567899886434990135 +6656987643234767892345995439789213345899998765699787892345699876542367898765432101456789876556789296 +7767898759349978901256896798679354476999899876987656943556789997653458999897546212345678998669893989 +9899999998998899213457999977598967568998789989877545894567898989767767987969765423789789239879992468 +1998899887876778924568998765467797679998678998765436789678997978978879896459896634567892123989989567 +2997698766514567935679987654345689789877567899854323899899986769899998764367996546678943434599878998 +9876598654323678956789898865256799898765478999876412910969875657799019653235987697889655675678967899 +8765459865445789987896789954367897997654349889987567891459854345678998774134699798998787988789456789 +9879768979556789598965679875679956789765199767898778962349763256789769892034789899999898999895345689 +0999879988767893459654568996789545899976988757969989543969854367899349973125678910988999999989656789 +2399997899878932398543467898897656999899877543756799679898765698978998763238789999977899989878967893 +9989546976989321987652456789998797898798765432345678998789976789654987654349892988766999878868998912 +8878939765698939898101345892399898999679986565466799879657898996543498765656943976845998764456789101 +7767899954567998765212676901988939987543198976877899765434999899432129897967899865439898752345894342 +6656899875678969874324567899876323499654589989988999889325998798943012998978965986549765431456789493 +4345789989789657985639678998765434598767678994599898765439897687892135699999234997678987210567896989 +4236798998996546987798989999877645699878789323978789897597656576799246789852129898789598997689954678 +5345987987895439898987899899999876789999895439765678989989943465678997899643498789895459798994323456 +6559876466989598799876798789899989894698986598979789878967891234569598988954989679954345699783212367 +7798765345778999654765789679789795913797899986898999765456910123789439567969976568943234987643102398 +8999894234567898743234897545679654202986798875787899976345691345678923459998965457892195699854253989 +9989982125789987656126789423599967319875987654546678976456789467899434598987954345993989899964359878 +8779873013499876543245893212999899497954987543434569876567899878976567987656893234589878999896479868 +7669765638578998757656789109898789976543498662123478987678965989997679876545994445678967998789997656 +6549876797678969767767893298765678987652398743236567898989654598998989987656789556789349879679879745 +5432989898989659889878954987434567898921019954356789989490123567899499998767897697894212965498765432 +6321499979693245999989769876545678989892198765579899975321235679976579889978998989943209892349874321 +5432349765432134578999878987656789978789239987678998765435346793298679767899329878954399721254989510 +6546559876593456679678989198769899865678949898899899976567458989109798656788919769895988670123495432 +7677867998989567989469893239978998654889997669956789987689569878919898543567998658789876541234987543 +9988979789878978992398754345989398789992986544345993298798998657899997632699876547678987632349999654 +8999998694567989421239897659891239993101297431234889019987654346789987543987654434569999743498998767 +7899876543459993210234998998789345679219398410124678935698798759898998679876543223478999956987689878 +6598765432568996521395799765678956989398987324238799549979899878987899989987651012459998769876545989 +5439877521256789434989998654567899899987996437849989698764978989456793299875432123467899898987632193 +3214986530235678949879899767978999789356797656759878984323469994345679099986743254589967987899991012 +5423497621234799998765788978989998679218899878899767965434678963234568989987654357678956996598789134 +6765986532345699987654577899597987568909976999998756896545889652134579876598765488789449895498678945 +9876998543656789695323456789456798457899345789998745789656797841049698765439876569899239784323569656 +1997899654567896545212968994345965349978956799987656999878986532198999654323987678987997673212398967 +0198998765678985432109899893239876798767899989999789129989998783986598767534598789876798532101767898 +2999639876989876543298789679123987899754978878998991098797899899885439898645989898765696543312356789 +9899421998994987754987674578994699989543457867897892997686799998765323979769878999654987654323487899 +8798990999895798865796543456789798765432323456976799876575689876543212367998767898943299765456569989 +7656989896789999978999652346799899898431012399865679765434799987694103456799856987892123976567878979 +5445976785679899989498783456789943987652123987654789865323459998989214567989649876789034987678999567 +1239895423456789894329854567897562398543239876543498954314567899768925979876535965679123498789410456 +0198799212678998763213965678954321249884345987654567896501234689656939898765323984568965679894322345 +9987678924579997654329876789873210398765467898775688985432345794249899789543219875679986899995436756 +8656567899698998765456989892984332469876578999889899876543456789098798689784301989899997898987645667 +9543456988986889989689999921296545689987689999999901998754568899988657578965513598968999987699987878 +8654579876575679998799899890987679799998799987678922789865678999865543489654324987659989986599898989 +9797698785434457899899798792998789898759999876569934569978799899874212398987439876548778975476789999 +2999789654321236789997687679899893976543497683457896678989896789965101237896578985434567894365679989 +1299899869210234599876593568789912987675986542346789989995955699876712346987989998756788920234678979 +0988932998921347698765432455698909798796987656787892499954334578987423456898999999869899321345679768 +9877990987892656789984321023567895659989999968898931249893212567897654567899989889989995432346896556 +9866989876998797899876432345679954349879989879999545698789993479998967678910976779799989543456789345 +6755576987899899934998765456899873198967778989998996999666789589979879789799875567679979655668893239 +5444345698998932123499876898998762097854567899987989876545697678967989894598964324569868987878965698 +4321234579996541012986987999298754996532479999876878932123489789459999943987654213498757898999879987 +5210123459889953329895698992019969875321345698674567891014678992398989312498643209987546799921998766 +6332335598779894459744349989929899965499956997543456789123499101987678943459654398765434897892349854 +9455447987669799598635234579899789876988899876432356789254689219876567896598975569874323456789459763 +8976559876556678965420123498798679989977789998553678998765679429765459987997897879963212367899998672 +7898667986434579876533234987687567998765679999654589329887789598954345699896989998654323478997899431 +6799899797415689987644349876575456789854567989765695312998899987953239989745879998765444599986789520 +5679987654323578998759598765432367898765698969878789423499999876832198765634567919987655689875678991 +4789999965437679349898959886521278999898789643989898746589999985421019874323788901298766895976789989 +3498999876545789199987845995432349899999899891097987657678989995434198765435699899349877934987894679 +1237999987659896989996534987545456789998943989196599769989878989975239986745797688998999545698923488 +2546789398967895879965423498686767898987659879987439878998767967897845987899896567897678969979012567 +4598990269878954567891012569797898967898798769876321989987658956798966798998944456986567898764323457 +5679321256989766778932123459898999656789898858965452397898769345789978999997932349976456589977444868 +6898732349896987889994535678989989745677987547896543456789891234678989799986521598765343467896576789 +7986543498765698999989689989679876532456799856987765687898910123489995679865432399763212398987987998 +8997959597654579999979799999568987321238898769998989788987421379678954299876654987654523589998998967 +9219898999763457898768989878467995432545999898999399899876532567799765989987765699765787678999539456 +4398787898932348987656878953359876545676789987993256901987656678949879978599876789876998789998321345 +5987656997891259876534567942234998656889899876989347892498998789323998765432987898999899999887310156 +6987549876789345984323457910145679968999998765679458999999129895434569886753698956898792398765321235 +9876732997896469876512367891236789879899899654589999878891034989598679987864569546799689989898752376 +9765431298999598432101278954345678998765679743468789456789123679679789898985689435986567978987643457 +9854320999998987643212349765678799249876798632345679347894234567989898789876790125987878967498784567 +8765439899897697656323757976789890129997896501257896556965345689999977667987891234898989654349895878 +9876598776786569997954769987996989298698965432348997867898766789898765457898942346789497901256976989 +4998789654692498789875899998965679397589876543456789978999877895679854366799657659892346892767997992 +3459998943210989688986789879654798966467989754779899999899998954298789245789798798901237789879989891 +2469987894529877567897898764323987654356899875678999878789109760129656166899899987892345678989878792 +1998546989698765486989969765934976543234567988789999764678919871299641017910999876793456899098766689 +9876534878987654345678959899895987654745678999999989843569329792988432199899998765689768943199754578 +9985423568996643236999249989796798965656789349989878992978939654976546978778999654578978959987643467 +9876512459985432126789198765689899877767893298765456789899998769997959864568998643466899998998752578 +9984301678976321025689999864579999989878999109874345698799899898999897653459876532345789987996543499 +9875214567987532134567899543689987891989998998765456789687789997698789762345977541234599976987657567 +8765323456898653465789998654568996542399987869989567896535678987589678943459865432545789665699768978 +9876435677998754567893469765879987653459896654398678965423599866476568964567979543456796543224978989 +3987576798998765698912579876789598767598765431298789986210987654324467896789098756767897656102989491 +2097687899329976789323489998896439889987654320129891397321298864212356789892198768878987643212495320 diff --git a/src/bin/09.rs b/src/bin/09.rs new file mode 100644 index 0000000..8d7a9a9 --- /dev/null +++ b/src/bin/09.rs @@ -0,0 +1,92 @@ +fn main() { + println!("First solution: {}", input().risk()); + println!("Second solution: {}", input().part2()); +} + +struct Field(Vec>); + +impl Field { + fn risk(&self) -> u32 { + let mut risk = 0; + for row in 0..self.0.len() { + for col in 0..self.0[0].len() { + let current = self.0[row][col]; + if self.neighbor_values(row, col).iter().all(|&n| n > current) { + risk += current + 1; + } + } + } + risk + } + + fn neighbor_values(&self, row: usize, col: usize) -> Vec { + self.neighbor_positions(row, col) + .into_iter() + .map(|(row, col)| self.0[row][col]) + .collect() + } + + fn neighbor_positions(&self, row: usize, col: usize) -> Vec<(usize, usize)> { + let mut result = Vec::new(); + if row > 0 { + result.push((row - 1, col)); + } + if row < self.0.len() - 1 { + result.push((row + 1, col)); + } + if col > 0 { + result.push((row, col - 1)); + } + if col < self.0[0].len() - 1 { + result.push((row, col + 1)); + } + result + } + + fn part2(&mut self) -> u32 { + let mut basin_sizes = Vec::new(); + + loop { + let mut fill_start = None; + 'search: for row in 0..self.0.len() { + for col in 0..self.0[0].len() { + if self.0[row][col] < 9 { + fill_start = Some((row, col)); + break 'search; + } + } + } + + let start = match fill_start { + Some(f) => f, + None => break, + }; + + let mut fill_stack = vec![start]; + let mut size = 0; + while let Some((row, col)) = fill_stack.pop() { + if self.0[row][col] >= 9 { + continue; + } + size += 1; + self.0[row][col] = 9; // Mark field as processed + for (nr, nc) in self.neighbor_positions(row, col) { + fill_stack.push((nr, nc)); + } + } + basin_sizes.push(size); + } + + basin_sizes.sort_unstable(); + basin_sizes.into_iter().rev().take(3).product() + } +} + +fn input() -> Field { + Field( + include_str!("../../input/09.txt") + .lines() + .map(|l| l.chars().map(|c| c.to_digit(10).unwrap()).collect()) + .collect(), + ) +}