summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/day8.rs56
-rw-r--r--src/main.rs2
2 files changed, 58 insertions, 0 deletions
diff --git a/src/day8.rs b/src/day8.rs
new file mode 100644
index 0000000..e69d5cd
--- /dev/null
+++ b/src/day8.rs
@@ -0,0 +1,56 @@
+fn ss(r: Vec<u32>, t: u32) -> u32 {
+ let mut i: usize = 0;
+ for x in r {
+ if x >= t {
+ i+=1; // If we're blocked, count the blocking tree as visible!
+ break;
+ }
+ i+=1;
+ }
+ i.try_into().unwrap()
+}
+
+// fn visible(r: Vec<u32>, t: u32) -> bool {
+// r.into_iter().all(|tt| { tt < t })
+// }
+
+pub fn run(input: String) {
+ let grid: Vec<Vec<u32>> = input.lines().map(|line| {
+ line.chars().map(|ch| {
+ ch.to_digit(10).expect("char wasn't a digit!")
+ }).collect()
+ }).collect();
+ let height = grid.len();
+ let width = grid[0].len();
+
+ // Naïve solution, just check every tree's path's to the edge
+ // let visibilities: Vec<Vec<bool>> =
+ let viewingscores: Vec<Vec<u32>> =
+ (0..height).map(|i| {
+ (0..width).map(|j| {
+ if i == 0 || j == 0 || i == height-1 || j == width-1 {
+ // println!("short circuit {} {}",i, j);
+ //return true;
+ return 0;
+ }
+ let t = grid[i][j];
+ let r1: Vec<u32> = (0..i).rev().map(|ii| {grid[ii][j]} ).collect();
+ let r2: Vec<u32> = (i+1..height).map(|ii| {grid[ii][j]} ).collect();
+ let r3: Vec<u32> = (0..j).rev().map(|jj| {grid[i][jj]} ).collect();
+ let r4: Vec<u32> = (j+1..width).map(|jj| {grid[i][jj]}).collect();
+ // let visible = [r1, r2, r3, r4].into_iter().any(|r| {
+ // visible(r, t)
+ // });
+ // visible
+ // Always see at least 1 tree in any direction
+ [r1, r2, r3, r4].into_iter().map(|r| { ss(r, t) } ).product()
+ }).collect()
+ }).collect();
+ // println!("{:?}", grid);
+ // println!("{:?}", visibilities);
+
+ // let nvisible: u32 = visibilities.into_iter().flatten().into_iter().map(|b| {if b {1} else {0}}).sum();
+ // println!("Day 8: {}", nvisible);
+ let maxvs = viewingscores.into_iter().flatten().max().unwrap();
+ println!("Day 8: {}", maxvs);
+} \ No newline at end of file
diff --git a/src/main.rs b/src/main.rs
index b370693..5a7634b 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -7,6 +7,7 @@ mod day4;
mod day5;
mod day6;
mod day7;
+mod day8;
fn main() {
day1::run(fs::read_to_string("input/day1.txt").expect("Failed to read input file!"));
@@ -16,4 +17,5 @@ fn main() {
day5::run(fs::read_to_string("input/day5.txt").expect("Failed to read input file!"));
day6::run(fs::read_to_string("input/day6.txt").expect("Failed to read input file!"));
day7::run(fs::read_to_string("input/day7.txt").expect("Failed to read input file!"));
+ day8::run(fs::read_to_string("input/day8.txt").expect("Failed to read input file!"));
}