Skip to content

Commit 5932ec5

Browse files
committed
Day 8
1 parent af0ffd8 commit 5932ec5

File tree

2 files changed

+258
-0
lines changed

2 files changed

+258
-0
lines changed

input/day8.txt

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
120121010220011210213341221400410032024200305455123332224004344232032410401431102230313221032220201
2+
201110121023013230302330021311131013421112311431532122324332345241340200121101343122032000302222002
3+
102002033003222123334224022231014024154332332522532413152443343521420024142120221020312130000012220
4+
022021002022122234142411233442032215244333211545534421223521213425452321323211314230012132230101111
5+
000001213311312040044040324004345251313532532244214111234445435523443223231014031402202003220202022
6+
120012020112324402101203201012524422234555211555441254543532525542455422431100123100134112211321001
7+
201331122323231022322414132513445443542225314522442122134451213343255351243403333214100001221310002
8+
202213230323304224323401211524355443322423124143363225331551434545141211212254401343122332131333320
9+
121211210204211420223411323113111133452563332665665366256342425412125353114131102200130004332132023
10+
112113312123342311122121342553114544542544233526256565633442525642534431424122340140222330201020323
11+
212220201231231331225325433524212465354244363465433323422435453425443544233314142214213330210013003
12+
323312311034244021124115523324235453334553635453645456346262262243535541114542314510442100141301122
13+
322110024020203105422444554225662245423642666363652244366624233565255245323234242444442021114003131
14+
020022410424334054535554351356634265526252563433644446436343335353246322341442512355214000224142031
15+
113011113224033331332141536246552466454462445665475466223436433443654356455435321342422024224240233
16+
122102233311322113321454362252243234624432447544375657355353324635433643234333542125422313433332321
17+
233120110314034111141332425232564555665674743363443764643543745663655355633235321453124432104042231
18+
100223100402324212124236262364446355557657673733745547363557734565242545543336131354254512432331033
19+
220330034134143234154666534352252567443643755654677737776567374445733425452244254511151514234122213
20+
223443144015521314236554452665635663377377657447643476573534645467643254356634655544313443004430033
21+
101024341052411454165252566424674556674773657353656433343654447535674354245262332333523555242331132
22+
220222110223533215564454533347744755673537673333455433773653447746564747422325642363122431352422211
23+
004240214354322434225324663557754746553337448764574547564453643767763554322626552333144243243100443
24+
244002112413445223342664642343745646463566545585488848785555544433334435346654656363551511425432311
25+
304400052221222432253436664545656765657858858457776876475466485467576564653534555232555135554512103
26+
301034122214253266644646654774435447685475487674646855754787758675544354374563635536335552414541214
27+
023143512353534433525535355774437554848684458474566677886556568485755464753466263454354215342330021
28+
114124535331116466652546536664743784557757457584466558864655558574887544735673255226463153414244423
29+
321342532351563246464464534353744766487574566878456865566554674786854545747634762245326241111545302
30+
222324535212433454342577763447374686575568646767976677984686747686474557575435676242252334324424133
31+
041455115451655624645633347335876745564448989976988776878785466666874474733445456453534561112433131
32+
234035233445234443233733563648557487657498899968897866857865785856886577346464734446452554244252101
33+
042322443436343444646433663564848665688656875758665978859999876545768558637766334653546264321513150
34+
311341431232552555557665566457765567779965958979678879768989766764576764655455375556543222315153443
35+
432422413526624252456643546576745546967687555668559776976867857598647675565463476444325456215124324
36+
141355224535323342657744536454664777877655975965957569957665675698786568684357375345633653521322134
37+
141122432443266326344466678445475489879775756757768969966588589795858867477644375464354434554334542
38+
404524251535424526674473367658588859789659999696879679797895959568796848845577655453633456432522251
39+
052555241454534576467457557666865778585785777799889798799796659588785886875774465345553622342424344
40+
431231215564564335455644575547778969999599889767689766876767676769986454648577766665523563621423244
41+
015225326665656474355776784465595855559969779697867888768786867958987787865555557547456236256414443
42+
342141412264436237555367786748757775996887869887989697887868985969768974878458377746343563334112432
43+
245424112423634535653674854786759556887899698796699686769888766789698865458448564436356633653255423
44+
152344536556354376777665885575687899789896978768778768999866666875968688488776547446465633626145213
45+
143323326635362775675356868684997875878998968799998887969968886965597768474646637337765355426415522
46+
233135153446345733457544575578758698668698796778899989988686677699869766655775463667634536224515421
47+
442322432622254677457747586645559867668696769899897879878696967979757585566766656433333444234655425
48+
211425433422356366455578477849687667769687699987878799877668866765868769745448755533566453422341433
49+
142142345456557463367644747779979677698996689878999779799897968888567959674764537364645446523214543
50+
112243356444435356743678858455568578689876999787888879899876878888997787884868645546363254345642533
51+
341213364343435537345688745769797686679769679778778788899769986867599798667874534334444664434313333
52+
243532164222666764774486748579698698876689888987878887998777687869755585776544863447444553624622141
53+
341332522665223377644645444775976687977778897889989999977786997965895889866888467355775645335253141
54+
451243166652422747755745765448975967698978698799997898999688997896678767488644763664752333224424553
55+
343541133453346465373775878855568559988786799997787988999967996859859695684886756457573444332411142
56+
314533526655524646634488455785868658876797968989787988796698776878757988467468647665474336362332241
57+
442414135564436647537445687767866699679676668888999999798976797688656765447854434566735624532545551
58+
032213154226544776557438554788789576777666889966978788867789879775988668444584736553534343622315541
59+
255412122466665773547546685885987799779967977989879687777878699876566957766566647736545453255421212
60+
211412336445563474775766856667577998798997678997686866778969879788687858487546455537653524366143412
61+
051255123462256533566636777744877655896767699778989868868796978589575976844685335654756444462141342
62+
322542554464342265336333465776645696986799889779679986879686787697697647574587563753453625343545521
63+
033123425643433264635737446877447585887867896898797976879886786698688684876443473335322466665351333
64+
133544314253462427775743366646484968555695958697698797876679667978854648774634475773653542364235421
65+
224252431244343247466566786445767556659899789679866787689999766568777856867437767535434222445521233
66+
125411234266255436753533664888768586696657765695958586799598696979557774846357447354325235425345222
67+
321143533344454465635634634548656884666775656786776897656977675678544868467454563642465352614111534
68+
324314254344466535257774447774685768796778758589556968777657876755454678874457376763662322355442241
69+
120211113135325666447367553357474668876977665968588879588965668457446445756555755344632353524213111
70+
311224554131565262327577435344586647744599595585679959557779868848845756333753447342232421323535422
71+
114341442141636522627743367646645776654857585789589599759987575646778664434443566622565323551144104
72+
210244242413152456645637357357684777785854766967688795794848665476646666547365474664225335235523134
73+
124135421531242254525555475654745664788867846468845666746887888854654564764543562353564341341354241
74+
232040414531142663343623666666355755588686458756864548766766867588477574653643266543552344453423142
75+
241410112355426236262654356375777777866756675768668766578744448586537335565654443353226124123350230
76+
140311131221515422466665656675435767464774857745654575455565568556777765663432626225661213413111223
77+
110443321215553263625546467777476774558756864846764586756488688436634346356345662654634352234124332
78+
221343332131351442346665233764775367674745555446558654675758467463467434543336344234234541352014440
79+
133230443252342342426444235644376747467676654877885765875675433747574653525324245321411114352213140
80+
040240221445511125623332465444547333774437463744467533446475535636346363436524235541352245340433401
81+
204040003033154341135523356453644337774767676777576456764567576775643742526424535545245441222304411
82+
304042224012435531346652244635426465733436576763444573475575775466734463356433425322423121010012441
83+
020411330415234333353346655465454535766463345353547737546577344756646645346366361232312212001341120
84+
222312310221442455555216363452342336634374577563373335557775555536643536565552525532355153044340022
85+
321033011221232232531512333546323244633765663543646734377575566264364252534433124442412113401004401
86+
221333321104001131445331226542565353624544444743335737563556662324243523642455442214142202003030231
87+
031021212444432454341232542543223433534244654624443452522654546656624352422154453544520212142411231
88+
000032101021330433425441534355232524355526522443326623545256546525633243254355513511514204131400330
89+
010223333430411314211115534412532344324626322226423546334526362632342264344512422433334011314221121
90+
030012220233010021431154541112223663556346352526365422265452334332256431412154223121433241032331112
91+
221110003130033344202155154114455435426433642224264344325523445244211424335321121000334310001120112
92+
010013223234422000001143241135132415264644343446443425245345324221223223212325552323312333412330222
93+
032103213130121140030443255125332243343336336555656433255235254313433424224542501100302101221320301
94+
112201332000400003333114425525244134115212434522644332332513434523545134353120230443131202022212112
95+
212133330330243143424430334431513443235234452344525122433141135525522345433024123302104122132331020
96+
110220302112033431311324214242235434551543155442332335242312153314452535320343340301332123320021320
97+
121223310122302143024320402120433335414432343211234251415554145543214421134023034143302200002123012
98+
221212001102321134443014113311424542341431224511531255544142141422243112400414323421322323011122100
99+
102101010031312320301133133420124425352551441553552411331314122515423420100020312041230123102220212

