From fbbb192cccb7de312e16dc3ee470d20e98609f23 Mon Sep 17 00:00:00 2001 From: missing Date: Thu, 8 Dec 2022 12:43:32 -0600 Subject: [PATCH] day 8 --- src/days/day8.rs | 66 ++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + 2 files changed, 67 insertions(+) create mode 100644 src/days/day8.rs diff --git a/src/days/day8.rs b/src/days/day8.rs new file mode 100644 index 0000000..3f047b4 --- /dev/null +++ b/src/days/day8.rs @@ -0,0 +1,66 @@ +use aoc_runner_derive::{aoc, aoc_generator}; + +#[aoc_generator(day8)] +fn generator(input: &str) -> Vec> { + input + .lines() + .map(|l| l.trim().chars().map(|v| v.to_digit(10).unwrap()).collect()) + .collect() +} + +#[aoc(day8, part1)] +fn part1_indexing(input: &[Vec]) -> u32 { + let mut count = 0; + + for i in 0..input.len() { + for j in 0..input[i].len() { + let v = input[i][j]; + + if input[i][..j].iter().all(|&v2| v2 < v) + || input[i][j + 1..].iter().all(|&v2| v2 < v) + || input[..i].iter().map(|v| v[j]).all(|v2| v2 < v) + || input[i + 1..].iter().map(|v| v[j]).all(|v2| v2 < v) + { + count += 1; + } + } + } + + count +} + +fn take_until_inclusive(v: I, mut f: impl FnMut(&I::Item) -> bool) -> impl Iterator { + let mut should_stop = false; + v.take_while(move |item| { + if should_stop { + false + } else { + should_stop = f(item); + true + } + }) +} + +#[aoc(day8, part2)] +fn part2(input: &[Vec]) -> usize { + let mut highest = 0; + + for i in 0..input.len() { + for j in 0..input[i].len() { + let v = input[i][j]; + + let left = take_until_inclusive(input[i][..j].iter().rev(), |&&v2| v2 >= v).count(); + let right = take_until_inclusive(input[i][j + 1..].iter(), |&&v2| v2 >= v).count(); + let up = + take_until_inclusive(input[..i].iter().rev().map(|v| v[j]), |&v2| v2 >= v).count(); + let down = + take_until_inclusive(input[i + 1..].iter().map(|v| v[j]), |&v2| v2 >= v).count(); + + let scenic_score = left * right * up * down; + + highest = highest.max(scenic_score); + } + } + + highest +} diff --git a/src/lib.rs b/src/lib.rs index 6a87c37..a6670ac 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,6 +8,7 @@ mod days { mod day5; mod day6; mod day7; + mod day8; } aoc_lib! { year = 2022 }