From a8cd0b068820fd2091090bd7f22df3ea97b0ec85 Mon Sep 17 00:00:00 2001 From: Tobias Berger Date: Fri, 9 Dec 2022 18:50:12 +0100 Subject: [PATCH] Rust Day 8 --- rust/Cargo.lock | 2 +- rust/Cargo.toml | 6 +- rust/src/day08/input.txt | 99 +++++++++++++++++++++++++++++++++ rust/src/day08/main.rs | 11 ++++ rust/src/day08/part_1.rs | 78 ++++++++++++++++++++++++++ rust/src/day08/part_2.rs | 54 ++++++++++++++++++ rust/src/day08/sample_input.txt | 5 ++ 7 files changed, 253 insertions(+), 2 deletions(-) create mode 100644 rust/src/day08/input.txt create mode 100644 rust/src/day08/main.rs create mode 100644 rust/src/day08/part_1.rs create mode 100644 rust/src/day08/part_2.rs create mode 100644 rust/src/day08/sample_input.txt diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 113a963..5bfe0c3 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -4,4 +4,4 @@ version = 3 [[package]] name = "advent-of-code" -version = "22.7.2" +version = "22.8.2" diff --git a/rust/Cargo.toml b/rust/Cargo.toml index b78efa0..cf91b72 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "advent-of-code" -version = "22.7.2" +version = "22.8.2" edition = "2021" resolver = "2" @@ -51,6 +51,10 @@ path = "src/day06/main.rs" name = "day07" path = "src/day07/main.rs" +[[bin]] +name = "day08" +path = "src/day08/main.rs" + [dependencies] # Required for one of the day06 solutions (commented out) # phf = { version = "0.11.1", features = ["macros"] } \ No newline at end of file diff --git a/rust/src/day08/input.txt b/rust/src/day08/input.txt new file mode 100644 index 0000000..6e16d6f --- /dev/null +++ b/rust/src/day08/input.txt @@ -0,0 +1,99 @@ +023322201321333021030055115233032255002015013044155106103011355435025324002515233340032023013101000 +333001114104040220402023231114145321226614100664520643140442646451222053211251540001413110202310311 +001122230342102202311114141015535535515054203105023201320052153606250133240404425023031241331132300 +200333324214423413533144232012440022651463612352410034332230305463511214140020132330411142212122132 +102231001140012234010224535134246522662353351650411444516502333264512265503131345351241014030131031 +123304301244220202523215244250456660036136615601260455124040610534545066145403001311004201223131020 +002003410244152204332045034034653220253344012342462512115252223350162403054433114541414402004230003 +200103132341510422452452250254525020361416010364344643552146232455465641450614242024420434042322023 +334030404131124213210110065630054201305051151553713765154353152015404165650352432220042240121431441 +310413343322502004445560402155236460562124231544313721765226711114232542101026341112103205000204213 +232113123250204533056410314645353415455472754633613676614123217267412616420454632514221110454141242 +212320215533235344164103161651553275174711711164356766211341364224361343166521543245501043403313020 +132311140310204051126312005021325536142561672714571554376563517456554662362641056124005412144432424 +231444210534331154552403515511564757547561574455777217737432721253356756144650425243250053400131102 +402305010405005545336224323172551713366727316646463352447124162453153245317302364400260244035250203 +141021340213551650603550523267762213732351446246376356435541361325455465416164254305301451555014314 +020311351332141136563303441244763611434365535243774772754265656372342514675761031061364550351222431 +413245211550112425253426472765645176882866845564838257856366634524532753142526650514203343254534533 +435452451101402442345414221334613563324588667748572632844384676268337331257112272444312406000210430 +010335302204230321512415571174254452865672753726557623855355483625486653112141562116114316324324330 +221535255034163464174647563575653473563783762385456872577376838278724787533322241360046164145143444 +023200114430135341415733511326526724522547637566455625677487874884677748355767326531554460531505423 +041325411513220355333664623324656723324354775368994776576453743226382234255146721766626165345032040 +251500552152654263475146756832747868664284795636459947469897787877228355585665321152065265452205010 +140403120623541575137615727682856477356763944336369569588959794665537746822241226172105532241014054 +453231451142201467644523625687875786636878558948395383658853983478274336626223261322670214456000012 +421401202553552263622464483244566634338573647446753646346899559455467362654676475666633635516251041 +323131612232542573237614644344532373875676439947597665797366646687698528458322143111266523602124520 +350402001311666477622377487567429458933773439373736474937847339863733784866824546353257615331613522 +250022536362331172722668268236867398553738597646787654948564865538536978368448574575767731313436224 +402502304364172157656277664376787847997386975947666779469869555838787877535542353742444675210555330 +534146105561234616268536526749363399896897558457877796995459475968646953877355285715651641221535521 +303646554131712176733825365433864398786578577698887656988984667665877535682343345654563777135012425 +324510121102677116583635857975693374387486566897798478994684955676694383688884846856322762430431244 +514510651516612266887483855356674543785959656848847555648447748596549489474486425724612453763154612 +231062261547177317255662764857346469768747497764945945965757496698454569588782672772143151160442034 +320163342042441177863252689394446487894788488755875979945694655996536354949538842337254673262442021 +332245516535336165424866833774775969444884989558587897695567778655663877687674655723514215474233645 +305304310765327184528544539776454864657499589567575587578857495776446886984958232865323375223161226 +041321134716326427345342674747478687768869565659869667675778958466596675773462357464554753265120531 +214135547421514268628575739385987456987579588577977997755957594894869954387663668252657636412005462 +303014312474744334438854664364557544877859878676955887957766794676788758349967423548311115421012031 +165600101766416548688563985883658885658968698975789866869555596449468493399749732764355275156263540 +023235337273751276356326956636445746559765659596677778588597776965655557544476853383777537142222045 +263022507373222354242473545837465487886578985999866897786966667659976798784598852254766575426052230 +051533463326323676244499536339757747598658665666877776665556856698587598784344845678845431463000630 +350253347271711387537784387454469565865799697798999978977698659775896784977364666572732246234315203 +341005566337216558547638778897554744866597657879686786676958688799857477968856776862486125354403122 +621400523354423736428594359887676575878885656668779879866568999864874549669688988364688346235212421 +505135676316352285588875489864578745965769688688697676766658757779794959756587955783527662755755550 +412201242457267256768457574464866864796989986797678879786798988756654489745697566438648525314315120 +465513676443563625564338436498947956778678989979778889888765868665989946774539564524422761417235426 +232566141256427682382546587547954498979759697699669866686878965577999966363744744433546335352521216 +426641574412112675686545366888568858676989567798688877989897665567789554385448648853822213741324260 +602662554527175344426766863695666656977788897678788977877689686966648487456799824486782375417461456 +651655223645567747258565638365989956656677767968776687998958899596855768455367926645753536315666440 +212101665433515738883643775536779945579976596567786779666997868695869664755484454857473235121532445 +050653146543541364346323653736454948456785699959977797599687588797759765975388673684657445217361543 +064052466421561857423457964965989558587599968966579676777587688855745463996659346764634531763304515 +561551524332754584232789895869594554468566797988665678658758669559768854455376555847876562742331036 +325314542655721556272469893855789755477667687765899758695565678476456955583865232637557334756250066 +142312051576173167535685734664695895767875888777758989579876988865676775699635377648226576522411336 +264405030674612265825464755969447459794894978557578676887898777658666398378833868636241721763463230 +226106504172532322756823879646746684578495976958986858585948768946563458695562852538355727621152553 +010455552047535263283644464733478545978574965849565776774485474589895848955474423534356723663335362 +344641162242761347244832643748385795576889884668584468667945796945977446594377836734377545264505202 +034234561142745745528645734559984454564945447687598468749845599859594766854257575231153232601521102 +540240251242253675342846365745937738658974994554845749686786757743643844958548825225537647141563520 +320206262105457111733842244254676634937587947685687474475998874887466954622334232535552551031406142 +514054400340122471674738287345975345554989578889687665977944569694376357735848686622213124245443111 +052523034313452374556723742753478844856887759998965975878847759763497772425467874322521153360256440 +222351100013277574717265537284546936359665666859668677959565397475778974448826377463727135443542405 +523350424041153637642545773628235636497539933545997447966795573653867437626874225421751645016443433 +312215360245443761525542452352242557749988469436989969593867853433893324724227766423165014506020545 +043301115145336365333177277445276796874975464449775989576653853847933547267577416233622000500250204 +010430464664035143116714487846468273747588694587534759975435534953667857468842565117553510343230004 +111523155243403247241622253657835525667595756477383976646535657887238383548526267564106666155410400 +331143444424136613727453361363448436384873986369887364654778533252327534346555721777153155043314351 +504400535003012413114454471286326822622755886597475895367832256758387882813774733523434423050344153 +031223513454351150575175721474553686462624367655585284263625746377334236266723565566434350565141505 +025403550435001645163142764332743577336357527577475885842585236426673634564751755732464325035342155 +330421145016464561337162167576453887346346662377775778365787447636782124712553177545200210154304402 +145134011342136360041775757661255522836536453757768526277233484847723214161226742551423535541545141 +120424442054514402401215773774217135788666446728752473458828676541113522171226164646163420350045352 +320300042111566320001225347233316661324787538457643633368888833762234461732376203555404440133510432 +040041531054536240250125277267321574523425854345828833847341557475227253772735231005626405434053110 +132032504525425163004536034651157757674257322776353873743315142663344157512332514161361250434024042 +412131202502245064331035365341616236636215157647137215154547671162456657640014205623004340003440042 +023402254450222445315156123064462256711636662722324113154653243672151665651410655544242445235033214 +030244442213354225245214420026615315266333321464545152275426214253367063445442123421222354522130443 +000404231340354415013353552601201031525655623675321422372514761615551410213215314045514055253413013 +424431424142012102050025660124656001446655324426514434456334347361251512153650510232214515000212124 +210444432021415253200025444324242403030613314565321154125546321420536312644040231522244324413232134 +304213344021421403321314610146511434231323466075612476301612210064435402534231034321214151220134113 +002213301410344533212501252101020156640662021153562032241365344241433044503534355443400214003434001 +022314333231043300404453132012064266633361244266541036223442060142103261113425041124401240222232000 +311003213421304332112132501310051205416305264342531341103003055416252025531115042105141422320032103 +133213004320332415205534404040532521244601552314401414553622300634114003055144015221303103000233300 +121313142330141112135522531401154344545521012501566652210433310564254034403545252414443110303020313 \ No newline at end of file diff --git a/rust/src/day08/main.rs b/rust/src/day08/main.rs new file mode 100644 index 0000000..cc219d2 --- /dev/null +++ b/rust/src/day08/main.rs @@ -0,0 +1,11 @@ +const INPUT: &str = include_str!("input.txt"); + +mod part_1; +use part_1::part_1; +mod part_2; +use part_2::part_2; + +pub fn main() { + part_1(INPUT); + part_2(INPUT); +} diff --git a/rust/src/day08/part_1.rs b/rust/src/day08/part_1.rs new file mode 100644 index 0000000..8a742b0 --- /dev/null +++ b/rust/src/day08/part_1.rs @@ -0,0 +1,78 @@ +pub(crate) fn part_1(input: &'static str) -> usize { + let mut visible = vec![vec![false; input.lines().next().unwrap().len()]; input.lines().count()]; + + // Left-to-right + { + for (line_idx, line) in input.lines().enumerate() { + let mut biggest_char_in_line = '\0'; + for (char_idx, char) in line.char_indices() { + if char > biggest_char_in_line { + visible[line_idx][char_idx] = true; + biggest_char_in_line = char; + } + } + } + } + + // Right-to-left + { + for (line_idx, line) in input.lines().enumerate() { + let mut biggest_char_in_line = '\0'; + for (char_idx, char) in line.char_indices().rev() { + if char > biggest_char_in_line { + visible[line_idx][char_idx] = true; + biggest_char_in_line = char; + } + } + } + } + + // Top-to-bottom + { + let mut biggest_char_in_column = vec!['\0'; input.lines().next().unwrap().len()]; + + for (line_idx, line) in input.lines().enumerate() { + for (char_idx, char) in line.char_indices() { + if char > biggest_char_in_column[char_idx] { + visible[line_idx][char_idx] = true; + biggest_char_in_column[char_idx] = char; + } + } + } + } + + // Bottom-to-top + { + let mut biggest_char_in_column = vec!['\0'; input.lines().next().unwrap().len()]; + + for (line_idx, line) in input.lines().enumerate().collect::>().iter().rev() { + for (char_idx, char) in line.char_indices() { + if char > biggest_char_in_column[char_idx] { + visible[*line_idx][char_idx] = true; + biggest_char_in_column[char_idx] = char; + } + } + } + } + + let result = visible.iter().flatten().filter(|v| **v).count(); + + println!("Part 1: {result}"); + + result +} + +#[cfg(test)] +mod tests { + const SAMPLE_INPUT: &str = include_str!("sample_input.txt"); + + #[test] + fn test_with_solution() { + assert_eq!(super::part_1(crate::INPUT), 1676); + } + + #[test] + fn test_with_sample_solution() { + assert_eq!(super::part_1(SAMPLE_INPUT), 21); + } +} diff --git a/rust/src/day08/part_2.rs b/rust/src/day08/part_2.rs new file mode 100644 index 0000000..54b173a --- /dev/null +++ b/rust/src/day08/part_2.rs @@ -0,0 +1,54 @@ +pub(crate) fn part_2(input: &'static str) -> usize { + let trees = input + .lines() + .map(|line| line.bytes().map(|byte| byte - b'0').collect::>()) + .enumerate() + .collect::>(); + let max_y = trees.len(); + let max = trees + .iter() + .flat_map(|(y, line)| { + line.iter().enumerate().map(|(x, v)| { + let y = *y; + if y == 0 || x == 0 || x >= line.len() - 1 || y >= max_y - 1 { + return 0; + } + + let left = &line[..x]; + let right = &line[(x + 1)..]; + let up = (0..y).map(|i| trees[i].1[x]); + let mut down = ((y + 1)..max_y).map(|i| trees[i].1[x]); + + let score = |pos: Option, default: usize| pos.unwrap_or(default) + 1; + + score(up.rev().position(|n| n >= *v), y.saturating_sub(1)) + * score(down.position(|n| n >= *v), max_y.saturating_sub(y + 2)) + * score(left.iter().rev().position(|n| n >= v), x.saturating_sub(1)) + * score( + right.iter().position(|n| n >= v), + line.len().saturating_sub(x + 2), + ) + }) + }) + .max() + .unwrap(); + + println!("Part 2: {max}"); + + max +} + +#[cfg(test)] +mod tests { + const SAMPLE_INPUT: &str = include_str!("sample_input.txt"); + + #[test] + fn test_with_solution() { + assert_eq!(super::part_2(crate::INPUT), 0); + } + + #[test] + fn test_with_sample_solution() { + assert_eq!(super::part_2(SAMPLE_INPUT), 8); + } +} diff --git a/rust/src/day08/sample_input.txt b/rust/src/day08/sample_input.txt new file mode 100644 index 0000000..6557024 --- /dev/null +++ b/rust/src/day08/sample_input.txt @@ -0,0 +1,5 @@ +30373 +25512 +65332 +33549 +35390 \ No newline at end of file