Skip to content

Commit f3b00e6

Browse files
committed
Add day 8
1 parent 10cd538 commit f3b00e6

File tree

5 files changed

+236
-0
lines changed

5 files changed

+236
-0
lines changed

day8_input.txt

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
020111211022103023211142403044034010412103202221251542233334311102303044440313120021223120332211221
2+
121001110111201102213124201444431204213221413222211111444444412110041144131210002230130121313101010
3+
112010221202200121420032021412024323255214355122345552225213252324112400440302313420323102103100221
4+
211022232121222123201224234031435144114512425232345144523531533222344004011342234110131312300332202
5+
202103101023303021233104321411251553151424242531344221312225231225524421412434024103021300123300121
6+
202122200002211022342141123051132352344252152315525324551424132421225143201002114204223302333232221
7+
011310312223223421431113235421121321152124551441143435255214322514544552225412313011414423301132102
8+
121331322013001133311000223433443341113554412556562564315233144532455221433431002003332002203013201
9+
003201332004241133340023541532333131413153266664363423353232622124454254554345422200200433100333102
10+
230030131344203011040552414515334144165664524445366334422626244444323115333252423443232113432003211
11+
020300112334333142144323414353551436334563654426436625356343222253643212532215422020214344013310331
12+
220002231221003314452445344223355655546325233543653632544634656464243551334233433413423044300230210
13+
111022100242320043435324555421362243363346366346456252353246655455262455415331454531201000441410231
14+
101232013434204241242451442362524554225656254252456532663634445256345634451144555513214431300213021
15+
021113211441142541244515256653533353262654644343347336234522332254544443346322422154143020040021110
16+
002032342223245141521455555365463263562446777664546776637635443223664266645633333231151132112140113
17+
002033421301452414544444345232465536465366346553375665564756366435442422632435555111445311104203133
18+
332303032232445231354236322622423634433537753546335477544636344436526324435436522445155410213401230
19+
214021124335354212514535634245552337436663757533364556376776733565333462254562365531354214423412431
20+
231123230423124441525624265443647673566756453545563655373357453547537663433453433212213223101140424
21+
100211320333154123522263362425466444634667564437776546465446345667366445453365366551352122420231230
22+
024400401554531424535342424657437377754456437376767656436366374765557746355562226364213315421212320
23+
214442223135544515264335435467756354735433346664764464468465443746334373746425422542525455354340110
24+
141333001441254326655252563743544735555378755475567457777687677357576573543344653346551442221242312
25+
022033123224325536562464253467675635635678458757477754757874576653435356676234462564555551451230022
26+
444402313413432333426636637636453646648557685556587678886878777785454364533433543362622525453201222
27+
031024134513414333456224636475357556855564445765756678575486648885765563445546566235352311454320030
28+
341331341415512452344636337436467675565575756658588888477584775646657736366434365523664142235144413
29+
333344214132152543325563654447567486457556845676545687457688885466457333476777342344625323533242333
30+
203124152315365546566375565336778768544768846599686578655665448478657834657533335544343231212251101
31+
200144235323636235437557635373578555545475587655886585897998654658858755534374677344254563233421200
32+
433243323452635426237436445738684446475489658878699879887988684656474746764334543334452565153341241
33+
340411231113223465344733555465744685569658998876796578688888887678657675875533766464362432543531231
34+
132155112416452662274535754677457567869597559977756878779755568868854644586734466764422245333151550
35+
321153454153653654743477667888756477986578656976558879999589975695487876766664653466625262642234421
36+
414524133155254243564664555445778475666688565697596679558768656758547564888437343772346336642515514
37+
321251321324554554436434474754674597576658986766997798688766897876884764444653674674326233445555243
38+
031553412663524646375654566887688857698655696699699989968699659665774657875735457664452362352111522
39+
121541141642244257534355645887786995858959886778688769889987779898696567777567344373632655621545545
40+
334343242432423553675774445756455759676757789677879778999988695976997447856786357655446656351514552
41+
125145532643224566754564678556569669557567799868776897666898866898879866784847777744353546366314141
42+
045114213352452647446647465888875868689699789677897977779999699895556776458746737674746244442322442
43+
132343555663565436365447746478789879798676878686777999789966769777759954677478745673552645533545344
44+
045253354653642556466557766588567888887978866878777776986676768968695857655674556463732652626443441
45+
324411423536354677366485874488885578788769789998797978797888777868996895476778847746764226464355343
46+
355421453642352476436787475855785669696986976979989787787998669696679989555875747556435562425323311
47+
225422326356526465747344444546866698678986799879779799999876866969999686664477767757535635652555421
48+
215234242322226376376776764668889755898766677787987779877967696689565966757675463574434246652622343
49+
214252444222333767567367778585695867987669688798987987789888698888858597545545475573737453632634414
50+
435144324646623745547785547465689576878976877888878988989877866686976879488454664357347554642541254
51+
424224446333664566656688545685586987987699788897978888788999878967887869445474863534753362524653532
52+
234521532264544735666677658457578679997886798878788998987976677895559685784445835563745433436535545
53+
541253562333326733576345574569866788667878789897999998887996866685657897848485475437744246636412312
54+
531515163362265375476387485646556857878999799999999989879989886696789765664474863474462523344554532
55+
134323536435565375644658845476675857999867789999988888799986676978987577885467643356656525623333421
56+
432434326635433776557758756889896978587689688777989999797796888999765958586488873373565325435254453
57+
352344243543664654777655867556767767686778796989888798998697976755779565488844544677343523234525134
58+
023522455223435563774634466685769765877876776996699877886876668865856964846675357575675235445525211
59+
452211123455454466574537744568597875596879779869788776669767986968956557448486336534544244456115451
60+
154255352645344566574476875484685575595898898697799799688669879869766768688476473673634645556554324
61+
153224532366353377543578485765465665577767776999977978876879865565957587758448456773452265664315213
62+
212223354662352476456344644856876756566976677698968768897897866997679566845464663374352444344522513
63+
333134124655526377555445475778655559695856687767878889687866969787796886748547665457422543622531314
64+
114114243655626456537464558857564557866777897897898988696885586978887448448543375665233363532515252
65+
022123225426235336675765546686448888566865666569876966556979869667956845877753374733454555422452344
66+
105112231544246463366336777688875488697685796985999888976956985978664485756743456372244565641412542
67+
243421525526425322655767646868764584599579555769857975877669799998745668566534743565233224245452434
68+
310412235133463356343754335447454467578555798898788997569756589585876868467475435524433556255213522
69+
122253434533466554567356664688567676567976599967878669868967997845484546736534545734455626245154423
70+
313334534424352263526466637778587787868565658999559897596678677574858776343543534432632333531241224
71+
001031252124265545567437743765668445564487997555965858678954577555545545743366457636433553523254344
72+
044305452425444342246365565576568644874888888677955886697487686854588744354437555336664241413533111
73+
110113123534526242542645447354547688467557884774478677887477777586654656653436654455626314334324023
74+
403311355254242426345425533745657486754657454674877454647844866774463676664344325662334351444144012
75+
114043453225323265365363357353773386544675474785655464585675566555543364665665355442555535443420344
76+
234021355512324262426566274773756436677647664566764646854655875476556464746532226425352124354233001
77+
104234323224222234534254555373773666356744475886877868685764755374634533664662562645321154355101301
78+
413312132345313224565423553463467666456365585778746647466885776356666677636556254365513311121044324
79+
010140223145323354554344523347476477547564757555585887484666633663433654743326266555221322431333120
80+
023311233225342421354662654347733666374666336373663635367447753577353743232632663243333251551031321
81+
143043334422453221246324462653756444767736544367544755375437335646557334655533644454121143303220002
82+
101212344011254432246625336226267546745356655577456676377355677475746633565634244145133154032123242
83+
233244204414154431434433222525444454344635736634774576466473546373725623264634363324244114303014300
84+
010103422332213512154554324533443662535535356636747655647737636354525554536436123454141421414030443
85+
203123300244432323224135253642444433566335637575443374634433336553444225543564152215332533001133230
86+
213003230312403552352142145445635666323653353353657647435752235322633623453542112525124011312421020
87+
120004434213011143254452233425222536464236356544634635324242345464353322232141135221424123200431123
88+
331000214011314431233211121223425334456246345226324246526344536556636556453143153253323114441131132
89+
303123142412333143322451153243464566344362342525524265432552566442626262524431454151443133021301210
90+
220100102032004440315125323552226234566265233246334556355333643662354455122325544112124232340231031
91+
112122312031202424345533512342151435534325334444254264542526645645533111234131131223411304322200110
92+
213031212133401330114433121533455425225634422363334633454623522242353342455454511303033230433202120
93+
223232022333401311421344332255311133421254642562465526534445512134533413153142410113101444330132130
94+
020133013100324344342244215442123125243314222446342424421353134352515124333133430431401122202211032
95+
102300310001014311443112031115242425132234255215315523551534154524134344433432201022200432133000302
96+
001233311232134334100401400151532231322435145412132433235254313414243334103312232304403111313321210
97+
110213022233310011220103432114343112114222424442543155552411511144115122143322304023012132213011210
98+
000220123220323122321103444304134551331233141552114422121151224211122042434324042030120133230110110
99+
020120232301110331441234422110132302233413123211331413433254521355004032312110222131010330301222211

