2022/day8: part 1 + add utils

This commit is contained in:
ktims committed 2026-12-03 17:18:31 -07:00
1 parent 3ca8ab82c8
commit 96ac7d1c01
13 files changed
+1392 -3

No files matched your search

+77
View File
@@ -0,0 +1,77 @@
use aoc_runner_derive::{aoc, aoc_generator};
use std::str::FromStr;
use grid::{Coord2d, Grid};
#[aoc_generator(day8)]
fn parse(input: &str) -> Grid<u8> {
Grid::from_str(input).unwrap()
}
#[aoc(day8, part1)]
fn part1(input: &Grid<u8>) -> u64 {
let mut visible_map = input.same_shape(false);
for y in 0..input.height() {
let mut left_max = 0u8;
let mut right_max = 0u8;
for x in 0..input.width() {
let height_left = *input.get(&(x as i64, y as i64)).unwrap();
if height_left > left_max {
left_max = height_left;
visible_map.set(&(x as i64, y as i64), true);
}
let height_right = *input.get(&(input.width() as i64 - 1 - x as i64, y as i64)).unwrap();
if height_right > right_max {
right_max = height_right;
visible_map.set(&(input.width() as i64 - 1 - x as i64, y as i64), true);
}
}
}
for x in 0..input.width() {
let mut top_max = 0u8;
let mut bottom_max = 0u8;
for y in 0..input.height() {
let height_top = *input.get(&(x as i64, y as i64)).unwrap();
if height_top > top_max {
top_max = height_top;
visible_map.set(&(x as i64, y as i64), true);
}
let height_bottom = *input.get(&(x as i64, input.height() as i64 - 1 - y as i64)).unwrap();
if height_bottom > bottom_max {
bottom_max = height_bottom;
visible_map.set(&(x as i64, input.height() as i64 - 1 - y as i64), true);
}
}
}
visible_map.count(&true) as u64
}
#[aoc(day8, part2)]
fn part2(input: &Grid<u8>) -> u64 {
todo!()
}
#[cfg(test)]
mod tests {
use super::*;
use rstest::rstest;
const EXAMPLE: &str = "30373
25512
65332
33549
35390";
#[rstest]
#[case(EXAMPLE, 21)]
fn part1_example(#[case] input: &str, #[case] expected: u64) {
assert_eq!(part1(&parse(input)), expected);
}
#[rstest]
#[case(EXAMPLE, 0)]
fn part2_example(#[case] input: &str, #[case] expected: u64) {
assert_eq!(part2(&parse(input)), expected);
}
}
+1
View File
@@ -4,6 +4,7 @@ mod day3;
mod day4;
mod day5;
mod day7;
mod day8;
use aoc_runner_derive::aoc_lib;
mod day6;