Skip to content

Commit 55e74a4

Browse files
committed
Day8
1 parent e7ded0d commit 55e74a4

File tree

6 files changed

+329
-1
lines changed

6 files changed

+329
-1
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ members = [
66
"day4",
77
"day5",
88
"day6",
9-
"day7"
9+
"day7",
10+
"day8"
1011
]

day8/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "advent_of_code_2022_day8"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]

day8/examples/advent_input.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main() {
2+
let res = advent_of_code_2022_day8::count_visible_trees("./day8/resources/input.txt");
3+
println!("{:?}", res)
4+
}

day8/examples/advent_input_part2.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main() {
2+
let res = advent_of_code_2022_day8::find_best_scenic_score("./day8/resources/input.txt");
3+
println!("{:?}", res)
4+
}

day8/resources/input.txt

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
101232232424431123432342554040352101405502025646520640026102435415402415524241404341001022013221221
2+
322003200404313034023203145443134010555246652210042156042654635314322525041550121322403330300102030
3+
300223331202022110511510154103110142442012332003514242206354525511353252421542430110200442102312123
4+
323133401312301310040251134225352136126616004434252462045055305202261355433025233042244241414400023
5+
101104403040123044220034201231621525016350121330311611014454116414515052210415423202213344444213032
6+
023343200312321341111312312116004553510400124564003503525336352206534312634553034341344231103320121
7+
232432314323445335412313341660535056605316245521312422204421411026063010063200110052210224330113302
8+
312104103023300150511003256056530232416620641476331566726505005253550226461151520445555123411440334
9+
212320403040021521143113451320122514052261721742217616232666314261562006620634125252332350322233244
10+
121203010304521210411013440333264052674653423316314375512175574325262425522003540134541121330431412
11+
242204210504314451204236552066106466756545677241223651236433541213145114512054256134345025222414332
12+
142041442305525524414554456630445566671622363554565311414164346521375126616036343225552430320142242
13+
444343225200452531125463161044526512711315644346716774456167633775552325212323502414500125304101442
14+
310331200433511524004324523024376145644272331714275745132552527332621757111641110604243400150102301
15+
324000410032344243443213031215347666416142612722352331527416524446272144632054626460610411343015214
16+
320123043045014453006030311336322753322266368648765847556774576424774346643352662320364111534525122
17+
442020510341512162315013222156456642565478638555682735664832583554422245165431250246236222000222413
18+
414115045035446663521337425454354252666867442648382483378248585426324451353146435112651062444320153
19+
404031553354665600421273415333424442557623485578324525478673862565714171753646445146504611043544310
20+
420221121313524320031226342733736342778854854745272737436344833444332262273475742543435414301514022
21+
102433434104221136261652367123828833742266365587682657232782236687733375147446456435013164623033453
22+
254052000640223505333752451714435245653688563475537427586864344637533256524566641541066561013340115
23+
402424554664414061612366662262546262344862767983768964383722283664424826344415366464635564003035522
24+
453430306616221255375164464844337767643264664566433699446375333477788728421713267327336401105425333
25+
424340460333633611416743647223584543446663447884569869575943675246265858843135752722203060601314510
26+
100104404661641521713564623486853464898563575445444677875368568656336855687665453132126163413415413
27+
215302243412504531566112426534574499379653449589737746345943794657936355232887343422456306315453552
28+
510025400262416244231532674726546437535536634838644683689356756484986473434524723553734023111254223
29+
200204056365027317421364428852638397454355393937474567465453855598477485557424664756156234516125002
30+
315311346005136326466555385687277868747539849498675894853944993694973352587363421743725352145116525
31+
003432325612552157642734768322776884596464869575664456575756766387847762385773543511142455502615010
32+
323005315540227472658648485349375446787359896487975999967994878588768333826274268465567534243515604
33+
440456221306764751672857476368954437466478794954588479785445855488353476362467345856473677540155641
34+
145626220661445722124873768476448493998999449996678498948886598577843394688625385873326644342243461
35+
445562611272117173322737877366885654845578785685455598488999678884466965543672555556173431656206420
36+
025130063437366574263434683849438959768954744996857595658945978854575385745862424366457372746221315
37+
330056343042353748468655383756666954488668996955869957549949945974898849943638748754612625120241011
38+
432440053115343158276372498785736949588874688959799775598548957878884775394762348284137737673641642
39+
102056040435747284853655648443579469664944568788855866779976889755858957959735324265746533251062403
40+
225266001744262443233334974839654865799445785659659675575666575758865948754463425885535443736632404
41+
110654201772435145572468544985578676579996656795879868576797847675977856844578738333816426565246622
42+
433122252566473253723864943786447484655995769756667897965855958658757798373853334626865115523210441
43+
556541053615336747283763545495655688878588665885858779779899597977449644485399674475255622322020110
44+
011246513266431242864686953687878987686577868895997988788689566679898456889984483487464125476243224
45+
146430654214211787457337933537555866765688875799887976988578679556659655564463526386463716114354031
46+
301355426474121386642848739759597465855588776978689979798756956985699554573779455545642175346410200
47+
665401551644225425227738385396554475758768868798997798968696775989794666575779876332737347375414225
48+
512264373337161748463436934959669586879679979679896989676576695988989664437878448276827532543745623
49+
414644336467578472474486349936485857567669856689768668976766988984549484863488738455255412422624432
50+
036441527146156483775836379836679964765795668878769977878799767597698768864788657252857655434752204
51+
444666167464342238764266465439498755555659877886888899999856978994556568955445528325646125672164600
52+
554433027362778886523875976836986988896695897689678886787996679658974797569374338344624672757631301
53+
310042272613272624885896949859765467876555996976999696789988967965466684754895746762223775172234330
54+
216343113411562425626633578339674859855875577977769879987785777674498658748899947272432652777416624
55+
644655617213144523726243646388856494887979798868676998696768878865895676484383655444254627274131426
56+
563636621655445248723867836434648597896597866798698876698598597697577776937367826877324161661354535
57+
341056514244726338453499673897657855866875759776669996869785685895756748635866544776371413425025432
58+
044435013274135828365337573975868886748866596788969877955955756895957574564469583387354142524005261
59+
410342122777647883362439734978585444678669878589755875966798767797995783366779884352756543534151243
60+
441043541147123324228379874699597946948555885687569696976589658895855548467778233287211424145465240
61+
013520313145473358567557484588597487658658866969675558679557775795544577887779883542724441435601554
62+
413424243572773133387782457533847759697988966666777979879595749686579798343746884752342364430012243
63+
326240460425117754473464599468874499568546888968889968769655795459984446735672755758632161131203442
64+
300131303151234667345248566663377796947878745698979798686969577865488637644574724367556777536526223
65+
201413522556166618225635288589984557686665556767657595967679955648876746974783378576524471465423550
66+
132203011242354537628633669369699337985447975976769776866447886769358896665782776877447544703406253
67+
133532343456535111383888625455386597867455655599757897755558666966586346883426636652263242705454404
68+
515656631143165354267432455336475494569987585578555494867798784856975634394283638544164731533012364
69+
140416432445724172247278853455798367884868796498777454967657748875548789832362674732676672056032431
70+
330330152414564665274243438433876363573585996985888846846845663567545668252624564762246173301325502
71+
302226105363143741352735557782868788733464859494486947884994938599357344542482523627145432560642353
72+
145422126105171466242567724557544489934658889494656859885599738537534845443644581256761746362221023
73+
441212145032074213275667582428345868377677696997785565377585765567746584667226657214272426340532331
74+
101452412564254231314347763234647978536934546886949376683847693599962882662556735366332605253254243
75+
242134245465654443763242368746262775635568895968649374696544643593776484646237623121155311612120205
76+
224324556334241543152423764688727563658665947774377475559746775387478253676754241443542553641515133
77+
112040215053533166723764146688552662254436968389648683363668663468622524888354726754315425216123100
78+
021125312165155354236637163434868472666833569875463833389786585853644277477624254454065142204421252
79+
524302450104653624156271174347323843256576684986975339949343684428633575452572646373611442213410412
80+
013000504615163043526333634318868574767387865682722275245644673556658444321441613432604542203511551
81+
242524501014131565575336754147245826628343573377352338767422674636538742125631345462033033403235255
82+
304401342145144243415545241676576556264543764465565724532645542743372525221461725562353265045051132
83+
234302031320344616366225577677124763333456446683735866667852458578422752674576640145530645050512154
84+
132421020111321255530526663734272513535262623562488642564636574385245112665623132642441334102314011
85+
102255335052511135645343464117315257162723258847453624673355647345665737125672220566436213014134441
86+
312245204230352351210405226233122343511172545755556267356584643164226213732752212035632152433154320
87+
433104142453501532612362446717232767156326652752737733565427127523161253271344216136060252512442041
88+
212230512443134051666444123274474475364671426411226276576372452514665156262633036430153240020401401
89+
300411330411451231141436052664142373444325735263226333375416444576253146002526306114152241250403310
90+
101032115301513352525160453141002773563414647477456721366627231126771542123126102455031250033223313
91+
324233212142235532140166110023400026333355425117234444773232146225326253461050325545343105500423104
92+
033431121034120033510616215166546146337352752676632624643526424216650102540533503153134132000420142
93+
240121103430140123513513462300432156652122676414274512336151334241532064154406340323144200421003324
94+
233142431112012541421523134462664026316416350553765272706623513350020256351263445534113304131112033
95+
323203003131024144404243206121423310355043066662201116504344265455134633403341445324024304010343200
96+
133044014403300041520020101412541120000145660632333004501603416535515112534533414430000213113231220
97+
020200134443230224532201430335140300110631641143466045463552301460233403451514045510012313003444012
98+
003320111443312431412215134024143654125511235514301244523512535041410415313130552501342112344000332
99+
223322121002242440120225413345342544646200604640400343653451404544533034511344250023302010222012033

