diff --git a/2022/day_8/input.txt b/2022/day_8/input.txt new file mode 100644 index 0000000..c21a1d7 --- /dev/null +++ b/2022/day_8/input.txt @@ -0,0 +1,99 @@ +122102002221220113213120331300324403204320144533424135412222404323003104143241322330203332311001201 +122101021201100333103013020111134433225413441345223355455242454420324041424144143320331213313011211 +002020102202133322040121034302303042143134335334145322255225234524340004204432223232112102301322020 +220012312123310100132111431130112432541411111551552451554212423331432121222413114341113030121331012 +201002212201312002313134042122454351541442441552442121522445315151245342234433410040022122332121102 +022032110101034413122002140132134344355151432255122144512123321252231413241444212332310003201030321 +202221121312021412211244435533212115121444135555133441115332134551132345451112300141040402100300202 +012312220202241130000011113153354512345554455246352624652351411552522235252320220001241023300202002 +012333201303122341124245121245445543543226522355634323552563215553344215343251121231221244211102101 +321301231011002302232325451215424531566442635526336536256234645441454222315444511024423430030333330 +133003322422223002304115312425243455525232535565226423436454233333321535512211142403210421302020033 +101133010023010323543533152214433423352362654562322325233322536523633121145214413250132044411100133 +320221132334300334142255424225345445666343362342664556562225525336544242242512134411121411023431302 +220303334444424011351432244365435464622533322535533646426655434625632334622324221515223224200440022 +221304441214044232254431226254626563623435252665353462634363535462254534545515141433333003012101220 +232021444421003153155555563266552625654464556746467434364574426222364436522543214355422403000441033 +012201023103313335534141242342266633235333737773456366635754757353643562353464411522131503110214013 +001312341320422254534224254526463545477565754347565653364577576546344354353323444331444231021332302 +122314301442525352324344444352624253544367643445553345537646357736556342425322323521153132114042013 +131322304413521551452533624455557357566464547433467466564456657667357636364246536151545131243120104 +101132340252151355245364224544646644673755576656444335747744436346774662556234254213342313124344420 +130103214431542422644566665465754735364345635355473343456557534564534635262346623535423523550344041 +330034234123153543264362365445456574446667446477765454656433537477573634363523253455433535354111002 +204234335312443233246566554734755677764544558775884567857655576575577433632344636352213523414341223 +044030434221432326234443327755374553678585776744775578465877876735477453377634335434323551354532034 +114130014233253356656333357773543354874657857588588876787764664453673464577465555655421532452423003 +041034312535533436645454347453545467746785575488446587668787558887463467345435652322365453343142313 +333023313452134226454456635364764574886675646587758884567648486854576437434736652646564143145413342 +134105212414452636233646446677654655547478864656677574578784868445788574536575756534346424211354320 +004111511452463433623563777743754767457886788586559968978657668848857455547475664223622231451253210 +231425442312332453557755476636755467675688856998977765968574677675455785675433663225554531115333201 +124445531132562536325476335364456677445588998868757697666786985857544675564437635334353523541435112 +002314442116354466363644545675644678689967785786789798578758958685655484644676347435562536232245230 +400145131452336525274473557654464558676985658789768977865877655664657458663373374764564455354413541 +002141524435534543377634638676587467985995589687759588896599677884847786856467363442555235244313233 +211312353532635354377353674675557447558785689899696557779759568777546775568455456442255222433414313 +433542544533525424746746776557785859999988779569786898985989757666656554745566637574245534533532121 +102115525355343357447434747864878698959765977979867866786777675895578765548855746376443465451125512 +351523154635255336464346858657666597765969966989679667699987766755796475565486736744752343364321213 +423235155355234367635766676588667966778586879877697788968986866566759764754786567645535554563312353 +131553436636563367663475685874458967969697977997979788867686658965787574768454574765644522224342452 +425311513252536365474678658644687955758996766996688996687966869869656656647766647546756256425343432 +035535316265363537444744585758789587587969697669788689878886799787756565545488643377356463666143551 +433111352346543655675648867788955697676896776696888997669867696797998575846847643365566322666253422 +142554222353623634663476656576576759599877876678987899796866667665667796758885566656763524326523412 +553221114466652537653487484688965785987978798787879778886869687879856576446844475475572665553322311 +322215126226235677355378464486855799889979787987789899998978999898888758566687435633462236545525221 +313112235462254453576755488856759997896886799799899988899779876666587756446785655466666326223312454 +244411355552535377345666688746856978696796898988979889888979699868688867445865557755755235454313531 +423534126443225554666348575686988895986769679879798788787867989986787998457447645534733633624642232 +534222565323663376567746886777776797666869788888888789789997876686865895557746476676743534663612234 +431433255624436444667577468887878697867788699999988897788987767687857655468476674645743656323315423 +345545125463354463476474887467975687668769888997978988987887696676889857556487434757444653262441443 +435223443263442335775687868588775867886768868998798978788987976865875989454457863563445263354635354 +345242142456554446376374765856966766687687679897898788899886788978978876846454864444446656626623124 +341531133422344366636755584885865569968787887898998898996888799658576887586564853536644355333123531 +531354324434252444447647888455677658969668989897887798896967867688667687764444653657655322242251515 +341243442253632754364654775476696998967688876686679796776869979996666974854784474465565646364251513 +032311355356424443443777684847859565797966967889979769999967767679557564857757576335644553354134133 +114145555335443455354745857886867865567989796787779986777997699798865678867787636364742266233335424 +344514245524255637545457646776555786678587787778666899669978956577965646645567343453432466522454441 +251343344325264256643575758776657986688857969886799699688966778578556554468453753547753446445535151 +043443135333624377566634886567746955695958989989876666788667678685785785467565477654355655425532543 +035122224554345443363445557578744885768878677698887879778669577958975774475734676457563326462311124 +021124344524353323337534574456675858888969557976787668957958679785646545746754447757546355225333125 +445245122262623336443754345874854655587699975769955678888677978667454758765665774473353522653153134 +104351424332455533735435545656474658675675897879799765785667865795858476786347753776343645631324454 +324542322453625462236334665774655447696856667668969589857996678545867547453554754724544322213213214 +034543343326354233237754643775676575846669956677888556676767776756756575665365735666445646541212213 +130313332234223236545753335468466765488769858995988665888657746744844845465747456426255455543114233 +333414434421244465363554774634545886574878696787956899785964544558487666454465433252355535125444511 +343134321142365333354375476744758675546757477969969756787556785478857636537677745565253615431223030 +024113431151145262433473566363374846657667848764887455678675566746848644354545643353233645124234002 +102102554415326564222565443454737686568866774874688677648666478578456576354334523555356551352521330 +214032555331126556325325576536535774644884484764877867877668468655443677744345435423522245154212003 +331000231233323222545443445746557477855878875855558887468857875583574453546353556365265531554514210 +230230424552513432642224545447756563555488468477556644765475748446436775655455522222211315542543210 +034320135334452542333332232775535436456684444444855755768555777637474433673263662664253121152140021 +300102443215153322652244553275445743347747738784785556788453473544744457466535556264245423134001034 +221330433531344432453662425623775347435735437537436475354373747763636375236342545441431315521104342 +242432404041145244545256562346644463454767445536533675363437667467336466445364256651231214512031101 +332012112315352345356344626244566335366433444363364747434465474775344245636463443145553211304430313 +014014200315441234414554263333352363376453765473366537567363537445554656442562362334315122140032213 +331231141401352354453214345322446344775336554354775777676774643466655266626653435541521122313333033 +022032100414421141543523356654425642335563433647543446343354573364364432655634342533311521220334022 +332024143231012134544415364652343322443645664433666673334362624566644436664443133454115432123322232 +022012440221044225513415343456223242262536443423746733235554554333652662342154134155131232242101021 +032200401321204114414341313223565442522255533422443554662333465325653524225531145252133030102012203 +132130302204012241232432525353555345426365455634555652356424333353266321331333533513424422431331312 +333331204200411111455334351333415422334456364533265252642262345446236431543241225432412423040033121 +021121100043220232314115541242255262646345554552356434566525625665344225331141424342424113302012010 +010232013234411124340321315251315331554254233364232653533255563233423221135354120214411114303133003 +000233213332310143202041141254132411515256356225645646462564621242424112342134113021100212310230021 +210233133023004431030124243353411441443323552256255462343313541135345353412553144201140321002301110 +120223033103020130313221301115522445342123311254451312533135135344423123513034312111432333300203030 +000032003312202042314110102051553525152114432152521441455154531252112324540404240320002132003031111 +120020023012112241030432400312341452211445333435122342123254353543241311324432422134302211113331100 +100200020233330030322133232440212423132312154431542113113331325525211133104212342300301221121211110 +121200200322220022421214003023013441412113134513431242215243245241223111100231441333010110323110110 \ No newline at end of file diff --git a/2022/day_8/rust/Cargo.toml b/2022/day_8/rust/Cargo.toml new file mode 100644 index 0000000..b29692f --- /dev/null +++ b/2022/day_8/rust/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "rust_2022_08" +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/day_8/rust/src/grid.rs b/2022/day_8/rust/src/grid.rs new file mode 100644 index 0000000..26dd3e9 --- /dev/null +++ b/2022/day_8/rust/src/grid.rs @@ -0,0 +1,99 @@ +use std::ops::Range; + +use crate::grid_peak_ranges::GridPeakRanges; + +#[derive(Debug)] +pub struct Grid { + pub rows: usize, + pub cols: usize, + pub grid: Vec>, +} + +impl From<&str> for Grid { + fn from(string_grid: &str) -> Self { + let mut grid: Vec> = Vec::new(); + + for line in string_grid.lines() { + let grid_line = line + .chars() + .map(|char| char.to_digit(10).unwrap().try_into().unwrap()) + .collect::>(); + + grid.push(grid_line); + } + + let rows = grid.len(); + let cols = grid[0].len(); + + Self { grid, rows, cols } + } +} + +impl Grid { + pub fn get_visible_items_map(&self, peak_ranges: &GridPeakRanges) -> Vec> { + let mut visibility_map: Vec> = Vec::with_capacity(self.rows); + + for row_index in 0..self.rows { + let mut visibility_row: Vec = vec![false; self.cols]; + + self.get_visible_from_line( + |i| self.grid[row_index][i], + |i| visibility_row[i] = true, + peak_ranges.x.get(row_index).unwrap(), + self.cols, + ); + + visibility_map.push(visibility_row); + } + + for col_index in 0..self.cols { + self.get_visible_from_line( + |i| self.grid[i][col_index], + |i| visibility_map[i][col_index] = true, + peak_ranges.y.get(col_index).unwrap(), + self.rows, + ); + } + + return visibility_map; + } + + pub fn get_visible_from_line u8, VisibilitySetter: FnMut(usize)>( + &self, + line_getter: LineGetter, + mut visible_setter: VisibilitySetter, + peak_range: &Range, + line_width: usize, + ) { + let mut max_num: u8; + + let mut left_iter = (0..peak_range.start + 1).step_by(1); + let mut right_iter = (peak_range.end..line_width).step_by(1).rev(); + + let first_left_num = left_iter.next().unwrap(); + visible_setter(first_left_num); + max_num = line_getter(first_left_num); + + for index in left_iter { + let num = line_getter(index); + + if num > max_num { + visible_setter(index); + max_num = num; + } + } + + let first_right_num = right_iter.next().unwrap(); + visible_setter(first_right_num); + max_num = line_getter(first_right_num); + + for index in right_iter { + let num = line_getter(index); + + if num > max_num { + visible_setter(index); + max_num = num; + } + } + } +} diff --git a/2022/day_8/rust/src/grid_peak_ranges.rs b/2022/day_8/rust/src/grid_peak_ranges.rs new file mode 100644 index 0000000..db3c124 --- /dev/null +++ b/2022/day_8/rust/src/grid_peak_ranges.rs @@ -0,0 +1,63 @@ +use crate::Grid; +use std::ops::Range; + +#[derive(Debug)] +pub struct GridPeakRanges { + pub x: Vec>, + pub y: Vec>, +} + +impl From<&Grid> for GridPeakRanges { + fn from(grid: &Grid) -> Self { + let mut peak_ranges_x: Vec> = Vec::with_capacity(grid.rows); + + for row_index in 0..grid.rows { + let mut max_num: u8 = 0; + let mut peak_range: Range = Range { start: 0, end: 0 }; + + for col_index in 0..grid.cols { + let num = grid.grid[row_index][col_index]; + + if num > max_num { + peak_range.start = col_index; + peak_range.end = col_index; + max_num = num; + } + + if num == max_num { + peak_range.end = col_index; + } + } + + peak_ranges_x.push(peak_range); + } + + let mut peak_ranges_y: Vec> = Vec::with_capacity(grid.cols); + + for col_index in 0..grid.cols { + let mut max_num: u8 = 0; + let mut peak_range: Range = Range { start: 0, end: 0 }; + + for row_index in 0..grid.rows { + let num = grid.grid[row_index][col_index]; + + if num > max_num { + peak_range.start = row_index; + peak_range.end = row_index; + max_num = num; + } + + if num == max_num { + peak_range.end = row_index; + } + } + + peak_ranges_y.push(peak_range); + } + + Self { + x: peak_ranges_x, + y: peak_ranges_y, + } + } +} diff --git a/2022/day_8/rust/src/main.rs b/2022/day_8/rust/src/main.rs new file mode 100644 index 0000000..621bc92 --- /dev/null +++ b/2022/day_8/rust/src/main.rs @@ -0,0 +1,21 @@ +mod grid; +mod grid_peak_ranges; + +use grid::Grid; +use grid_peak_ranges::GridPeakRanges; + +fn main() { + let data = include_str!("../../input.txt"); + + let grid = Grid::from(data); + let grid_peak_ranges = GridPeakRanges::from(&grid); + + let visibility_map = grid.get_visible_items_map(&grid_peak_ranges); + + let sum = visibility_map + .iter() + .map(|row| row.iter().map(|&v| v as u32).sum::()) + .sum::(); + + println!("{sum}"); +} diff --git a/Cargo.lock b/Cargo.lock index 34f4e18..b53ecdb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -29,3 +29,7 @@ version = "0.1.0" [[package]] name = "rust_2022_07" version = "0.1.0" + +[[package]] +name = "rust_2022_08" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index ab7056d..70f5a7a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ members = [ "2022/day_4/rust", "2022/day_5/rust", "2022/day_6/rust", - "2022/day_7/rust" + "2022/day_7/rust", + "2022/day_8/rust" ] exclude = [ "2022" ] \ No newline at end of file