summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorReiner Herrmann <reiner@reiner-h.de>2022-12-09 01:01:44 +0100
committerReiner Herrmann <reiner@reiner-h.de>2022-12-09 01:01:44 +0100
commitd509a258e2170ba59f80ac629a66851a3c0d58a5 (patch)
tree7f85b46ed0fa8148445cbb282b6d6bb33f079788
parenta942a8008789dfc647ec253a9c9b8f317791257b (diff)
day8
-rw-r--r--inputs/day899
-rw-r--r--src/bin/day8.rs163
2 files changed, 262 insertions, 0 deletions
diff --git a/inputs/day8 b/inputs/day8
new file mode 100644
index 0000000..c400166
--- /dev/null
+++ b/inputs/day8
@@ -0,0 +1,99 @@
+020110220332333020110144320304042020444223003535441353331002333431100300241023221210123003331021020
+002120010112022233203323334422340102033151553341235324543343233301202102130210343113312320222102020
+000000000301021002423224442341031145215244543444223314545524515335404222310244314423023331200102012
+011200302010212334422213334343335452314532352111122533515334412241512412033340322004232020212310011
+110111200102310120411101020333232255345143342245212323552344535253214434143032323213200301101020121
+122001020113011314043121413025543341512335111452355331355351515421515411500421204113413212021112302
+011210211003201420103123304513544114145311452334355144111521441452154223512022101102111221311323311
+011030200312240341224434214241313322323544154242345656634321531531424124544551344224331443202111303
+031023120224010220144244342511241334331166432443322363352643453525244443222142533014331212133113031
+331033331100013013431114214253543323143222523423236646456363626622312534521454454123324132323233200
+313320023430241044213221132131122135436564345625426422255326234363243521151144414344130023210131130
+002203011410101343345413454143416333665335233566235623536644652644522345535525153521230411343303211
+022030323311323124542521154345356626352343324652424464566645425223224521453553252234321033403323130
+331222243010032043452454232565665256426536435254566436633323343652534544214412242551144312310221231
+231133233004211422411531255443363333555345434532475762623642433466363266634413551244254134333243030
+020231113200002214145443133533534653326664654474363645366343345466265644554325342415122332200024311
+311032340032414323451542554464466352256347635357637447736577667463426532442564245132242343241314212
+210342413212232542421452232234546643373633347344743377376553465333423522425436534352355150302110320
+012302042023135125251364365232626547463576733637455663764376436734352545425434331543241124042312202
+304042410233125532432523224245663443763753734647533535565636563665334542556665554245524241400402122
+100324144335411111343624365526556753335435457754653754663647544353454443256623466532421524442220212
+004310002315421423634565264237567445665436743333467647547754434765353667542365233361541321530411122
+340100133133254351662534562377644656735477678648447854646765556636735646456562322345353223545130204
+223422234555541234336653556365464346366386775667444744567758637737757667635354324626442355153142402
+301202352352311332364654446567354376644665887886878854847858588356564773766244445222614543212440041
+121010254322255546522625235356663453785775455846758588786788567566653645763532436352663334521520342
+223123122512444365553626454767563488464886467858877655766685458675437556363673526236645213411512224
+003314511142456453664565476333555654845565878476575468776548666885476777366354622652365442341232330
+100434351354142623236353735657745766665557455465675844665578767675874476556574642555262344222545124
+310035122251134332246455666547787878577677667587668567788744875844888844364453536232254344234442004
+332355524421462334544344655646458867688886798865795569865997666688465755335745555656646243433122421
+212252432545222565265377376368574784887787696558698987968675547855656555445333366624445325141553342
+113535121253534442546646364347455675758878676575695576785998796454646647555637657565266546254424513
+023531155256556443573534665476784478579889966685965558767695667786588646763565366352565623135242132
+332235311465633455637577656876574774599858977857599877979855878694546488485435736564352255345534352
+423312331346353345545646578646466549968669789766955768696578957665488458568376747735452426221223133
+042131555255526335474354376567744577697576655686887997959968659596857774864465577464334245541411144
+314335533542324633457667677854784879668896688676989899996875778858675674784535777333543532331142411
+342431321343652634443346854886679789759856968779866777899677797959996774647753365556465426225312415
+443122331352464374355445874586585857875568898788666678776787986756859447544446666763365552244452322
+434332122456323647344556476668759666569568769987679766689798887678765548587477474536465533643451422
+012515214225433447464677777487776597697976998888987789798887796967785955887765466437333665342324523
+151212435544622335634548655777875966867886867777679699887698988956977857868655457465635544364542154
+425332155643342656676678888575989888788767888899689899798688796666757955465858667777362363434424542
+531532414223243445466348848575895777968779899997887798776667989655588765447757447347342252524115215
+155245334655555376575664568856876978978787888977789987998877778796686666588544843473375333466131323
+442223263655353573545578848875879796997899897789887987979686977788898887848487563664644353624212355
+532342436423523433743374566649565796978689677778887988977986779775985585775684847364464225436344143
+144131162643455446377457566657777998676869667898877798979888978868889578745676553337663625625354412
+212531125562654464546354657756555595879978788779777999979988869895856856488587466676673534564554215
+515521136425625643733788668547967896969879989899779977888986996776697776777887634766746536654352314
+533115456346557773554457648785576555967877979777897997977979879979889786544548474557775226255314122
+432554142445363766476368787846679896977899887779797978979969889696765986666844737665565256236231155
+125253442625345476646468778468579567787976888987879797899978977695756855477657433457362446432521533
+451335344244435635354545558745856999567779868887799878899867977959975677866558447464563253543641554
+412221312464362643575387865576575778679997969899879799897898997777656968646646747476434353234412443
+132123436534443354553586484485777568678779897878779989966877968659558598445764456573762542646254544
+024143156436353736354736885476858987768978898698789976987668878886568974574565737457544455543452243
+413135313643263775675578866878568599957868689889887898688888897699957964455656556464353643525222353
+151155234625524455454576866886566769799886776697778986968676996687578554546778475347663665445441542
+225335432336665664467554464786765996688599897886876679899878795577796866847588643664352344525425543
+231314414363454365656445447648785668665699968677877867679686575989979554888874435546345632362455211
+215225154624264466667536475884866766856799777898688869799678789658958655666655664763723652345235254
+035354151466424636347656474544888889966575676978876877797665689589954588444647355553546242434541533
+001253252266442433343367457848868577696576876956987767987878766766685545864636355333223636351531525
+432114153244234344474473336455665467595685659677769775776598955788485844764764774633253355345153344
+440231335123622442356777766866685464699576999777998779679677676798556674878735743562224245652132511
+321533313113424533355576556587855784598797699998756887876998858868457756543476743664332364153212141
+003415211213442534357575744467674686668987887956977567898997789587764784753377657636326255155213240
+224445525333436434526743474634674544887785577959758998579798555487687877644644777665225554533252422
+114025431113445462556535777346657775747764689775588768859877487468477864653336547436625221413353402
+444431445545324552322733365535555774656466544687955795855665775645668663457547655566226341423452434
+410445115125126462226236353755455888548674587788447578688475766447586446763443644432535242233435244
+124340412535436432636663557555763744747854587474866557668648548584743443577534342355356535325312231
+200313132541126432532542747437536486654767558545587875558487658465577773437664544545354413212211212
+101333454545223643623655465756576356785656755457885765456674785763465474656725224222541315314114031
+131233142323341366245232435554365765545574774767785475776848668453355554474332533326544433554514201
+321411402434455156645353526473756763444567846554465464446465455445745336435566265254512531525210112
+030131034355454454644464565464475577344753366858566476585577545346763733342656446563454444451042420
+123233024345514531322525463344645673755773347575544437475656346736346777342352464432215452454013411
+014322323421235432556233655455544546633674634474456664346664375533773475562242423541545331424443423
+103044012005453352356343445634325645434337637364655565435774676736733553262634342435541343244030000
+320224321434222311444445623462256346445463567453434633465443337646352226626622452524224345304403330
+212420324020443535541356556336654222753455556557453353546474577342535554563532343321131244211234443
+122201202000423112251512562424625443344753367744337643545467743645523543344535431154212330403302410
+302200224224202341324112146243633653523426575646533673677763345633345645554351214352331400423031333
+123001320330302415435532414224224226352235334353577574436324344226425624263133551342153034141032032
+213131040401103044255424155325562642646633364323565455323646566426224245451451423431324433130343033
+231211142003310021532152553311226325626362465244625533422332633634454451545253314415220400420413203
+312320130422410131322514243524545532465435666446335525623552365342455442353243151122403024303312021
+133332233112042420435241253424143243654366264342464546256332442465235435334432132240241304411322221
+333302213333240222002452421142454145262355663455246362444253326353144344554554324413300122011222021
+202310030210410440001115535553444131333333663342464256255352255253344353414423520402213424030101331
+020120023231001430421410113431542415212332151336254445313532121145524443121220343020012020230032230
+210023323121322121303302233233245321224455333332343322323215223451525532222223304043001030230132022
+002222021003231011234340303045243412535411232132142332533521354111141433134004443142111110122121100
+022122131333022231003311233440122445251415343142141523345254553214425533033042143220401300010213100
+122110132221332212341431023220242412121544355542132225542445544553234342030402442141111321103231211
+122021200131211021314010312001241211235253115315252213451111223452204220130001212341202130012001221
diff --git a/src/bin/day8.rs b/src/bin/day8.rs
new file mode 100644
index 0000000..e71bf0f
--- /dev/null
+++ b/src/bin/day8.rs
@@ -0,0 +1,163 @@
+use std::collections::HashMap;
+
+static DAY: u8 = 8;
+
+fn main() {
+ let input = advent::read_lines(DAY);
+ println!("{DAY}a: {}", visible_trees(&input));
+ println!("{DAY}b: {}", highest_scenic_score(&input));
+}
+
+#[derive(Default, Debug)]
+struct NeighborVisibility {
+ up: usize,
+ down: usize,
+ left: usize,
+ right: usize,
+}
+
+fn visible_trees(input: &[String]) -> usize {
+ let mut map = Vec::new();
+ for line in input {
+ let heights = line.chars().map(|x| x.to_digit(10).unwrap() as usize).collect::<Vec<_>>();
+ map.push(heights);
+ }
+
+ let mut visibilities = HashMap::<_,NeighborVisibility>::new();
+
+ /* visibilities from top to bottom */
+ for x in 1 .. map[0].len() - 1 {
+ let mut current_vis = map[0][x];
+ #[allow(clippy::needless_range_loop)]
+ for y in 1 .. map.len() - 1 {
+ let vis_entry = visibilities.entry((x,y)).or_default();
+ vis_entry.up = current_vis;
+ current_vis = current_vis.max(map[y][x]);
+ }
+ }
+ /* visibilities from bottom to top */
+ for x in 1 .. map[0].len() - 1 {
+ let mut current_vis = map[map.len()-1][x];
+ for y in (1 .. map.len() - 1).rev() {
+ let vis_entry = visibilities.entry((x,y)).or_default();
+ vis_entry.down = current_vis;
+ current_vis = current_vis.max(map[y][x]);
+ }
+ }
+ /* visibilities from left to right */
+ for y in 1 .. map.len() - 1 {
+ let mut current_vis = map[y][0];
+ for x in 1 .. map[0].len() - 1 {
+ let vis_entry = visibilities.entry((x,y)).or_default();
+ vis_entry.left = current_vis;
+ current_vis = current_vis.max(map[y][x]);
+ }
+ }
+ /* visibilities from right to left */
+ for y in 1 .. map.len() - 1 {
+ let mut current_vis = map[y][map[0].len()-1];
+ for x in (1 .. map[0].len() - 1).rev() {
+ let vis_entry = visibilities.entry((x,y)).or_default();
+ vis_entry.right = current_vis;
+ current_vis = current_vis.max(map[y][x]);
+ }
+ }
+
+ let mut total_visible = 2 * (map.len() - 1) + 2 * (map[0].len() - 1);
+ for (pos, visibility) in visibilities {
+ let height = map[pos.1][pos.0];
+ if height > visibility.up || height > visibility.down || height > visibility.left || height > visibility.right {
+ total_visible += 1;
+ }
+ }
+ total_visible
+}
+
+fn highest_scenic_score(input: &[String]) -> usize {
+ let mut map = Vec::new();
+ for line in input {
+ let heights = line.chars().map(|x| x.to_digit(10).unwrap() as usize).collect::<Vec<_>>();
+ map.push(heights);
+ }
+
+ let mut visibilities = HashMap::<_,NeighborVisibility>::new();
+
+ let update_range_vector = |range: &mut [usize; 10], height: usize|{
+ /* all heights less than the specified one will start with a
+ new visibility of one; for greater heights the distance
+ increases by 1. */
+ for (i, val) in range.iter_mut().enumerate() {
+ if i <= height as usize {
+ *val = 1;
+ } else {
+ *val += 1;
+ }
+ }
+ };
+
+ /* visibilities from top to bottom */
+ for x in 0 .. map[0].len() {
+ let mut ranges = [0; 10];
+ #[allow(clippy::needless_range_loop)]
+ for y in 0 .. map.len() {
+ let height = map[y][x];
+ let vis_entry = visibilities.entry((x,y)).or_default();
+ vis_entry.up = ranges[height];
+ update_range_vector(&mut ranges, height);
+ }
+ }
+ /* visibilities from bottom to top */
+ for x in 0 .. map[0].len() {
+ let mut ranges = [0; 10];
+ for y in (0 .. map.len()).rev() {
+ let height = map[y][x];
+ let vis_entry = visibilities.entry((x,y)).or_default();
+ vis_entry.down = ranges[height];
+ update_range_vector(&mut ranges, height);
+ }
+ }
+ /* visibilities from left to right */
+ for y in 0 .. map.len() {
+ let mut ranges = [0; 10];
+ for x in 0 .. map[0].len() {
+ let height = map[y][x];
+ let vis_entry = visibilities.entry((x,y)).or_default();
+ vis_entry.left = ranges[height];
+ update_range_vector(&mut ranges, height);
+ }
+ }
+ /* visibilities from right to left */
+ for y in 0 .. map.len() {
+ let mut ranges = [0; 10];
+ for x in (0 .. map[0].len()).rev() {
+ let height = map[y][x];
+ let vis_entry = visibilities.entry((x,y)).or_default();
+ vis_entry.right = ranges[height];
+ update_range_vector(&mut ranges, height);
+ }
+ }
+
+ visibilities.values()
+ .map(|v| v.up * v.down * v.left * v.right)
+ .max()
+ .unwrap()
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test() {
+ let input = [
+ "30373",
+ "25512",
+ "65332",
+ "33549",
+ "35390",
+ ].iter().map(|&x| String::from(x)).collect::<Vec<_>>();
+
+ assert_eq!(visible_trees(&input), 21);
+ assert_eq!(highest_scenic_score(&input), 8);
+ }
+}