day8/src/lib.rs

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
enum ShadowDirection {
2+
North,
3+
South,
4+
East,
5+
West
6+
}
7+
8+
fn is_shadowed_bidirectional(a: &Vec<Vec<bool>>, b: &Vec<Vec<bool>>) -> Vec<Vec<bool>> {
9+
a.iter()
10+
.zip(b)
11+
.map(
12+
|(n, s)| {
13+
n.iter().zip(s)
14+
.map(|(n, s)| *n || *s)
15+
.collect()
16+
}
17+
)
18+
.collect()
19+
}
20+
21+
fn build_shadow_map(tree_matrix: &Vec<Vec<u32>>, direction: ShadowDirection) -> Vec<Vec<bool>> {
22+
let mut shadow_height = tree_matrix.clone();
23+
let mut res = vec![vec![true; tree_matrix[0].len()]; tree_matrix.len()];
24+
25+
match direction {
26+
ShadowDirection::North => {
27+
for i in (0..(tree_matrix.len() - 1)).rev() {
28+
for j in 0..(tree_matrix[0].len()) {
29+
res[i][j] = shadow_height[i][j].cmp(&shadow_height[i + 1][j]) == std::cmp::Ordering::Greater;
30+
shadow_height[i][j] = (shadow_height[i + 1][j]).max(shadow_height[i][j]);
31+
}
32+
}
33+
},
34+
ShadowDirection::South => {
35+
for i in 1..(tree_matrix.len()) {
36+
for j in 0..(tree_matrix[0].len()) {
37+
res[i][j] = shadow_height[i][j].cmp(&shadow_height[i - 1][j]) == std::cmp::Ordering::Greater;
38+
shadow_height[i][j] = (shadow_height[i - 1][j]).max(shadow_height[i][j]);
39+
}
40+
}
41+
},
42+
ShadowDirection::East => {
43+
for i in 0..(tree_matrix.len()) {
44+
for j in 1..(tree_matrix[0].len()) {
45+
res[i][j] = shadow_height[i][j].cmp(&shadow_height[i][j - 1]) == std::cmp::Ordering::Greater;
46+
shadow_height[i][j] = (shadow_height[i][j - 1]).max(shadow_height[i][j]);
47+
}
48+
}
49+
},
50+
ShadowDirection::West => {
51+
for i in 0..(tree_matrix.len()) {
52+
for j in (0..(tree_matrix[0].len() - 1)).rev() {
53+
res[i][j] = shadow_height[i][j].cmp(&shadow_height[i][j + 1]) == std::cmp::Ordering::Greater;
54+
shadow_height[i][j] = (shadow_height[i][j + 1]).max(shadow_height[i][j]);
55+
}
56+
}
57+
},
58+
}
59+
60+
res
61+
}
62+
63+
fn build_tree_height_matrix(tree_heights: &str) -> Vec<Vec<u32>> {
64+
tree_heights.lines()
65+
.map(
66+
|l| l.chars()
67+
.map(|c| c.to_digit(10).unwrap())
68+
.collect()
69+
)
70+
.collect()
71+
}
72+
73+
fn _count_visible_trees(tree_heights: &str) -> u32 {
74+
let tree_matrix = build_tree_height_matrix(tree_heights);
75+
// Vectors represent which trees are visible when looking at a certain direction
76+
let north_shadow = build_shadow_map(&tree_matrix, ShadowDirection::North);
77+
let south_shadow = build_shadow_map(&tree_matrix, ShadowDirection::South);
78+
let east_shadow = build_shadow_map(&tree_matrix, ShadowDirection::East);
79+
let west_shadow = build_shadow_map(&tree_matrix, ShadowDirection::West);
80+
81+
let visible_north_south = is_shadowed_bidirectional(&north_shadow, &south_shadow);
82+
let visible_east_west = is_shadowed_bidirectional(&east_shadow, &west_shadow);
83+
84+
let visible = is_shadowed_bidirectional(&visible_north_south, &visible_east_west);
85+
86+
let shadowed = visible.iter()
87+
.flatten()
88+
.filter(|b| **b)
89+
.count();
90+
(shadowed) as u32
91+
}
92+
93+
pub fn count_visible_trees(input_path: &str) -> u32 {
94+
let content = std::fs::read_to_string(input_path);
95+
match content {
96+
Ok(content) => _count_visible_trees(&content),
97+
Err(er) => {
98+
println!("{}", er);
99+
0
100+
}
101+
}
102+
}
103+
104+
fn count_view_north(tree_matrix: &Vec<Vec<u32>>, x: usize, y: usize) -> u32 {
105+
let mut count = 0;
106+
for tx in (0..x).rev() {
107+
count = count + 1;
108+
if tree_matrix[x][y] <= tree_matrix[tx][y] {
109+
break;
110+
}
111+
}
112+
count
113+
}
114+
115+
fn count_view_south(tree_matrix: &Vec<Vec<u32>>, x: usize, y: usize) -> u32 {
116+
let mut count = 0;
117+
for tx in (x + 1)..(tree_matrix.len()) {
118+
count = count + 1;
119+
if tree_matrix[x][y] <= tree_matrix[tx][y] {
120+
break;
121+
}
122+
}
123+
count
124+
}
125+
126+
fn count_view_east(tree_matrix: &Vec<Vec<u32>>, x: usize, y: usize) -> u32 {
127+
let mut count = 0;
128+
for ty in (y + 1)..(tree_matrix[0].len()) {
129+
count = count + 1;
130+
if tree_matrix[x][y] <= tree_matrix[x][ty] {
131+
break;
132+
}
133+
}
134+
count
135+
}
136+
137+
fn count_view_west(tree_matrix: &Vec<Vec<u32>>, x: usize, y: usize) -> u32 {
138+
let mut count = 0;
139+
for ty in (0..y).rev() {
140+
count = count + 1;
141+
if tree_matrix[x][y] <= tree_matrix[x][ty] {
142+
break;
143+
}
144+
}
145+
count
146+
}
147+
148+
fn tree_scenic_score(tree_matrix: &Vec<Vec<u32>>, x: usize, y: usize) -> u32 {
149+
count_view_north(tree_matrix, x, y) *
150+
count_view_south(tree_matrix, x, y) *
151+
count_view_east(tree_matrix, x, y) *
152+
count_view_west(tree_matrix, x, y)
153+
}
154+
155+
fn _find_best_scenic_score(tree_heights: &str) -> u32 {
156+
let tree_matrix = build_tree_height_matrix(tree_heights);
157+
tree_matrix.iter().enumerate()
158+
.map(
159+
|(x, v)| {
160+
v.iter().enumerate()
161+
.map(
162+
|(y, _)| {
163+
tree_scenic_score(&tree_matrix, x, y)
164+
}
165+
)
166+
.max()
167+
.unwrap_or(0)
168+
}
169+
)
170+
.max()
171+
.unwrap_or(0)
172+
}
173+
174+
pub fn find_best_scenic_score(input_path: &str) -> u32 {
175+
let content = std::fs::read_to_string(input_path);
176+
match content {
177+
Ok(content) => _find_best_scenic_score(&content),
178+
Err(er) => {
179+
println!("{}", er);
180+
0
181+
}
182+
}
183+
}
184+
185+
#[cfg(test)]
186+
mod tests {
187+
use super::*;
188+
189+
const TEST_INP1: &str =
190+
r#"30373
191+
25512
192+
65332
193+
33549
194+
35390"#;
195+
196+
#[test]
197+
fn test_input1() {
198+
assert_eq!(_count_visible_trees(TEST_INP1), 21)
199+
}
200+
201+
#[test]
202+
fn test_input1_part2_test1() {
203+
let tree_matrix = build_tree_height_matrix(TEST_INP1);
204+
assert_eq!(tree_scenic_score(&tree_matrix, 1, 2), 4)
205+
}
206+
207+
#[test]
208+
fn test_input1_part2_test2() {
209+
let tree_matrix = build_tree_height_matrix(TEST_INP1);
210+
assert_eq!(tree_scenic_score(&tree_matrix, 3, 2), 8)
211+
}
212+
}

0 commit comments

Comments
 (0)