From 892430fc47cdef28de6e1c8d6b59617327f251d8 Mon Sep 17 00:00:00 2001 From: Bizcochito Date: Thu, 8 Dec 2022 11:38:24 +0100 Subject: [PATCH] Day 8 --- Cargo.lock | 4 ++ Cargo.toml | 2 +- day06/src/main.rs | 1 - day08/Cargo.toml | 8 ++++ day08/input | 99 +++++++++++++++++++++++++++++++++++++++++++++++ day08/src/main.rs | 71 +++++++++++++++++++++++++++++++++ 6 files changed, 183 insertions(+), 2 deletions(-) create mode 100644 day08/Cargo.toml create mode 100644 day08/input create mode 100644 day08/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index e5587ff..f1a0d60 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -30,6 +30,10 @@ version = "0.1.0" name = "day07" version = "0.1.0" +[[package]] +name = "day08" +version = "0.1.0" + [[package]] name = "tiesto" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index bfb26ae..32499e6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ members = [ "day05", "day06", "day07", -# "day08", + "day08", # "day09", # "day10", # "day11", diff --git a/day06/src/main.rs b/day06/src/main.rs index 2969f62..55eb486 100644 --- a/day06/src/main.rs +++ b/day06/src/main.rs @@ -25,7 +25,6 @@ fn find_marker(s: &str, lenght: usize) -> usize { #[cfg(test)] mod test { - const INPUT: &str = r#""#; use super::*; #[test] fn coms1_1() { diff --git a/day08/Cargo.toml b/day08/Cargo.toml new file mode 100644 index 0000000..7156aba --- /dev/null +++ b/day08/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day08" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day08/input b/day08/input new file mode 100644 index 0000000..ca6ffe2 --- /dev/null +++ b/day08/input @@ -0,0 +1,99 @@ +200210101302123001201232003214413304042424024222111455334421333412020113121332322101331130101222121 +120100002010203210130421301201404231101254535242322112224524254031333224202413331320010302202022220 +001200100200133300103241144403033124133341435524324143423341133252144040000423121430200100022211211 +000012321202320024030320421230202213131514345325422513145221212451233202023402411040301113132131121 +111103022312211340103042313022513413532253114352113252432213335135141552003023313142423321320321212 +012112331020102411442404102132241553154421513555415233312432515445154422402444314033003303323320102 +200110201230142322043222345241215442224221523534521123322555135454422121142320102300340211100120310 +120012121330311223132012342543213553334431544155643463443115441515143121132442433102420433301221030 +232202001121031432403445341332243414414443326545556526423425612114153351234111432130314202330220010 +133033302004430221300431213144135422424546344243432363665246244341243432352514332142044434023131003 +323030003440212122341123551231453352464334362343245665335563565343421512431425213111441304340011231 +003000034413321402514254435224115366533425244663634336625232232553226312522142535212414040004113221 +203203234020411021411532552144456644553434362455342664624546642245543563524451523133334201141411113 +103032442424101341214421543433253563535422262333555222632224462463423526635422354541113034433423000 +020133314440123532124351233325245665424345546323747756264656566346366665333444354252523314300303020 +303102341103425544443131253325266522334544545666744335557473366256323634266213415324324341134211112 +023044020203451414535535625633224445223364357435363343536455577666362444542245113553311434011140201 +130031020304324235255516652533544625454347643363434554557436654343442356543323225524232341230421320 +220403302205252431242554332553635575345744474647573376655365465333365246645632425211423335322131241 +214400202003451322143346242334324356456753434655775663756755654555657245563342333124515412311131042 +144333342021131124452334346355653767663565565443573444757577736363654665266666525633314514341134041 +111433243134224443344522523636635556736633645434676475377437457745566744646666544632522235344014021 +421102342525325521256266235434736567476654664744455647574564433767653357626264235361354533341223334 +030103204353142134336562363447366767454375766855756788856466566674566447433523254456444314525331002 +410404421315115456635435323737634573668554668666466474777667486334467637567356564452622223212300033 +012140231351312622653242266664756743747655464548677545754787855754646777456542663563451151125130001 +122202541435443225443523376367336378575474446665666578564485767586653557636553243543226514123433024 +322341213145516625463356665437453687675788656768568586885644456856643777435654242663525154214512210 +140223342343464363346556333753354858857777544686557677466886555488585473373334765233552623543312200 +422403124152143243445554363655577858878487546898958587796787475657654747656366532662626445424424410 +321231255223524234247766447646866656646454795565859795788557647554485844673466335434256341522445230 +434145334313623446664556646655886667557495589675986998765976684664875654474437775454342445551523513 +433432522546645452345446344376857768848989787655576756768988889768856644743374354326655353222112422 +420512322415655344574666457745755754768778785979569655887679799664764746786677655366663463434125353 +124155335466242262756735466446866676986698997576575557966855895697587767454454543472442666623435524 +415345153363443642766655644464854747967899588766957797576697788779586574578644737774624632644545113 +441333524323362443745375688445665755769658789797769878578585579797547585786676677364322333511241423 +434241414244556353633455448684455597576765697869997689879755769799778646786856633667554436244553435 +245314523664342366643466667485888767568897999988699997889967759988686546445685776346654553265354523 +321432211425653366363657657447568665877957979987786779697868577888595675855456734636524456425431142 +033422313632342655364345567644755898785796699996888776886796657757797857688876376354363532536232314 +052242355542323647576334458576689579586968867996689878797799969587659875547656644765646363224253444 +234434342653226546436658855775896968867876797997996768779677667699889796745768337366436634632245213 +445222213235346735753667755676669878598898697986687998696976996987769858656476757667332332625351514 +435355546442363575345376588657989988589877788898987788998987888969858697674677876744344654353312151 +133553556352633665357787686775799577779997878787787897797699998878965787455885663557545355624324115 +545233354544544435573376578458655587598789677877787899897869896658788766588656837747764363655341521 +211555453542466735366787685658866856979676887788987997989769978968996968546685834576542622243213155 +144113123264426777463777488587699869767977889987787997788887696979756858576885677473635653233543243 +311313235543366635754558457576795998769778679778889889889987668888968988774448543757733325643231422 +443355342254425446767468656476595865887986787897998879979969967775865587546587644777655235234324344 +442443355324564334365767887885668589686866989997898887797879967668978575855866836656334552623632351 +413433144665547465436464847768768777996977968777788977797977679895685768676556634643436422334442455 +453142566345254453333444885587565578677688779977889999789888796877778557847675867477652535546215422 +151332265265444777364585745778956687969888787989789897978868788787686655764465553577745632625231455 +445223254646322766445774546689887968566967668798798897976876999668956869458456654344465452425345523 +354122314463434747436654644484979696677686898978887878797976779669677694454485555635752664656513133 +012232355266625635373378685656767767887668769986988989987778777769685698467858437754343553255322444 +024555213224464774353435485656687879797989768686778867676969798596899575656786466333455553355345234 +132432444233664544676735848585667856997869777886887797667989767578765688576686457343436442546533142 +444143435624244477333448558448667577665667766788798979786987858959699677748784665774745235262414311 +231541231646652273467743578658756967656599897986666898787998598868878558884847373763743436333442222 +452115141256244235637554556584849575796768969699769887767797597866989785545764765437552564345131455 +425244513366263336575446588857846976888767658868676696667958899568857767458773744637333452423533532 +105131435443626324576435676555478786567757999689999668897789599599856875674774656477364623225123151 +032315232453426453465765574644575446896598678899586759777588775565758668474745575344566425615355433 +414341314426235264647543645746457657967695558965975769898785985654576867658657635475666624352134144 +100323311242236242255347744575476776858879688698797878579969975874748675444573747344653555211352221 +014311441544336355454575445686757688689666888758996965685596966444845578855576543546465554243154142 +342254542123656656546356366778675574845697675569556687688586558646447667434456665352633422255244414 +211453143353434566563465675745864885674687588998996685977676784567474566434544466522533343353114231 +313022332133466332336477634667465867888785648576775865668487487544484845366535575442326624351431012 +114211155121535434243656644454647447848458486648876856858767458446765544736737656643222525432544232 +010122525344156263354227756636434587458665676586887787858748554686666673755577552352343354534152040 +412321535231542523462334653374657358685586488785485678564864446878734675557735423665442154552342011 +104424333415333444626345536643374555557465464777678466847574454875666744474766234464241242553110441 +124112314331255362622446225633636335345644866876474848854678487634655646573245644334324331251444400 +312000445323443215433555564337345455776744675674478888474578745344363573744242633426233313351422233 +430022234512251423424326264637465355355746637447648757857473743746654776525262553642221524414041323 +233102134414232412332356226435465646465443546335436365365755446655434343246552323324433143130002111 +111404313153153442123666355224747746356376564475444457353545546774737575424345446353354342244313412 +234334440424325351212523232526466575445565545647676363335445774536647332224524363441415351434423203 +123130340344441323432464644565445455655737653455763575457374566467753266262333355253144434121341413 +032133414041244541233153446223224334665566654364436733465455436362242424353334354423444542342323120 +331120000421342243325115456255364522626457536537755343436545747263466663636524533235411311121411332 +332230404213303353133111256254363655255434744674474754434646343454354433433515513135334442222004010 +202121313223222511453512322255566444565544643556454452443454524223535655434354335552444042314134332 +213300030010040331541141441242645424354334635234565662362526354224246533454523242512414430303043302 +303300002321212004244535244451242654252646636554662636263664646235263231315331142352102120130122303 +331101014024440131342122251123415223633335635453354263636226636262256541131443215354304023243302102 +331202033431032221244232125455313233642635432336534464333635335635423524411525215033323313120333013 +033030202002100313111523432311242321352266332543436322536365535415434245433251242122313014031212313 +222211100333332310242343252221251543414443226355325632364262625135251521332232522230244440201332023 +000203310213011213412344315255131512335223453552465335425413252322422253334440310223340313231332020 +011332231323130141310431031222132533232453353124412341254122255424331153344341340113311310010033130 +010021033100310312123242131122524221555411455523421253253142545515323423322444300142424333201332021 +110101220202201114310003401420513324334525253112533435355244442344311112421320024234333132222021000 +020011123333222200003244032430305123255531342321413242534522552125444313014002424233330331130222200 +100220000312023302114114440201033304323431155212155522541415515211123243000444221343002001121202102 diff --git a/day08/src/main.rs b/day08/src/main.rs new file mode 100644 index 0000000..43ea7a5 --- /dev/null +++ b/day08/src/main.rs @@ -0,0 +1,71 @@ +use std::fs; + +fn main() { + const FILE_PATH: &str = "input"; + println!("Hi this is the seventh day of AOC2022, first we will read the file {}", FILE_PATH); + let contents = fs::read_to_string(FILE_PATH) + .expect("Should have been able to read the file"); + + println!("The ammount of visible trees are {}", visible(parse(&contents))); + println!("The best score is {}", get_best_score(parse(&contents))); +} + +fn visible(forest: Vec>) -> u32{ + let mut visible = 0; + for i in 0..forest.len(){ + for j in 0..forest[i].len(){ + if i == 0 || j == 0 || i == forest.len()-1 || j == forest[i].len()-1 { + visible +=1; + } else { + let tree = forest[i][j]; + if (0..i).map(|n| forest[n][j]).filter(|t| *t>= tree).count() == 0{visible+=1;continue;}; + if (i+1..forest.len()).map(|n| forest[n][j]).filter(|t| *t>= tree).count() == 0{visible+=1;continue;}; + if (0..j).map(|n| forest[i][n]).filter(|t| *t>= tree).count() == 0{visible+=1;continue;}; + if (j+1..forest[i].len()).map(|n| forest[i][n]).filter(|t| *t>= tree).count() == 0{visible+=1;continue;}; + } + } + } + visible +} + +fn parse(s: &str) -> Vec> { + s.lines().map(|l| l.chars().map(|c| c.to_digit(10).expect("NaN")).collect()).collect() +} + +fn get_best_score(forest: Vec>) -> usize{ + let mut score: Vec = Vec::new(); + for i in 0..forest.len(){ + for j in 0..forest[i].len(){ + if i == 0 || j == 0 || i == forest.len()-1 || j == forest[i].len()-1 {score.push(0);continue;} + let tree = forest[i][j]; + let mut a = (0..i).rev().map(|n| forest[n][j]).take_while(|t| *t < tree).count(); + let mut b = (i+1..forest.len()).map(|n| forest[n][j]).take_while(|t| *t < tree).count(); + let mut c = (0..j).rev().map(|n| forest[i][n]).take_while(|t| *t < tree).count(); + let mut d = (j+1..forest[i].len()).map(|n| forest[i][n]).take_while(|t| *t < tree).count(); + if a != (0..i).rev().count(){a+=1;} + if b != (i+1..forest.len()).count(){b+=1;} + if c != (0..j).rev().count(){c+=1;} + if d != (j+1..forest[i].len()).count(){d+=1;} + score.push(a*b*c*d); + } + } + *score.iter().max().expect("there must be a score") +} + +#[cfg(test)] +mod test { + const INPUT: &str = r#"30373 +25512 +65332 +33549 +35390"#; + use super::*; + #[test] + fn treehouse1() { + assert_eq!(visible(parse(INPUT)), 21); + } + #[test] + fn treehouse2() { + assert_eq!(get_best_score(parse(INPUT)), 8); + } +}