diff --git a/day08/common.rs b/day08/common.rs new file mode 100644 index 0000000..6fe6037 --- /dev/null +++ b/day08/common.rs @@ -0,0 +1,55 @@ +// Copyright 2022 Christian Ulrich +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +use std::io; + +pub struct Forest { + trees: Vec>, +} + +impl Forest { + pub fn from_stdin() -> Result { + let mut trees = vec![]; + for line in io::stdin().lines() { + let line = line.unwrap(); + if line.len() > 0 { + trees.push(line.bytes().map(|b| b - '0' as u8).collect()); + } + } + Ok(Self { trees: trees }) + } + + pub fn dimensions(&self) -> (usize, usize) { + (self.trees[0].len(), self.trees.len()) + } + + pub fn tree(&self, x: usize, y: usize) -> Option { + match y < self.trees.len() && x < self.trees[0].len() { + false => None, + true => Some(self.trees[y][x]), + } + } + + pub fn line(&self, index: usize) -> Option> { + Some(self.trees.get(index)?.iter()) + } + + pub fn column(&self, index: usize) -> Option> { + match self.trees[0].len() > index { + false => None, + true => Some(self.trees.iter().map(move |line| &line[index])), + } + } +} diff --git a/day08/input b/day08/input new file mode 100644 index 0000000..d4d4fd0 --- /dev/null +++ b/day08/input @@ -0,0 +1,100 @@ +233314113252402225351463633453424226362041164065666135612411011040522031304523540110205035014411044 +332122310405013102405106464353665644607664150663467462700534002161131361415444052114131045140243410 +440000154211143151036643465424430437150326344660672414322263612306613505030214413503140154240143331 +323343255421523302132452525040120535030306056101323626031642774451731442413053643256135313042003043 +442005315501104446243554125512523705520741532675611167103501123704102401231225310004344454441311331 +332130414130356034341366056652625210567702243041400445367704317060210626605415512605335350520122311 +324030243443122504054434240071266742352112321376000244606511532513331523563532353420154204241013554 +405514351330112521566052015217062045756373753646835340126272647475527567717322610632203041311303430 +335344112324634006053114070623702457531233080182014252513623435405053472452142020411651513420241150 +242041131336116162436425631055271632161034811060125876160151764545471725530572712055225156042443422 +510115143602101614215644471437202165862138121575207306363288027622104656656232657106303540123445433 +323023452213641660103641556437400327347513153233743771336775454828804123702623477265225140631521412 +202414114362551307121033522362685234048874113835556780630765201243481547353207660367310163062450444 +251210540002101157605104272102100805434031582776285412180762321556517203802774651440303062465423305 +415154424136606622417173367536300675207863456056626612502574128212038664377073051707565151352430241 +422343416235314110055766487664127721622002932278537772434781110282275624223646255613020563013414324 +402501442363526063250415576530161587636599439927185751837499351077528874028027725403237453225064102 +504234134044257264655215060321538257751387982248126653446534117721070521632241341327330746014516212 +046421106227215776577005025532583722724216237543578962475762466531136887322178170357170305360651141 +055031536466374206237511402485882788415741555457619525942245935594343888851226034246745555525105303 +313316632007533014153074787161584766685843475942726193487858652825242238106152063072370317116502432 +345426660702445442335226831889943991158617188464619968385935798291964336101667488024624136722351004 +553433023431367776013442510449498261312593877587925648886725526355324173132272087282444404673023212 +156321141760325157812461514459436947142926654837377323496455524659313115197526460314171035362510231 +255120642613302053266486441787818289142646555466227786469549465753577142215522110616726724621755043 +334560516360223658024601577515767963496326366868843234647892475283342192849565184146074327226212463 +351153721766750360870230632513718533288747664849867792728752858296473818827894546842165222662660316 +230546472451664022018817486253724286896847769324654739253839985376855756726394310051213741574334634 +240604111313412778575138553583993533754688392955365776855668274427545426777852711683051850766130035 +612053444440242110747524697575138856763564432993786937663252964632522297343191877616788702776256531 +213210746050611853364362496895533938487867659678984548544999789999252968144975244772045683045163143 +234313560047266766104657929337338747939999549989343339775674649843878538256756297465206356607215721 +541330056702266326645419617828852697233569954595594373743435783473477458272526351331155042164020032 +140412541255011411271525629485342852659763893333568788878686877387434739657433158924624635520312150 +556450111785818650834747363432795446558935634334688647986889543767825728745991426532778554774250041 +046374536364502613174368347367376484886386459583593967639436864647827484753857757357047128547756242 +655764564731052057576529129964629339964949833467658766644456934553679432454423318918284774722205121 +301454635173026337889714886368752544354576898644765747749593399993443244885693658957270520386432731 +613651341801476361933539945247365669358764655966668996656667973574449468955773393241131553833236721 +042712127874888396848539559356923856975699558476695477574879649457344544586753161376200083775010372 +633571133050088089565169284347688865568347469855686456948898869676734992839954519559214335640327013 +172043723757200297443165429577876637868564587665756749555668684974876324328894776283612126381240111 +635125136468188597197264372853466657935845654969889595996546968735445775435645812218328362205464515 +656713007810654782857485344549587333657698446778779649598947986469666454469269555718555500650676011 +630073065257712132592797837839439883385447879768997677977956469988597434494722784121685628415221204 +444604034821608779684622777623558743884886456886957579576977858998653784523662941495476424336113773 +514217671037186833491983278925346394784797648797698665967689974439553895999834522263288078771344552 +021015255420302927289942773454777353967589988997567586785896579989679678247998851883197521625052130 +421020702178063299133439633496746947998454998766655968999899446783547887482855713278711117821233107 +114364606446783776565749269624677945778789678679777777886755497459955637294537453616518784767252223 +142046260522656474665234998643755538575585667687579557758887565777974637389892415231713727714020545 +413262342277803134611736838553467587864496795555768658557598695666533579454964336395296454485042443 +204517185630034591417397997875956734665779995669788556788867949657786895495638811945649547400371307 +174033605718865969171853766365888998784497996577888589689477575486489599887526816741551681644861060 +063224685827512324137576693964389734944878776689697878975777568576888374227623832471753857414056670 +745313234117087656527378237327965487387495769997565976887546785886477554653439376446681216706565573 +714221627364867314579127862985967974769475946858556695875745779584938862235922341138325687134671324 +235137535058112125486715389748736438398876596577887698784464594585565475969977691455623226423203660 +331457736838274583155152898236648886649484885689575478878859449534476546585873369917917603680222744 +370056553104404574421729223659546734366977645685779476846478896477486329567528693841705350032460301 +012046147400171288423168662683944356977758997457588769489878697875636833272395415686405718542670511 +262552341358752126152124869842937539864957956664896875844667973579966523479295826721313375133257021 +341335311104544323385654534727568873565755546974875679488749436576963238488492321632936002023135437 +615306301262148803186235472539894694565945596584665544954959389566469239372486931536222011310441436 +324234323647385703932647765832954885887394937486885966935453639473679586767584874836377576245127406 +607164142714103623815198749329787575443985479863553945758558959498828382965456286964707151450421625 +314465066533633057228986634877635684496949577394987736868947454473842782594226393615334352826715141 +536475143305424208759752363667664867845838863986454395453978597925332488579511898801816403500125341 +600436437024871886647399158842535329649445587579746788759559564329954295266229924951756877470326446 +635061431072367818521728582256787599349759447773957754769437873366692627645595542612842717645730302 +251607161407032570875153116448595364788888378855675796658956867972396653145239427853001770112657603 +520241640710420727048842416277438383273627564337797943589249474727857339824479251678702445344250203 +506023425206020443136017159716969548233444659552973699328436724666925167146818911207262175137364612 +442525337116108025845215346249313587956988464638548455829596548637338122164362510653537202133162600 +623261232770026480642481695826472689876524548894353286453262528934387623416282466608203617473042451 +626053201167043156740764734514534985388485442264596935478566658482829581156117030012081322032200121 +515113523713157076317332585859135277147225933537378925962628268962323487376155347083215167110255554 +643536511610472445252441712381625244353195223785935227686229495474181892477324203217374633002550026 +153316344605375470614626642719443813563119365997536454752526938116346697801468177321355021471120515 +010334311035603565003056106843521955633914195866275698547136967639217264457708135633577350770316135 +351656015235033300075252446207592216329119342698142528782475484976514664823836254046377242355203231 +522341020414273625444887743684716565489521553184979199653378924268741480405417451447044277355321421 +320440214437322574221247068377611333151334766751972378176217877564457826632646430162364212550024162 +140006436600523345517508215782380433418514688293966641916347884224234845031257747444644402460030420 +104146605633362747562251487583451148253612867252927843577181415070521748116343064574155031564324255 +103130334346503557465533100482078578137542764112232739942722778250542504456660050343333220202316205 +224104423522010530242024415522604324510310083645671747433145834566115676313351317733033620325342051 +434330306111002653115073507708580057557878202203186883544117540038650412834072007217050442340043025 +230451225551565213004055211423448362510382160274334687381264153611307161161541343174055136520415131 +345102326410332306132112553766214623524786867854606163254728785833674760574565046554641232320425233 +324453112442152321362461117074604608838002887441621782406511511711643754374647452022531051110420525 +543002053003201421256545767762532417627081703274583184256503570111365335442301734221343521443001212 +235415514510600046234062765244344640577612404816608483334516626661775011137245042203526504552113144 +232104354241603004312140061650307762345454636588020108162255100572653572030455333104521444441001334 +032504101332516043103535067326435732054073207541115761605146733506251652016161600036450050551314041 +101112052131531010033161261171241410642510371771443174324170123145060261426566620551630411143502140 +022233151424524336433611141606565135707143053177553172244617521250201055320411606530411451334123003 +234144345551514036433010066345436637404726175521270115026425067373731004102313350660213212022501014 +103403251030225452432315040644602502722124614674434524653306555370426625422245263065135410325331421 + diff --git a/day08/part1.rs b/day08/part1.rs new file mode 100644 index 0000000..d1eedcd --- /dev/null +++ b/day08/part1.rs @@ -0,0 +1,40 @@ +// Copyright 2022 Christian Ulrich +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +// usage: ./part1 < input + +pub mod common; + +use common::Forest; + +fn main() -> Result<(), &'static str> { + let forest = Forest::from_stdin()?; + let (x_len, y_len) = forest.dimensions(); + let mut result = 0; + for y in 0..y_len { + for x in 0..x_len { + let tree = forest.tree(x, y).unwrap(); + let is_visible = forest.line(y).unwrap().take(x).all(|t| t < &tree) + || forest.line(y).unwrap().skip(x + 1).all(|t| t < &tree) + || forest.column(x).unwrap().take(y).all(|t| t < &tree) + || forest.column(x).unwrap().skip(y + 1).all(|t| t < &tree); + if is_visible { + result += 1; + } + } + } + println!("{result}"); + Ok(()) +} diff --git a/day08/testinput b/day08/testinput new file mode 100644 index 0000000..16d6fbd --- /dev/null +++ b/day08/testinput @@ -0,0 +1,5 @@ +30373 +25512 +65332 +33549 +35390