From ae55b29ff42edaa9b90a72321b3432acf03c5c8e Mon Sep 17 00:00:00 2001 From: David Ashby Date: Thu, 8 Dec 2022 19:47:08 -0500 Subject: [PATCH] day8part1 done... part 2 might require another approach --- 08/main.rs | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 08/main.rs diff --git a/08/main.rs b/08/main.rs new file mode 100644 index 0000000..9eb5fb4 --- /dev/null +++ b/08/main.rs @@ -0,0 +1,88 @@ +#![allow(dead_code)] +use std::fs::File; +use std::io; +use std::io::BufRead; +use std::path::Path; +use std::time::Instant; + +#[derive(Debug, Clone)] +struct Tree(i8, bool); + +fn main() { + let now = Instant::now(); + part_one(); + let part_one_duration = now.elapsed(); + part_two(); + let part_two_duration = now.elapsed(); + println!( + "p1: {}ms, p2: {}ms", + part_one_duration.as_millis(), + (part_two_duration - part_one_duration).as_millis() + ); +} + +fn read_lines

(filename: P) -> io::Result>> +where + P: AsRef, +{ + // note that this discards a final newline + let file = File::open(filename)?; + Ok(io::BufReader::new(file).lines()) +} + +fn part_one() { + let mut grid: Vec> = vec![]; + let mut total = 0; + if let Ok(lines) = read_lines("./inputs/input") { + for (i, line) in lines.enumerate() { + if let Ok(row) = line { + grid.push(vec![]); + let mut max = -1; + for c in row.chars() { + if (c as i8) - 48 > max { + total += 1; + max = (c as i8) - 48; + grid[i].push(Tree((c as i8) - 48, true)); + } else { + grid[i].push(Tree((c as i8) - 48, false)); + } + } + max = -1; + let invert = grid[i].len() - 1; + for (j, t) in grid[i].clone().iter().rev().enumerate() { + if t.0 > max { + if !grid[i][invert - j].1 { + total += 1; + } + max = t.0; + grid[i][invert - j] = Tree(t.0, true); + } + } + } + } + } + let invert = grid.len() - 1; + for i in 0..grid.len() { + let mut in_max = -1; + let mut out_max = -1; + for j in 0..grid.len() { + if grid[j][i].0 > in_max { + if !grid[j][i].1 { + total += 1; + } + in_max = grid[j][i].0; + grid[j][i] = Tree(grid[j][i].0, true); + } + if grid[invert - j][i].0 > out_max { + if !grid[invert - j][i].1 { + total += 1; + } + out_max = grid[invert - j][i].0; + grid[invert - j][i] = Tree(grid[invert - j][i].0, true); + } + } + } + println!("{}", total); +} + +fn part_two() {}