diff --git a/src/bin/d8.rs b/src/bin/d8.rs new file mode 100644 index 0000000..8c36b56 --- /dev/null +++ b/src/bin/d8.rs @@ -0,0 +1,129 @@ +const INPUT: &str = include_str!("../input/d8.txt"); +const N: usize = 99; + +const fn gen_input(input: &str) -> [[u8;N];N] { + let mut out = [[0u8;N];N]; + let mut i = 0; + let mut j = 0; + while i < N { + while j < N { + out[i][j] = input.as_bytes()[i * (N + 1) + j] - 48; + j += 1; + } + j = 0; + i += 1; + } + out +} + +fn main() -> anyhow::Result<()> { + let trees: [[u8; N]; N] = gen_input::(INPUT); + let p1 = count_visible(&trees); + println!("{p1}"); + let p2 = max_scenic_score(&trees); + println!("{p2}"); + anyhow::Ok(()) +} + +fn count_visible(trees: &[[u8;N];N]) -> usize { + let mut visible = [[false; N];N]; + // left -> right + for i in 0..N { + let mut bar = 0u8; + for j in 0..N { + if trees[i][j] >= bar { + bar = trees[i][j] + 1; + visible[i][j] = true; + } + } + } + // right -> left + for i in 0..N { + let mut bar = 0u8; + for j in (0..N).rev() { + if trees[i][j] >= bar { + bar = trees[i][j] + 1; + visible[i][j] = true; + } + } + } + // top -> bottom + for j in 0..N { + let mut bar = 0u8; + for i in 0..N { + if trees[i][j] >= bar { + bar = trees[i][j] + 1; + visible[i][j] = true; + } + } + } + // bottom -> top + for j in 0..N { + let mut bar = 0u8; + for i in (0..N).rev() { + if trees[i][j] >= bar { + bar = trees[i][j] + 1; + visible[i][j] = true; + } + } + } + visible.iter().flat_map(|r| r.iter()).filter(|v| **v).count() +} + +fn max_scenic_score(trees: &[[u8;N];N]) -> usize { + let mut max_score = 0usize; + for i in 0..N { + for j in 0..N { + let mut scores = [0usize;4]; + let mut max = 0u8; + // left + for jt in (0..j).rev() { + scores[0] += 1; + if trees[i][jt] >= max { + max = trees[i][jt]; + } + if max >= trees[i][j] { + break; + } + } + max = 0; + // right + for jt in (j+1)..N { + scores[1] += 1; + if trees[i][jt] >= max { + max = trees[i][jt]; + } + if max >= trees[i][j] { + break; + } + } + max = 0; + // up + for it in (0..i).rev() { + scores[2] += 1; + if trees[it][j] >= max { + max = trees[it][j]; + } + if max >= trees[i][j] { + break; + } + } + max = 0; + // down + for it in (i+1)..N { + scores[3] += 1; + if trees[it][j] >= max { + max = trees[it][j]; + } + if max >= trees[i][j] { + break; + } + } + let score = scores.iter().product(); + if score > max_score { + max_score = score; + } + } + } + max_score +} diff --git a/src/input/d8.txt b/src/input/d8.txt new file mode 100644 index 0000000..01b5ec8 --- /dev/null +++ b/src/input/d8.txt @@ -0,0 +1,99 @@ +100021112110202312022010330204312040000111012143445142221414240220240442332040010320133120230011020 +111110201332323210211143123214321343332124211413115514155115511033421001222101204330001300333011010 +222100123300231230122203432310224441551434231352112532354252244334410042212441233243220102033110020 +212210311310102334321243104133012535144151532555155341325352512544453402400411340202133231000102020 +112002102322001003304233302040124544411225523533534235113353522233535550432232202401221022110311110 +112102131331201432320312233124434232544144233241123334112232531521542551332434224211234133132330300 +201212231001314001232020011521543445152251413225334151155332443144315433444321403022301110231133320 +222200212012234021440323522251222114255435321245463652634454445441313331334221403314121340322201323 +011101313030124034011053132411352313135432265223564644563435541314314453511355132441242220212301210 +330120320004414224001225342325133541125622665563263444434232635442123423514224513321324324321221201 +232201002203142013213541415521331136252365342544552365442625322555515234315251334143110114433011330 +231032032133203330135134524113312353466322543643543364665636365344645123214255433423420443411102113 +323210134414131324325554245224653435365453364456243243426526432462344551442433224554110342343103002 +232211304402432454533435252145662665532355636326423563423433555236565455345421453313301243030201033 +301334404444113432444154526622565534622453452465656373363346236352264432242255511441531301201430310 +021031402014322514331554342423522244643245347463537353546733344353665264632651255422445201241324231 +311341433423143525332424243226653324554455444337664375746566644363342254524365544454512533412221003 +100141044421132331524146432626626222746377344775337675474374667736346526345525531112515524220332342 +103100424013355243423532363653563355763646667345657754366374646535626642326366655331552511413232033 +010411402325242212332662662566365363733444434344454736635435565544436445432562223352255125432214413 +321313023231141232536442263534773775537454346454663656656365364557334363362446463531553445101214414 +240014233445325423455462553425364653653645333436556455474746633767756644256625645261111551134401102 +141243103121441452453322334274356634453674654475774776665766755335473637655355324552442441555404422 +322032122454423446326624652657555775743558467874777864887767556333635645342466232542452344223021030 +341012035442545266666542455563533577735885866784868684486868864657367574557543634634445432352101333 +020332133545225646653645453347737756754567886778577468644476777475537454454734354466644222142343014 +443312425144123235564325363434377486685588484488457888748854756668646745465366635445226143535350403 +401011343544455465645234773757567857786858744667847887678865684557657673336765353252254124535144301 +231023111541232646654364676655554478448887447788656586858686874555544756355355425663634445444231041 +440414511232163335524763454743366676656778445597669969755446578885785477433565366464462411314121213 +423133115311242232226334356676577546867445799868866958556677644564858573537455653355226445155233301 +203312245131462222554746646746567788757698759759588997656957785788856475653644633464554355125443503 +232515114525335654457543443555565868786576595878575876598966858547666846455373765745424444124453521 +222335423146245553644554757748846867467979895568976578965955877985885478853344575356225432342255433 +101542414345642523353567565774687668996586857857667878786879985798875768547745643456564266313332224 +301144344462663436337536345845856858757875777768966787979577767876448646654465333646326566332352143 +421524351232465434654576565767446575666755968687986969675676875569846488676754753357442332442433411 +413325242333454424656334665558754657887868888788696877986559786689587455864465343775662452632451433 +154244543442464674633346657856868997877985698877867876966997859957879646664844343364323322443522435 +413112322322235675547345547474859589658598796866669766699978589785866454757775477664523433333434442 +424333122434626456777635768578686868759696687688967977688996678795685586557475554454652235566333123 +455211225265424477347444685544699887876977897876978778998988685797998647458856773634522525443114455 +215254146366363747473755887445877868656789966766676898799688786567569855574684673733533243626145254 +232214336435344464337365855745665767968888669969788898668797767777899554757865644635655552352235241 +431445513242642634373387444466656585686776979787998778778876688797776987575785547434636436252135425 +341254135453625764555454784558697758996768789898879887979776988667895866467544537467352465554231341 +353511356522232754333665846875756885597979987979797998788867898755966966747777863335465653622312521 +342131425455252547374557454676889585769899979877988878787678998666857676478654857544553344324545533 +423514554622265364535758656757779586677868668897789789888887987885766775484647466733766544444641311 +443223335562467674644684785459675797877798787899879888997799677698799557567657744564575455564221324 +434334525556625335565445744449679766879887898799888779777969797666677897876666577353534665444633525 +254452226436653735365676845666667659869878998777989789787868689999995856864675465356773254455643433 +541343235664626453454454858567778559888976667877797788989989978879887596787865646354356245342421323 +522412226622325753744545556855766985687768979797897799987698989897779696765756544634672555453353213 +412522254444564635737677845545956698889698677889799979989779898956976777758878537634642425526241551 +454222136263222765655775647779568866577886976888987989877869678975585697848476455557545354635351442 +135121552535465363736474668878979599679996967978778988767779668655599557568578867666446332363541241 +443121425555335774663648754687768668779998779886677866797868896598977975744577757466744252426313232 +413544235544623474445636785886865856978977898886979867667687979665559785856884354355373456533433444 +131513455323433453365575445468776665975667767679666666676699795889878855677784375677726235226322425 +434442215625364456464667564866697986985698768698867797888969976976557974477456645553454563433515241 +453531215644623547465376468645845588955556796689667796966679798897589845547855353757662556342441532 +332355435635654445755366546668857777995897877866676989779679698556675648665863767454324443235151225 +213534313254232564576535444544764898568875856676999867867657557989768547684445736355423244324413543 +124522522235244346567747584544557769579796878589766996768788685995875444645835373437362355652144245 +442414332462645464457376647585848848965577665855857885698768798696575546674475573646566222314114445 +112315233256652343737667637858458576968777559868597579967759677588458846648655454334245245345554224 +130224422112546654465464363764457446579885795857586579789779888566478866866743355335226264212535322 +243155553555342624676436455375874765489687699588798796759766675675567774646753634355346454424431253 +313442313423322535325375377344687658457576699776856776787599774474677575337743334422233423431421211 +234051131411522623337474757677857685464857596667557579666787555744487574544673364663643435211255240 +434314134422524322443747337633658574444468845599797888867456487676744754336473656352235344411242110 +004101145215335222325475546343745855484848877575546845867885845455746457433457436466565332352124123 +233441434144112564624645544646557557655667845446677675588784866645475343643574552243656523444534030 +121224143555135555564544776734555455674556575754675486888465845458835743367333425354526331434444031 +421244422344321446364634257366433445747885577774664546565647578767333446746356442243433411415210430 +134214243522231144462465353566577763536746478855876848468846687644663756364224224464635451234214111 +141132203421215213235464224663666433534678865566554887458784575743373335336464662256425123321434014 +213231333411351412344352524277763665543774674775545875744754576367464737663564262663112544415131113 +310320312444245332353443555344534456457456364753657473447435543364646364264425336635335354223233231 +314242332123434345266456226225367677737356563336765735555554546764556543652364555632435415411342334 +220144410231433531546232424625336566373376474676454647665666437455766323355345563421345422444413112 +331442434421142252212466563352266545334633374637735376643435675553544366564244525132151223011342202 +203334211344322333542433654434655335633734475664756747766375655456225622225333343324215521200001401 +133001044130025211433335626552525266367334377736466473364444543236446653253662222322415412004404203 +102331031201225135443424246553365346664536353367434546473743334356454654552233145534345231113214102 +212230010342131553532451526322255666663223366332674653633446456525262543246511312215123143100414322 +232101044243441133154151523542336655464563325444436323646534526542245542613552531321441404211220231 +230210113134231442312133523231426423645562233522662322645556325555424565225553133115020231142422303 +112122132434123424454552435541116536436354343364366253452343245526436543253435122423041022130131032 +303333011024122011435455241134321332555664352652523256255643224225644425233221344014220444400330023 +103302231310301313234354141312323434532246526636322542364336342533331445554253314030214223423211003 +110123122321424301403345325425115544551445462636362336444425435312233541552212143231043320211100213 +202211312210402020033410432324431253144325523235463625332451131521414414333322211144344230221323102 +210100112030114430323324002554125435321123334342551235451453442512153411112423411424123322300320102 +202220203220032000004314434423331253245534454123425114423553133414151232240304414101402311330002020 +012211131001201000134433142021511253314413353152553443443342425435421132024242024214322330313300110 +200011111022120322143342040443334552235435211511231451452222252222113214233002413424112001211130112 +202000213331311222142203242110321123234552545321425122145424451321304200332123042013120323022211012 \ No newline at end of file