From 20b3e5e07b336e03798f3da04e132206a042e82f Mon Sep 17 00:00:00 2001 From: Gabe Venberg Date: Sat, 18 Nov 2023 20:16:06 -0600 Subject: [PATCH] day 08 part 1. --- Cargo.toml | 4 ++ src/day08/input.txt | 99 ++++++++++++++++++++++++++++++++++++++++++ src/day08/part1.rs | 88 +++++++++++++++++++++++++++++++++++++ src/day08/solve.rs | 15 +++++++ src/day08/utilities.rs | 27 ++++++++++++ 5 files changed, 233 insertions(+) create mode 100644 src/day08/input.txt create mode 100644 src/day08/part1.rs create mode 100644 src/day08/solve.rs create mode 100644 src/day08/utilities.rs diff --git a/Cargo.toml b/Cargo.toml index 92627cf..4311748 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,6 +38,10 @@ path="src/day06/solve.rs" name="day07" path="src/day07/solve.rs" +[[bin]] +name="day08" +path="src/day08/solve.rs" + [dependencies] regex="1" once_cell = "1.16" diff --git a/src/day08/input.txt b/src/day08/input.txt new file mode 100644 index 0000000..a531013 --- /dev/null +++ b/src/day08/input.txt @@ -0,0 +1,99 @@ +002100030222341303232115221524311502200403234452204125251345024423544443305142410110142101223120110 +102030322341143330244233013052025453422005415304655426206452251310214055314024034341233110302320003 +003012203404011430120134554314552441635001641050211406204621114102021543144514102510421243314102033 +330313423002034140035001210441156554545625042262651356556544202500446121455112434243313013430141203 +331203121131230002055113430244610062654034520106363462155201210355601153542301323442223310134324300 +232201231021003254355411402310326206625011564114125202263504451451212600155351424154310240411213431 +033033333301011523544023206136145505304224325606020360006206455564513553512054354124523232201230441 +132201032234334044251334220345360402456541662253556375754662323265201641514121105025300453041111234 +314322341322104402235020206565044225363527617767574524475421763504341061551154250344125514014234344 +101323043023430543342440532324326413655657527215661116623455613664321005134300223542510253102211310 +320404242514122111525043444530625417656773117247512712444315767422412130351505513231104322032421303 +221204123205133142044056245654205231734371546616753217321556472223654044043612554152141554333102034 +340310155225452152216635634352712541366312326272262143522347111166646736001360102010220214000133002 +012404003515002416422500163244271366252711531615333437352461454777311142132000140205630042305222013 +332434552302203445503543424523327464362526657251687634745536521746523154717531643250132300541053000 +020130312425316362611130462217125425435545265467725256332777233137141524314631100051153314314343032 +112115124512002346011342433146124542356845483862756322785545283652723747276776403131622540014001524 +332354013101003031053022245431274627654677343638672643442563448782737464337546541546630431125021330 +220315320142064161406534365262377268373743527784575246325564676238816733767331345114363612344051535 +102041153526000601402325625766534543527882476653366727573448727775677146365732431425225562043130034 +350054114310533002426654125166575285757827422257872378766383267543484865317252532765440216543555512 +222440404353016144747347457368562668586285847553578732435887584463446862116134751240360645225435344 +554234546163262630313431762248876668663352436888374587856263343842526635237314231451062036605442520 +525531324662660356256614344438373575732699988366775989976447384338336827686445577222030363631240512 +055541261042533424177375756738443482846986979788978997559377679255463858237757327464422026230510130 +514123221240250442457744728347386637684953574546455344754548333793233865436751231314653541223102202 +355454250661114645144551785724753237378345375493453967559796675839837444334776625552412552302431302 +202445234333206767744653874724276586973668878949653473947686567565487325864733735677672241416513452 +445415223211061633767433865252483574744685974593334963668983745643354885658423263536531213102544202 +201405464251164654177385783224666473975674643767946648464678958969336335638578275154116223152222102 +401423140356441654745572266848965593553995667644648964747884544439493545472454437715336645303346422 +154440323240741427732867467564957336394985859766644495658985963583496399736552632162756445212536404 +441663663062661372122867364584655388977474857585778686889578799644949733463423826715534215154065204 +422036122016243651174663588693898583374968974778579585977478599643389537573534354622377452555411315 +430412403362272513348763673566578945645997454758694486758844958569786758359475575577532661126532300 +503653020441267422765353469867998339764449596776968996449777464644846938866332635583727771665263026 +006252631541331635768754753357384754988659585857956567666645975599653466655333342485345762600032403 +215205542775713732868842389885585646884858449858579777975997584997463698339932263363162431251615251 +342224450731115175762626546538955447796468885776778879697695699945446388444478355347827635732006646 +045011110226211633623658548365544498466468766878699597655897586488555435934935526883851537736604133 +440352212243431376382869634434568447654566895897796875679759667598898849334873844583742271767024403 +566632367121414762272448784755746799985759966559657989697977677955455754549393784586875275341562621 +004131167135757426882464486773699786789959957795985595559587775554479945637849747667752242212324244 +022411031432724262533554794664597446479869568767598899669969588984864457535986725263473577457413134 +036415005331162527346577487593798458668976888776888766856699598847776567686579476836656723623111342 +504001302331545267684439554776894957999788577876688969865558955547965687556849786463457244355044511 +464266074264126575276833864784546697885788878967966988678696686685576789843676844576623242451411411 +660130573642326366362654785549676574587876888769689696798799988678648554635745724364273772474666502 +413511521313437652422664948645649566979966676679778669968777776786958464356797545247763267365643645 +455033174473274564484285387995879595555686569997977698868769855686564847593489668858847744131410225 +560316675532344635443349663994499869866598568889798868967976679655597494679336978524332246567265361 +121053163342234352453837754569968465557968697766766888969966667796479457384444468264837376474515455 +604062313527642634768255846899648545559665886668966887999959659769978495794795825775373713613231451 +041031112622754332842439776736966994677888699898679988967779696758849589365579766676242256227420364 +630161363126476778455475799658545955598576568877768698668579988799596866465635488284555252726211044 +455563641427127283364758796475975894658677688789777769876667978747955677573575525588444241765111511 +256420063532645225542863884958844496596759999769769687678888788875449888945958948378756476127155054 +523256165217547362636325547686549767698957967985968987897777685866576744344388256886854362647342110 +205224606764556763544234874676659474468968576879668558856675855668779847849738247387744421321145610 +022651355615653552468486676459694744747575877656886776677858584988944678787576556747724125467333305 +526313424324717126486746644643397589664468865599858886996669788566489473798387676766873216657521426 +426530404522411762572567697375959747975669667958685679878659587764977658398697353283563215312244014 +023510245741131775436864579684699874979976567776557765866775469587757498544476554753722641656303654 +211341433442225414565332887899363678699495567888998675657659688744458493387343842346747477252626250 +406543152154335614758277748595836896786654948759999978569588555764838686653647362854723163226456101 +333640633617315535456525364849458558744656899855554897897568844446533866457545654847562377442214226 +040003053246534354438748723933365934878485997458955574785966996653353969666867276761661111124104550 +405602516206264365157378627495546935658594587869579958969686766986836687348285865231616336232000100 +304131030254233641578272475868953446545577766644956989679964489688554398956223433417637732661500540 +131164662225325441733366324345477954639475997658684766465487974699579665344326248136151472051130445 +452312154206174654138684277376889449945746954689646875777596486949365968888456226732332263263420000 +003010154551246223274282454345355673349967689986885795449989359633496728553885221437611730454554455 +131435126460017635544785754344874379695379366384573934357795398633967475353836864461424146264354142 +350154544045013352533173566257757787385448697484997454578735937499665628425542376452563635450224022 +302420224365163651765264446875287359533637498445639443969586466433384346337722662774436566624454214 +415242456321105173655733744886343826548944666338683876383973497998744488687446541256724244661531431 +155303206134314124372553618623857237346567857564464797593393675563536467777317174625640132203003240 +335521544652523624641575451785635352333587957647598535633948867838652636686573137515062356442045153 +235314500244636615342325346277256685653838226755493858683465683736283367654226655456541546644313125 +252520511531061305614364671212856473625642477732845278766573642425286572562373137446456005011330433 +241525455120116046543737642325543677623243884242534423327827446383376441727657314343125403632434002 +301045223551220356167117621755325772252845222546246588464784874685774676163655131632622146412415515 +011150133311336614251435416453632383454327534367288242328746537673543714263674263262001132354544412 +314104033000306256531666256433553535833538337463847277453476563227672715617272601302551120422305504 +104211442013141664402363645617163435245556228235337864656673626354151416721375243323635625100210424 +413422313502535362615054171511431775335574748526574728747453724472157632635263350024552502052340024 +131225301023304456424150404525765416565572272321835377167235317312551645334432406536160545434122120 +342131052232413325301451041516716541737346252413667115612642227312572433104525003114240240141232002 +244421101354005430232434420344323273421374225635551363463245561271743411452116466311034334101231020 +232404402304311415164151401553463731147111147752141732625152553615361046133341313265525232544143442 +201321033520133414312301326330253041267251614477322566645472656414545035321106361434351501123314104 +211232034442524242313453325046331353174352335422752364775746537513333534355120322320404054014303124 +224032233240430510135541652065052415154147565656231651653327511443424113100543020223335130010141400 +004110101133543440511523265424013341433043655022512172622140322434116104552043413352211021312421433 +000304433232402410151205333526613043463556021413402141634154404530112303306244402221312522221004042 +200204441020135232033032541516232550445221141520665016336536052063115335040344025054213324400312303 +300211022440310123055510053251505226452265556365100101014044645236505213550201425252404100302413231 +310312413210032422053242024243343060135340422313312403505631104430120233112324204414021331012401232 +132020341330304022053503522313342026211133610000020554213662244142551253124302245242110111033101203 diff --git a/src/day08/part1.rs b/src/day08/part1.rs new file mode 100644 index 0000000..77b2a34 --- /dev/null +++ b/src/day08/part1.rs @@ -0,0 +1,88 @@ +#![allow(unused)] +use crate::utilities::*; + +type StructuredInput = Vec>; + +pub fn part1(input: &StructuredInput) -> usize { + let mut acc: usize = 0; + for y in 0..input.len(){ + for x in 0..input[0].len(){ + if is_visible(x, y, input){ + acc+=1; + } + } + } + acc +} + +fn is_visible(x: usize, y: usize, input: &StructuredInput) -> bool { + is_visible_north(x, y, input) + || is_visible_east(x, y, input) + || is_visible_south(x, y, input) + || is_visible_west(x, y, input) +} +fn is_visible_north(x: usize, y: usize, input: &StructuredInput) -> bool { + if y == 0 { + return true; + }; + let hight = input[y][x]; + for y in 0..y { + if input[y][x] >= hight { + return false; + } + } + true +} +fn is_visible_east(x: usize, y: usize, input: &StructuredInput) -> bool { + if x == input[0].len() { + return true; + } + let hight = input[y][x]; + for x in (x + 1)..input[0].len() { + if input[y][x] >= hight { + return false; + } + } + true +} +fn is_visible_south(x: usize, y: usize, input: &StructuredInput) -> bool { + if y == input.len() { + return true; + }; + let hight = input[y][x]; + for y in (y + 1)..input.len() { + if input[y][x] >= hight { + return false; + } + } + true +} +fn is_visible_west(x: usize, y: usize, input: &StructuredInput) -> bool { + if x == input[0].len() { + return true; + } + let hight = input[y][x]; + for x in 0..x { + if input[y][x] >= hight { + return false; + } + } + true +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_part1() { + let input = vec![ + vec![3, 0, 3, 7, 3], + vec![2, 5, 5, 1, 2], + vec![6, 5, 3, 3, 2], + vec![3, 3, 5, 4, 9], + vec![3, 5, 3, 9, 0], + ]; + assert_eq!(part1(&input), 21); + } +} diff --git a/src/day08/solve.rs b/src/day08/solve.rs new file mode 100644 index 0000000..f1896c1 --- /dev/null +++ b/src/day08/solve.rs @@ -0,0 +1,15 @@ +#![allow(dead_code)] +mod part1; +mod part2; +mod utilities; + +fn main() { + let input = include_str!("./input.txt"); + let structured_input = utilities::parse(input); + + println!("Part One"); + println!("Result: {}", part1::part1(&structured_input)); + + println!("Part Two"); + println!("Result: {}", part2::part2()); +} diff --git a/src/day08/utilities.rs b/src/day08/utilities.rs new file mode 100644 index 0000000..cb2416b --- /dev/null +++ b/src/day08/utilities.rs @@ -0,0 +1,27 @@ +#![allow(unused)] +pub fn parse(input: &str) -> Vec> { + input + .lines() + .map(|l| l.chars().map(|c| c.to_digit(10).unwrap() as u8).collect()) + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_parse() { + let input = concat!("30373\n", "25512\n", "65332\n", "33549\n", "35390\n",); + assert_eq!( + parse(input), + vec![ + vec![3, 0, 3, 7, 3], + vec![2, 5, 5, 1, 2], + vec![6, 5, 3, 3, 2], + vec![3, 3, 5, 4, 9], + vec![3, 5, 3, 9, 0] + ] + ); + } +}