From ed0c44e2b1864c4c24543bb41a83095c20c373ce Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Thu, 8 Dec 2022 15:44:10 -0800 Subject: [PATCH] done with day8, part1 --- 2022-aoc/Cargo.toml | 1 + 2022-aoc/src/d8.rs | 81 +++++++++++++++++++++++++++++++++++++++++++++ 2022-aoc/src/lib.rs | 1 + 3 files changed, 83 insertions(+) create mode 100644 2022-aoc/src/d8.rs diff --git a/2022-aoc/Cargo.toml b/2022-aoc/Cargo.toml index 0ff3b28..53aaf75 100644 --- a/2022-aoc/Cargo.toml +++ b/2022-aoc/Cargo.toml @@ -11,3 +11,4 @@ aoc-runner-derive = "0.3" lazy_static = "1.4.0" #itertools = "0.10.5" regex = "1.7" +ndarray = "0.15" \ No newline at end of file diff --git a/2022-aoc/src/d8.rs b/2022-aoc/src/d8.rs new file mode 100644 index 0000000..a54f677 --- /dev/null +++ b/2022-aoc/src/d8.rs @@ -0,0 +1,81 @@ +use std::collections::HashSet; + +use aoc_runner_derive::{aoc as aoc_run, aoc_generator}; + +use ndarray::prelude::*; + +type Forest = Array2; +type Tree = (i8, i8); + +#[aoc_generator(day8)] +fn parse_input_day1(input: &str) -> Forest { + let nrows = input.lines().map(|_| ()).collect::>().len(); + let ncols = input.lines().next().unwrap().len(); + let shape = (nrows, ncols); + let mut arr = Array2::zeros(shape); + for (row, line) in input.lines().enumerate() { + for (col, tree) in line.chars().enumerate() { + arr[[row, col]] = tree.to_digit(10).unwrap() as i8; + } + } + arr +} + +#[aoc_run(day8, part1)] +fn part1(input: &Forest) -> usize { + let mut viz = HashSet::new(); + for (ridx, row) in input.rows().into_iter().enumerate() { + let mut max = -1; + for (cidx, &tree) in row.as_slice().unwrap().iter().enumerate() { + if tree > max { + let addr = (ridx, cidx); + viz.insert(addr); + max = tree; + } + } + max = -1; + for (cidx, &tree) in row.as_slice().unwrap().iter().enumerate().rev() { + if tree > max { + viz.insert((ridx, cidx)); + max = tree; + } + } + } + + for (cidx, col) in input.columns().into_iter().enumerate() { + let mut max = -1; + for (ridx, &tree) in col + .as_standard_layout() + .as_slice() + .unwrap() + .iter() + .enumerate() + { + if tree > max { + viz.insert((ridx, cidx)); + max = tree; + } + } + max = -1; + for (ridx, &tree) in col + .as_standard_layout() + .as_slice() + .unwrap() + .iter() + .enumerate() + .rev() + { + if tree > max { + viz.insert((ridx, cidx)); + max = tree; + } + } + } + + viz.len() +} + +#[aoc_run(day8, part2)] +fn part2(input: &Forest) -> usize { + 0 +} diff --git a/2022-aoc/src/lib.rs b/2022-aoc/src/lib.rs index c1965df..c120763 100644 --- a/2022-aoc/src/lib.rs +++ b/2022-aoc/src/lib.rs @@ -7,5 +7,6 @@ mod d4; mod d5; mod d6; mod d7; +mod d8; aoc_lib! { year = 2022 }