This commit is contained in:
missing 2022-12-08 12:43:32 -06:00
parent 933cdbfd19
commit fbbb192ccc
2 changed files with 67 additions and 0 deletions

66
src/days/day8.rs Normal file
View file

@ -0,0 +1,66 @@
use aoc_runner_derive::{aoc, aoc_generator};
#[aoc_generator(day8)]
fn generator(input: &str) -> Vec<Vec<u32>> {
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>]) -> 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<I: Iterator>(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<u32>]) -> 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
}

View file

@ -8,6 +8,7 @@ mod days {
mod day5;
mod day6;
mod day7;
mod day8;
}
aoc_lib! { year = 2022 }