advent-of-code

Perserverance, or the lack thereof

git clone git://git.shimmy1996.com/advent-of-code.git
commit 256912bd55a13d6b5b9496cf160ae8dd06eddef3
parent a1328eaadf0807f1186acaec5217514f0b33ccba
Author: Shimmy Xu <shimmy.xu@shimmy1996.com>
Date:   Thu,  8 Dec 2022 19:28:28 -0500

Add 2022 day 08

Diffstat:
A2022/day08/Cargo.toml | 8++++++++
A2022/day08/input.txt | 99+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2022/day08/src/main.rs | 93+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 200 insertions(+), 0 deletions(-)
diff --git a/2022/day08/Cargo.toml b/2022/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/2022/day08/input.txt b/2022/day08/input.txt
@@ -0,0 +1,99 @@
+220102001303332210111144403232401113333122342344231242454143210203320302212443030013122003001101100
+002012111323310331302003042221132010432132445255133124455255223004414440440003301012203200022100210
+200201202011003110423331304200331011531224544245554512113213335525210302033141324322321031312102110
+100020212221032131124244103404003222433224521422334123232422124515312313433224100204010121132311000
+200113321032110240111004030043155443522233113242534443312241445211112450323031312112213000023232100
+000123222130133422212242344055134444215311551422533215421415442551425225432130430124043210211111310
+220013001310041302023441034332322423241253523413533421512551432233114553552003013120121302130121320
+103122021001324130041021443134551553414353255343356623553131221454542451225451442032131420112320101
+012123121220242033212021343121244143344324534625253522262464534215422121222134240012234133102111220
+330310331223230000003152443554413335326665536266322522652335566553114314142351411020403401413302112
+033203120011110011132251145135331144533354253453463224233644324632453434135523253300131220303213123
+320111230330142103134512313345346434643654336344264466663434525446644113511452351451422214014013322
+001132233321313303345535112443242622242446334234425332545346556362543355243332135453212444110331303
+112033313244310423153325243233225655536462366623456645652435535435325433454445524512423111021132122
+313313043314211444453212142362452645555543222645663664243443432442223333565131234141351344321344102
+131230133213133211232332525334634543566623665537334334547734623265434432455254451425235432443212211
+033344041000113254352455455346236225633465644343664557364344535543336532242444234311533433344140212
+213210413044552341333323656654354424366446744766744473434577534775255332236334522521333331002022121
+223112203001555435245233523555243667443447767433564533557556755677732432236462355155425414244240300
+031112340101154335334634566542526467535374465476634474475365364377737233444423444335143215320411041
+010203132113252122424352345322745777347553756767766736766655557657446763326426264545545441510322304
+321034434351253424555342636457546533636345766763737363536656637564757633453436366225532223151301123
+140333103113245552456543556436376775537436555564765867677377566536736367466446544545111231522432042
+344202325543255413523362252766476773354345756567775846446585764655346457333656245566142114252141442
+300442033331415536626644654454735373634646576484854768867856765345656633644236246242335514321313432
+422444251112412665556253554536437336675554766648744646768865576456545754676632532244262315154431312
+202304543452455552253665657746647784558765457564785778845788767784833536336745445354462312521531440
+443202253112536253525354345435633886668445868754667447674764886554756567546737654246334325241222041
+444423415155442365223265743454556684558776766888858567565767784775755635475565754224462452335222241
+344303321125225553466564654375755674684544768955877976656855558645744675543644552655335222244234041
+300235531322254555657355344757787845684787759755989576658884775746464854347454745445252251123331200
+202355355135453623555734775437867546864765789685787875887555685885564677653753475626332543431524440
+123112125155234436477477667347767767746589595765887578895688659457644647637353775542533353231324344
+410231223354354564265577744565557576558859695696978987776856795557766747576576743622336235154122541
+144345221145432366464547578854445477995896778965569886979678656986544557875776343376245336222211154
+041355524153432432536456778856655655586558689789676558987567866689878584574356765462664355555422315
+043213424536224446535754387584686658555998969679778967988975666897764557557733673744622262412325233
+101514245332544663477377677875765977975768579996797978899567698975768865784644537776543454651544223
+055515332353232236436447686544477656596959996666689896787797795969665874555543664764442344265232243
+222545542226664544634434558757485778565956766669899769668998978795896868888674777634532432531545351
+434314333334362377634654477865886799965779778887799678789966958767788684856666356767346645644343444
+052335546342344533746576644847456965678786776877897777697767787998596844688878543443425354265524523
+245321436235332754756734464668569895557967788998689969678678888859577664555777765333674636253522234
+222511135542326767344777655475586999669889777967697967976779776769789666884457455657536456246131555
+254433214252433545454764655656756765598866789679798998989699886966798756574757563674433652342254411
+252443455634345774563668584548569877777679776897897977978768868758769758655687433345452443435424512
+312151563634655557774676587765775678576967989777779888989698876657887877756744456665655635556644123
+255315424526333665355778666668586665799969869877998777889777988668875966868565465457776522236513141
+141324353246555364776377784867669969887687879788978777778977696768597689744655653757436644265454325
+354554356562633675467576477485995977969979699799889979879876989688558895876557644555777244452335314
+235324556262356365436555887646778779897999678779989989789867689977685966845857674434347554655643252
+413432246554227346473675665849975567696667989798798778788967877675576875667875866434546262246521543
+552324452542255347547654567456656665796776679978889979999797899897878689747764654756336634422352455
+132142446224635664776385465487598668797987978789877989998967699965559668558447437465553652345225512
+535121326642222474774354544766768756799778998798789898877898889965958696446887843476762526426515244
+345151122435326774546477755588766796667978679897798799788798667756596585444845757774352236466242124
+324135422245435777577646446776855665599676997689997887788889876766665986567668467764463526524522524
+325343515546653536653454765454579566578777977788879878787979966579696677474785343347473444324354355
+411221244324436736446567757755797766577698989699898679769679676797557885877546755664674522365314215
+011225512566433343766434774658485575577887896689896789789788897667989867646846655537445534555245154
+353122224246523453336574684477667558859686788676888887889677967668868867745484457553323245424151214
+025211533334552466553436874465855779555988968677869768998666855765958765844553645533522566662215435
+011144555564545656657564766665457958988879979779789696768889888785895588848586367454333255641115431
+312135513646235366335766645784546897755855986679986669787888667577687576776674647476324553225412153
+401131435555652456777737485558685475567889776796686996966887777867565457675674635564364663332115334
+111542111546535443776533554556858445867565786988765999858788986988467667765674337645225253433114412
+324152522543244453747533764587454556659676798855686758699569787576577568846344576564242526335344431
+123513525116652325364735577676854466795879775965858579978758766786788576585574533555234644544424444
+201422231514666656435556464744556487779676697575668797755696587875664885475643663462362633532152224
+443133412441244332664735557756557478667877999697777576977768645754645558333564346654626425334525344
+431015242443666335263335435575686474856864565688697865686896558787678553436564534565256645251434512
+010022255222133364253766673665475577467776484765858776654564844676776444447654375652434614143215022
+112002141144134663334274465647734767886457477454887888474577867446657475453544742245245225443332224
+331232153352336525225567445365565486747774487468877475878758554778586376763333443326544225345123013
+320123541455416433363426343365775545857885758565575848877877686547673556675754423464442514232324111
+042432343324232554524246554574474345666464658776446546567878767443577744556362465553665411142432422
+142111123322431534522545253754364545474457775847846786887674646573467346333352524442333115335223141
+344133332441235422454233333556736343777476764657687856476465776533456554442553426542432524345343333
+200031013552112555654336435633347655674473538755855765665344475745556736546535462654344221551230302
+304022204315531423544354645467655764773637456746376536645753764476654735362436623665431353330033320
+001214000225135351445433524663673656735567443667544556374445736766637732424362445435124235202421144
+133240444115255522316642262344554645657446334644647444534543345455733253453543223511253231133210321
+324313341004114352115254632235242543363566666546735757746734646346355536356645334223223155213043334
+322012103401425423541123244634634342577565467364645454366476547536536356344363313411143511003322321
+030233343013121433455142565364335554237444577544465747334546363663525346435245514412331312412441220
+210201421410133422542335534522343332455655473755663556635632662655563553445642432325453033042340112
+012224401212432145413124536446542562653225343456763356434464356455233544345355532151231212104022112
+312232323430402414252325451525553345425256326265324326656464322435443535213233315511433100124021321
+010230232102113031224241215434543254266544664634225463436365465636465234441113521431122331212121101
+212212302203143433332245352152333246623456656325435343642442543354454522241344343333411433411331230
+011102200023414304323524211214225553365332454633645434256444354263245252314454334421304444012133303
+120113010124021413002342554241434235565233233626345455655236425423152444344321441224210011210001020
+101033322321412021400115241224135351431466535533654236552223322311141154321444212340220200110122301
+011220031300131001424314223221135334414132553352224226351421342145342332444324224232200001102210233
+121312303322224212003013214154522352135431322413434142221234334123523122531033230010321012211132201
+012020110022314203102101331443513341244141113213442111455332441213534235144341443443042312110221221
+100013222332201123334301101412554224524144445454322353452424512151232340130231214403422202221333120
+211200011012011223124042241233103554151124335225451435512351451314113131210120200021021301331230211
+222211000301233212310103403144201411351211113145331241322345244155131304344032001313100223022021012
diff --git a/2022/day08/src/main.rs b/2022/day08/src/main.rs
@@ -0,0 +1,93 @@
+fn main() {
+    let input = std::fs::read_to_string("input.txt")
+        .unwrap()
+        .trim()
+        .split("\n")
+        .map(|x| x.chars().map(|c| c.to_digit(10).unwrap()).collect())
+        .collect::<Vec<Vec<u32>>>();
+    // 1789
+    println!("Part 1: {}", part_1(&input));
+    // 314820
+    println!("Part 2: {}", part_2(&input));
+}
+
+fn is_visible(input: &Vec<Vec<u32>>, x: usize, y: usize) -> bool {
+    (0..x).all(|i| input[i][y] < input[x][y])
+        || ((x + 1)..input.len()).all(|i| input[i][y] < input[x][y])
+        || (0..y).all(|j| input[x][j] < input[x][y])
+        || ((y + 1)..input[0].len()).all(|j| input[x][j] < input[x][y])
+}
+
+fn part_1(input: &Vec<Vec<u32>>) -> usize {
+    let mut visible_count = 0;
+    for x in 0..input.len() {
+        for y in 0..input[0].len() {
+            if is_visible(input, x, y) {
+                visible_count += 1;
+            }
+        }
+    }
+    visible_count
+}
+
+fn calc_scenic_score(input: &Vec<Vec<u32>>, x: usize, y: usize) -> usize {
+    (0..x)
+        .rev()
+        .scan(0, |max, i| {
+            if *max < input[x][y] {
+                *max = input[i][y];
+                Some(())
+            } else {
+                None
+            }
+        })
+        .fuse()
+        .count()
+        * ((x + 1)..input.len())
+            .scan(0, |max, i| {
+                if *max < input[x][y] {
+                    *max = input[i][y];
+                    Some(())
+                } else {
+                    None
+                }
+            })
+            .fuse()
+            .count()
+        * (0..y)
+            .rev()
+            .scan(0, |max, j| {
+                if *max < input[x][y] {
+                    *max = input[x][j];
+                    Some(())
+                } else {
+                    None
+                }
+            })
+            .fuse()
+            .count()
+        * ((y + 1)..input[0].len())
+            .scan(0, |max, j| {
+                if *max < input[x][y] {
+                    *max = input[x][j];
+                    Some(())
+                } else {
+                    None
+                }
+            })
+            .fuse()
+            .count()
+}
+
+fn part_2(input: &Vec<Vec<u32>>) -> usize {
+    let mut max_score = 0;
+    for x in 0..input.len() {
+        for y in 0..input[0].len() {
+            let score = calc_scenic_score(input, x, y);
+            if score > max_score {
+                max_score = score;
+            }
+        }
+    }
+    max_score
+}