src/bin/day8.rs

+159
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
use std::{
2+
fs::File,
3+
io::{BufRead, BufReader},
4+
ops::{Index, IndexMut},
5+
};
6+
7+
#[derive(Debug, Clone, Copy, Default)]
8+
struct Tree {
9+
height: usize,
10+
visible: bool,
11+
}
12+
13+
const W: usize = 99;
14+
const H: usize = 99;
15+
16+
struct Grid<T> {
17+
cells: [T; W * H],
18+
}
19+
20+
impl<T> Index<usize> for Grid<T> {
21+
type Output = T;
22+
23+
fn index(&self, index: usize) -> &Self::Output {
24+
&self.cells[index]
25+
}
26+
}
27+
28+
impl<T> IndexMut<usize> for Grid<T> {
29+
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
30+
&mut self.cells[index]
31+
}
32+
}
33+
34+
impl<T> Index<(usize, usize)> for Grid<T> {
35+
type Output = T;
36+
37+
fn index(&self, index: (usize, usize)) -> &Self::Output {
38+
let (i, j) = index;
39+
&self.cells[i * W + j]
40+
}
41+
}
42+
43+
impl<T> IndexMut<(usize, usize)> for Grid<T> {
44+
fn index_mut(&mut self, index: (usize, usize)) -> &mut Self::Output {
45+
let (i, j) = index;
46+
&mut self.cells[i * W + j]
47+
}
48+
}
49+
50+
fn main() {
51+
let f = File::open("input/day8.txt").unwrap();
52+
let read = BufReader::new(f);
53+
let lines = read.lines();
54+
55+
let mut grid: Grid<Tree> = Grid {
56+
cells: [Tree::default(); W * H],
57+
};
58+
59+
// read data
60+
for (i, line) in lines.enumerate() {
61+
for (j, c) in line.unwrap().chars().enumerate() {
62+
let tree = &mut grid[(i, j)];
63+
tree.height = c
64+
.to_digit(10)
65+
.expect("not a digit")
66+
.try_into()
67+
.expect("cannot convert");
68+
}
69+
}
70+
print_grid(&grid);
71+
println!();
72+
73+
// check from left
74+
for i in 0..H {
75+
check_visibility(&mut grid, i*W..(i+1)*W);
76+
}
77+
// check from right
78+
for i in 0..H {
79+
check_visibility(&mut grid, (i*W..(i+1)*W).rev());
80+
}
81+
// check from up
82+
for j in 0..W {
83+
check_visibility(&mut grid, (j..j+(H*W)).step_by(W));
84+
}
85+
// check from down
86+
for j in 0..W {
87+
check_visibility(&mut grid, (j..j+(H*W)).step_by(W).rev());
88+
}
89+
90+
print_grid(&grid);
91+
println!();
92+
93+
let visible_count: usize = grid.cells.iter().filter(|t| t.visible).count();
94+
95+
// Scenic score
96+
let best_score = (0..H*W)
97+
.map(|pos| scenic_score(&grid, pos))
98+
.max()
99+
.unwrap_or(0);
100+
101+
println!("{}", visible_count);
102+
println!("{}", best_score);
103+
}
104+
105+
fn check_visibility<I: Iterator<Item = usize>>(grid: &mut Grid<Tree>, indexes: I) {
106+
indexes
107+
.fold(None, |acc, i| {
108+
let tree = &mut grid[i];
109+
tree.visible |= match acc {
110+
None => true,
111+
Some(max_height) => tree.height > max_height
112+
};
113+
match acc {
114+
None => Some(tree.height),
115+
Some(max_height) => Some(max_height.max(tree.height))
116+
}
117+
});
118+
}
119+
120+
fn scenic_score(grid: &Grid<Tree>, pos: usize) -> usize {
121+
let i = pos / W;
122+
let j = pos % W;
123+
let mut score: usize = 1;
124+
let height = grid[(i,j)].height;
125+
// check to right
126+
score *= view_distance(grid, height, pos+1..(i+1)*W);
127+
// check to left
128+
score *= view_distance(grid, height, (i*W..pos).rev());
129+
// check down
130+
score *= view_distance(grid, height, (pos+W..(j+H*W)).step_by(W));
131+
// check up
132+
score *= view_distance(grid, height, (j..pos).step_by(W).rev());
133+
score
134+
}
135+
136+
fn view_distance<I: Iterator<Item = usize>>(grid: &Grid<Tree>, height: usize, indexes: I) -> usize {
137+
let mut dist: usize = 0;
138+
for i in indexes {
139+
dist += 1;
140+
if height <= grid[i].height {
141+
break;
142+
}
143+
}
144+
dist
145+
}
146+
147+
fn print_grid(grid: &Grid<Tree>) {
148+
for i in 0..H {
149+
for j in 0..W {
150+
let tree = &grid[(i,j)];
151+
if tree.visible {
152+
print!("\u{001b}[31m{}\u{001b}[0m", tree.height);
153+
} else {
154+
print!("{}", tree.height);
155+
}
156+
}
157+
println!();
158+
}
159+
}

0 commit comments

Comments
 (0)