Day 8
authorGreg Burri <greg.burri@gmail.com>
Thu, 8 Dec 2022 20:02:15 +0000 (21:02 +0100)
committerGreg Burri <greg.burri@gmail.com>
Thu, 8 Dec 2022 20:02:15 +0000 (21:02 +0100)
data/day08.input [new file with mode: 0644]
src/day08.rs [new file with mode: 0644]
src/main.rs

diff --git a/data/day08.input b/data/day08.input
new file mode 100644 (file)
index 0000000..3001d42
--- /dev/null
@@ -0,0 +1,99 @@
+333430420313204425152200622665264212245045167252605104200413164212660350624632202413410021112040303
+134032100422510245106510040264663362503422541041505357621476703420114122465165643524253354025030130
+210440124144200401002041231025015610061612057522105774716346600327132553305630102152112533024041242
+013233025442442502513652024201322435500523165612141337672674773011016503513304233064521152335042120
+011305252432552501064411033666722407647207507507162465046733615606446206134542053636654320003411012
+243023202214501634062402146610001573103776454700372701707211030076547400104323412251550200540512404
+012253133143312645661144666762653037430267574705006276313562402352550711050455115603136051241513541
+233542100202100442245031231337073750307372173356368622812112644626652722416200145424010111505415531
+113050445521502306510155606560425124341767678204475464563457405544476324177711546422100335421551051
+130312341330642365123453250761202155236155527441423565417554884760031702032102426223145121613433104
+530143503231520461205002652051677511261563711762374740277300428262470663721100666052106105145213354
+151015303344663420237540025132125454561370180385741740506056160664715223770111664734064302411202001
+514034244406063254635323474251671315666526663746817240033573000108656045541344434601625022610052020
+243154262363253554536541354003054186654045878102055585888161073638066213274671743325026010363535544
+154120103402345375743506401360683734777656123267113230466240585461121530607117743057503356131114533
+505455224133247240206564160431257714250053833782497581816130354025606255867122005077143403662520504
+433241563055247633235066320883447875386276657781137827895139991200185503683760166777605024035426130
+303666031235074150733051862308708620362115983146857284227577913250668362434852742536154264141044024
+511222166044117055516803386663311572614519912678966147915185424343732707633260441221576201346435364
+134360166233105100536433310812063681167658735195196145453391339382888328188314274546107317546511612
+523620025403415442506243437017113976271955932743366455248318611782168343828238367826356710005034553
+014143654212533674034560645259292873615816185144293735857682321366761974620115762457417172700305122
+333105652265122475614582321536546127468959745749345269543234433166487289336786316820563170617120623
+121601237502476667431600852195954252478198756563435696885982284312387934896760152481177324330131535
+513446344670703428776508215695735445332247985475848233554657523817331875769331074017245476757301446
+012251345264710602027058214811913234262454325876757887757444777969332385321984700241201345057245460
+332461240272352410086272428238633529256552789239244723226288876562681521623121465728463663000253564
+050241632437771176667281335711828626835432482452477575257825295624648638453256338282617312743052665
+320460534364211482024332384631858292847294377732562824355842225275426886973273352617364252451510462
+266231012324356276581324273442396899956952282898875358753279379896748925691919481576682154760400425
+251566121704323168579128997588889283799488966757983883789954475486796857383783125527355207013547362
+245170254224671131425381468779784773763587376849883899445985425964397253171121657364305543424605521
+343241363651032654076929887424957669374497498475354869687897566288757258268124776805646407142505401
+245235561074546607814525522575333844748378895964779738636436493832747556344467633450422573200613626
+621467001761357111428393119677926772938443995376393853338365997532477292336453235160086048160217366
+023775641054213273228832376568693489696566663398748497398659859837842294529619661172107248203627734
+113467643020563674853798174949656774647946468556996446468936764346927347556233294257603825510225301
+051772112150067612784116555244795748964966494976764476446478663548637694355969461949802873774665326
+331233366625881187249285952947585544548997559569878848947886766655686939429493965123777681254223431
+531573100137313597119916274627286959837544585968649788887976885577683473432893398693858562631240775
+054773273536345576254932477965348359857659449696548949674766533585646392228226613594760535225257520
+634743657825387116635366754892478485999446656868585575764798688597369672379597529612611500237237137
+651102700443476274786872839522556949489444557965565689748499865784355354284683261999963545702441533
+410422511344877258216478752346893586694958864569678896795749775497587467528943274138345032337425341
+673074304265406323296329836734538597845554475567958596678746854759768739593368885149598108068333526
+164263147103304418178278924224955484489949675979877868596747956467863649963887857128138635543007456
+532045787035117465515664676376447636548656879595576977999774677855856735289939791867587713667806712
+140404452768874515592229468574985653794588555889757998699874747589835947525564614257120186050531145
+626535737062375329774963856647668644846998677768989895657468468657744348329253697139398584012102340
+542125001671358961555599377429643566889856477767859887987675655559949868728345434499546123323161001
+762120151054669481822452228565333874958454877687779859759785868573886766599894442765981274505151104
+562471434368683846173563223634735557586587876899579875869878986493584743886292333767594628622775172
+734222626506039543828356435245676596966499685666977779895844487493883497384439766576599302272713402
+267177441831802248969359922326967573465467679688596788687556889843594594235859261867187578441714647
+102117327257157522217556335628537334684966957875575975895695664957459754746968515546793540724822122
+505233476882555673336484437283696496447664879799897577664966999965538753757237631528664630007177613
+172066031365473733492776338746459955449887594496989558546775496577455654236726314131630007341654314
+314412002266354633365762544569746844798457477779477686985845846857578447499935436861745481600772033
+055237658717854148961524344444377964774585475448796459485964778855656967368965165942952602115364653
+101635324187220117445419465884645869853647668548567797785599767597638684836376214415920463048311165
+617323525735135461172377579929749788439689969985978844695495846787466498442754416512973663308107726
+530710323015385832525292779264243858439684558659447696949696563673466249277668864281263486712173314
+002226007723488635329843652368236983569879555555499475484483447567969679832981393855106427002552444
+235757010870250337834974492853239769846747448878894988996873977468347779876273667633884411064437311
+221537334662832777621573374939367297734988578388657548445987535345422574756283382295070667547270175
+521462413004381720189539712492796774975667453383769536485344786668527448878163526268353212024115651
+064167050511487528145433135444328348559358573773345433397876388385483662364771941163658800310756444
+436636405027304747391412742798583342975676858784346378455554898673579853526351275216574538060506774
+566540130441706782811763156886277636247669397646566433399777965674843633877716691920750361127241155
+053372117542772170611448311472432495277284498837535486344595366632773998492323674853732413030542046
+534364447320670867339153474824374957359843939965384798895376292698497378559437674083011730747713713
+216264161444177358545356854382958565667999985377695889365464464565434283664444674351070427062510112
+364206763745554337574296145539342755237347237689765249424576387533835284249337951552318166532717320
+301321356401273810374524947234817483255967848336723823829824987424831571693585243183871374121705463
+013142075522165017716733784396332487689825356344265736822666843742955823165716761855438154413641115
+630515150411425607542463652174216633554978982572463873259338392247874837335324720668014545652155202
+455031465466436525813303414966445845648576498854883558298456269113968383196025720070233635412326555
+513541260473273275035537870749767166583572678469583476276998677949239286734633062745335042001531221
+601365053241646675287550254721413885162954529985825668299219771922524729302706116406630414253264405
+400344346330307036562615004227693316398374195415562381655134627581299617653477441607644245074524600
+210313442047552663082465608147628816371648823764493322559365352994178913344658255573614412766560553
+111352631055464137145152202878508984883298488591213254293969862555144081556414044421631665225522264
+010164431247751037435264713238223247871448917563495625643168725587551433114342107055334272403301466
+525253154605640500025274247121151664615511794958191647464479638471563708865215767367622432614243221
+414144456652451577051364610780083460325111586672621671575123547806751385176766676676633016126156340
+403125420124660622446312308556131072702464323774759977763680287787058873788251453020274036222556015
+010450420461552234703024366261462024624268728424661115870447017746132551326225623245140220643235304
+331053044132010120214026102738858112181065532060281161763064277514475458526554547622102204600502333
+530232021520613242257763671271000321860552177274506581315403280378427012530121320262015343211500552
+513451503556626221314452431160424362623172424403733615107832160275406543265725327322555342222542502
+202051420236066361423130725131562714626884368317212022283380747212427753155731701600356565400542402
+435145215102611325100662266006561366705875616488262665660460450412270343501022642555052262142035502
+334403454455503501622450035064441374653775177026475804121642120210207061757610262641220243402302545
+321311214442661462245102175522241130532337170266155071611254164127776674570166055246211562130500431
+443124503252503622414632304026653173070670661231063766052605435514223357361561150225254553205315111
+232120055124304462512532500502156531032207726437333213051406414333207241265312052356525310410451511
+202002311103141164630106023656017441203731706174163257322311073432575771525344436600220200050051342
+041004212342412120333645230362446732233342662123535575315702023322757200126221443432142400145220443
+420334452105533313552263566256611333163022717266264743363602103371464132116230560501113054442342042
\ No newline at end of file
diff --git a/src/day08.rs b/src/day08.rs
new file mode 100644 (file)
index 0000000..013d55d
--- /dev/null
@@ -0,0 +1,140 @@
+use itertools::{FoldWhile::{Continue, Done}, Itertools};
+
+#[derive(Clone, Copy)]
+enum Orientation {
+    West,
+    North,
+    Est,
+    South,
+}
+
+#[derive(Debug)]
+pub struct Matrix<T>(Vec<Vec<T>>);
+
+impl<T> Matrix<T>
+where
+    T: Default + Clone
+{
+    fn new(h: usize, w: usize) -> Self {
+        let mut m: Vec<Vec<T>> = Vec::new();
+        for _ in 0..h {
+            m.push(vec![T::default(); w]);
+        }
+        Matrix(m)
+    }
+
+    fn rotate(&self, i: usize, j: usize, o: Orientation) -> (usize, usize) {
+        match o {
+            Orientation::West => (i, j),
+            Orientation::North => (j, self.height() - i - 1),
+            Orientation::Est => (self.height() - i - 1, self.width() - j - 1),
+            Orientation::South => (self.width() - j - 1, i),
+        }
+    }
+
+    fn get(&self, i: usize, j: usize) -> &T {
+        &self.0[i][j]
+    }
+
+    fn get_orientation(&self, i: usize, j: usize, o: Orientation) -> &T {
+        let (i, j) = self.rotate(i, j, o);
+        &self.0[i][j]
+    }
+
+    fn set_orientation(&mut self, i: usize, j: usize, o: Orientation, value: T) {
+        let (i, j) = self.rotate(i, j, o);
+        self.0[i][j] = value;
+    }
+
+    fn height(&self) -> usize {
+        self.0.len()
+    }
+
+    fn width(&self) -> usize {
+        self.0[0].len()
+    }
+}
+
+
+pub fn parse(input: &str) -> Matrix<i32> {
+    let mut m: Vec<Vec<i32>> = Vec::new();
+    for l in input.lines() {
+        let row: Vec<i32> = l.trim().chars().map(|c| c.to_digit(10).unwrap() as i32).collect();
+        m.push(row);
+    }
+    Matrix(m)
+}
+
+// O(n).
+pub fn number_of_visible_trees(forest: &Matrix<i32>) -> i32 {
+    let h = forest.height();
+    let w = forest.width();
+
+    let mut visibility = Matrix::<bool>::new(h, w);
+    let mut nb_visible_tree = 0;
+
+    for o in [Orientation::West, Orientation::North, Orientation::Est, Orientation::South] {
+        for i in 0..h {
+            let mut max = -1;
+            for j in 0..w {
+                let tree_height = forest.get_orientation(i, j, o);
+                if *tree_height > max {
+                    if !visibility.get_orientation(i, j, o) {
+                        visibility.set_orientation(i, j, o, true);
+                        nb_visible_tree += 1
+                    }
+                    max = *tree_height;
+                }
+            }
+        }
+    }
+    nb_visible_tree
+}
+
+pub fn best_scenic_score(forest: &Matrix<i32>) -> i32 {
+    let h = forest.height();
+    let w = forest.width();
+
+    let mut current_best_score = -1;
+
+    for i in 1..h-1 {
+        for j in 1..w-1 {
+            let current = forest.get(i, j);
+            let dist_w = (1..j).rev().fold_while(1, |dist, j2| if forest.get(i, j2) >= current { Done(dist) } else { Continue(dist + 1) }).into_inner();
+            let dist_n = (1..i).rev().fold_while(1, |dist, i2| if forest.get(i2, j) >= current { Done(dist) } else { Continue(dist + 1) }).into_inner();
+            let dist_e = (j+1..w-1).fold_while(1, |dist, j2| if forest.get(i, j2) >= current { Done(dist) } else { Continue(dist + 1) }).into_inner();
+            let dist_s = (i+1..h-1).fold_while(1, |dist, i2| if forest.get(i2, j) >= current { Done(dist) } else { Continue(dist + 1) }).into_inner();
+
+            let score = dist_w * dist_n * dist_e * dist_s;
+            if score > current_best_score {
+                current_best_score = score;
+            }
+        }
+    }
+
+    current_best_score
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    static FOREST: &str =
+        "30373
+         25512
+         65332
+         33549
+         35390";
+
+    #[test]
+    fn part1() {
+        let forest = parse(FOREST);
+        assert_eq!(number_of_visible_trees(&forest), 21)
+    }
+
+    #[test]
+    fn part2() {
+        let forest = parse(FOREST);
+        assert_eq!(best_scenic_score(&forest), 8)
+    }
+}
\ No newline at end of file
index cf08101..28b0f67 100644 (file)
@@ -8,6 +8,7 @@ mod day04;
 mod day05;
 mod day06;
 mod day07;
+mod day08;
 
 fn day01() -> String {
     let f = fs::File::open("data/day01.input").unwrap();
@@ -64,6 +65,11 @@ fn day07() -> String {
     format!("part1: {}, part2: {}", sum_part1, min_part2)
 }
 
+fn day08() -> String {
+    let forest = day08::parse(&fs::read_to_string("data/day08.input").unwrap());
+    format!("part1: {}, part2: {}", day08::number_of_visible_trees(&forest), day08::best_scenic_score(&forest))
+}
+
 fn format_micros(t: u128) -> String {
     if t < 10_000 {
         format!("{} μs", t)
@@ -90,6 +96,7 @@ fn main() {
         day05,
         day06,
         day07,
+        day08,
     );
 
     let args: Vec<String> = env::args().skip(1).collect();