day8a/Cargo.toml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "day8a"
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]

day8a/src/main.rs

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
use std::io::Read;
2+
3+
fn main() {
4+
let mut input = String::new();
5+
std::io::stdin().read_to_string(&mut input).unwrap();
6+
7+
let map: Vec<Vec<_>> = input.lines().map(|line|
8+
line.chars().map(|c| c.to_digit(10).unwrap() as i8).collect()
9+
).collect();
10+
let mut visible: Vec<Vec<_>> = map.iter().map(|row| row.iter().map(|_| false).collect()).collect();
11+
12+
let height = map.len();
13+
let width = map[0].len();
14+
15+
let mut visible_count = 0u16;
16+
for y in 0..height {
17+
let mut altitude = -1;
18+
for x in 0..width {
19+
let tree_height = map[y][x];
20+
if tree_height > altitude {
21+
altitude = tree_height;
22+
let this_visited = &mut visible[y][x];
23+
if !*this_visited {
24+
*this_visited = true;
25+
visible_count += 1;
26+
}
27+
}
28+
}
29+
30+
let mut altitude = -1;
31+
for x in 0..width {
32+
let tree_height = map[y][width - 1 - x];
33+
if tree_height > altitude {
34+
altitude = tree_height;
35+
let this_visited = &mut visible[y][width - 1 - x];
36+
if !*this_visited {
37+
*this_visited = true;
38+
visible_count += 1;
39+
}
40+
}
41+
}
42+
}
43+
44+
for x in 0..width {
45+
let mut altitude = -1;
46+
for y in 0..height {
47+
let tree_height = map[y][x];
48+
if tree_height > altitude {
49+
altitude = tree_height;
50+
let this_visited = &mut visible[y][x];
51+
if !*this_visited {
52+
*this_visited = true;
53+
visible_count += 1;
54+
}
55+
}
56+
}
57+
58+
let mut altitude = -1;
59+
for y in 0..height {
60+
let tree_height = map[height - 1 - y][x];
61+
if tree_height > altitude {
62+
altitude = tree_height;
63+
let this_visited = &mut visible[height - 1 - y][x];
64+
if !*this_visited {
65+
*this_visited = true;
66+
visible_count += 1;
67+
}
68+
}
69+
}
70+
}
71+
72+
println!("{}", visible_count);
73+
}

