Compare commits

...

2 Commits

Author SHA1 Message Date
MasterofJOKers 2ffa87d872 day08: Adapt code for part 2
I could probably optimize that by getting the iterators for all 4
directions and then to the scan on each of them in a loop, but I'm with
with this right now.
2022-12-20 23:48:03 +01:00
MasterofJOKers 30c0c2b42f day08 Add part 1
Took multiple seconds to run, so I wonder if there's a more efficient
way to do this. Maybe would be better to have a Vec<Vec<usize>> instead
of putting all lines after each other.
2022-12-20 23:16:30 +01:00
3 changed files with 107 additions and 0 deletions

7
day08/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day08"
version = "0.1.0"

8
day08/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "day08"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

92
day08/src/main.rs Normal file
View File

@ -0,0 +1,92 @@
use std::error;
use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
fn main() -> Result<(), Box<dyn error::Error>> {
let f = File::open("input.txt")?;
let reader = BufReader::new(f);
let mut map = vec![];
let mut width = 0;
for line in reader.lines() {
let line = line?;
width = line.len();
map.extend(line.chars().map(|c| c.to_digit(10)).flatten().map(|d| d as usize));
}
let width = width;
let height = map.len() / width;
let mut scenic_scores = vec![];
for i in 0..map.len() {
let line = i / height;
let column = i % width + 1; // we need + 1 here, because there is no modulo 0
let mut scenic_score = 1;
// tree-houses cannot be built on the outside, so we have to skip those outside-trees for
// the calculation
if line == 0 || line == height - 1 || column == 1 || column == width {
continue;
}
// println!("line: {}, column: {}, self: {}", line, column, map[i]);
let left = map[(line * width)..i].iter()
.rev()
.scan(true, |state, &x| if !*state { None } else { if x >= map[i] { *state = false }; Some(1) })
// .inspect(|x| println!("{x}"))
.count();
if left > 0 {
scenic_score *= left;
}
let right = map[i..((line + 1) * width)].iter()
.enumerate()
.filter_map(|(j, x)| if (j + i) / height == line && j > 0 { Some(x) } else { None })
.scan(true, |state, &x| if !*state { None } else { if x >= map[i] { *state = false }; Some(1) })
.count();
if right > 0 {
scenic_score *= right;
}
// let right = map[(i + 1)..((line + 1) * width)].iter().max();
let up = map[0..i].iter()
.enumerate()
//.inspect(|(j, x)| println!("{j} {x}"))
.filter_map(|(j, x)| if (j + 1) % width == column { Some(x) } else { None })
.rev()
.scan(true, |state, &x| if !*state { None } else { if x >= map[i] { *state = false }; Some(1) })
.count();
if up > 0 {
scenic_score *= up;
}
let down = map[i..].iter()
.enumerate()
.filter_map(|(j, x)| if (i + j + 1) % width == column && j > 0 { Some(x) } else { None })
.scan(true, |state, &x| if !*state { None } else { if x >= map[i] { *state = false }; Some(1) })
.count();
if down > 0 {
scenic_score *= down;
}
// println!("left: {:?}, right: {:?}, up: {:?}, down: {:?}", &left, &right, &up, &down);
/*
for direction_max in vec![left, right, up, down] {
match direction_max {
Some(x) if x < &map[i] => {
visible_count += 1;
break;
},
None => {
visible_count += 1;
break;
},
_ => ()
}
}
*/
scenic_scores.push(scenic_score);
}
println!("Highest scenic score is {}", scenic_scores.iter().max().unwrap_or(&0));
Ok(())
}