67 lines
1.8 KiB
Rust
67 lines
1.8 KiB
Rust
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
|
|
}
|