day8b/Cargo.toml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "day8b"
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]

day8b/src/main.rs

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use std::io::Read;
2+
3+
fn main() {
4+
let mut input = String::new();
5+
std::io::stdin().read_to_string(&mut input).unwrap();
6+
7+
let map: Vec<Vec<_>> = input.lines().map(|line|
8+
line.chars().map(|c| c.to_digit(10).unwrap() as i8).collect()
9+
).collect();
10+
11+
let height = map.len();
12+
let width = map[0].len();
13+
14+
println!("{}", map
15+
.iter()
16+
.enumerate()
17+
.flat_map(|(y, row)| row.iter().enumerate().map(move |(x, &height)| (y, x, height)))
18+
.map(|(y, x, tree_height)| {
19+
let score = [
20+
(-1, 0),
21+
(1, 0),
22+
(0, -1),
23+
(0, 1),
24+
].iter().map(|(dy, dx)| {
25+
let (mut y, mut x) = (y as isize, x as isize);
26+
let mut distance = 0;
27+
let mut altitude = -1;
28+
loop {
29+
y += dy;
30+
x += dx;
31+
if y < 0 || x < 0 || y as usize >= height || x as usize >= width {
32+
break;
33+
}
34+
distance += 1;
35+
let this_height = map[y as usize][x as usize];
36+
if this_height > altitude {
37+
// count += 1;
38+
if this_height >= tree_height {
39+
break;
40+
}
41+
altitude = this_height;
42+
}
43+
}
44+
distance
45+
}).product::<u32>();
46+
score
47+
}).max().unwrap());
48+
}

0 commit comments

Comments
 (0)