2022 day 8 part 2 interim

This commit is contained in:
Maciej Jur 2022-12-08 12:09:17 +01:00
parent a788a6ce27
commit 145b112dd7

View file

@ -1,3 +1,5 @@
use std::cmp::max;
use std::collections::HashMap;
use crate::utils; use crate::utils;
@ -31,11 +33,7 @@ fn create_mask(data: &[Vec<i32>]) -> Vec<Vec<bool>> {
let mut visible: Vec<Vec<bool>> = (0..rows) let mut visible: Vec<Vec<bool>> = (0..rows)
.into_iter() .into_iter()
.map(|_| (0..cols) .map(|_| (0..cols).into_iter().map(|_| false).collect())
.into_iter()
.map(|_| false)
.collect()
)
.collect(); .collect();
for row in 0..rows { for row in 0..rows {
@ -63,18 +61,74 @@ fn create_mask(data: &[Vec<i32>]) -> Vec<Vec<bool>> {
fn solve1(data: &[Vec<i32>]) -> usize { fn solve1(data: &[Vec<i32>]) -> usize {
let mask = create_mask(data); create_mask(data).into_iter().map(|row| row.iter().filter(|&e| *e).count()).sum()
}
mask.into_iter() #[inline(always)]
.map(|row| row.iter() fn find_limit(
.filter(|&e| *e) data: &[Vec<i32>],
.count() last_heights: &mut HashMap<i32, usize>,
) row: usize,
.sum() col: usize
) -> usize {
let mut limit = 0;
for h in data[row][col]..=9 {
if let Some(since) = last_heights.get(&h) {
limit = max(limit, *since);
}
};
limit
}
fn find_scores(data: &[Vec<i32>]) -> Vec<Vec<i32>> {
let rows = data.len();
let cols = data[0].len();
let mut scores: Vec<Vec<i32>> = (0..rows)
.into_iter()
.map(|_| (0..cols).into_iter().map(|_| 1).collect())
.collect();
for row in 0..rows {
let mut last_heights: HashMap<i32, usize> = HashMap::new();
for col in 0..cols {
let limit = find_limit(data, &mut last_heights, row, col);
scores[row][col] = scores[row][col] * (col - limit) as i32;
last_heights.insert(data[row][col], col);
};
let mut last_heights: HashMap<i32, usize> = HashMap::new();
for col in (0..cols).rev() {
let limit = find_limit(data, &mut last_heights, row, col);
let limit = if limit == 0 { cols - 1 } else { limit };
scores[row][col] = scores[row][col] * (limit - col) as i32;
last_heights.insert(data[row][col], col);
};
};
for col in 0..cols {
let mut last_heights: HashMap<i32, usize> = HashMap::new();
for row in 0..rows {
let limit = find_limit(data, &mut last_heights, row, col);
scores[row][col] = scores[row][col] * (row - limit) as i32;
last_heights.insert(data[row][col], row);
};
let mut last_heights: HashMap<i32, usize> = HashMap::new();
for row in (0..cols).rev() {
let limit = find_limit(data, &mut last_heights, row, col);
let limit = if limit == 0 { rows - 1 } else { limit };
scores[row][col] = scores[row][col] * (limit - row) as i32;
last_heights.insert(data[row][col], row);
};
};
scores
} }
fn solve2(data: &[Vec<i32>]) -> i32 { fn solve2(data: &[Vec<i32>]) -> i32 {
2 let scores = find_scores(data);
let reducer = |a, b| max(a, b);
scores.into_iter().map(|row| row.iter().copied().reduce(reducer).unwrap()).reduce(reducer).unwrap()
} }
@ -110,6 +164,9 @@ mod tests {
#[test] #[test]
fn part2() { fn part2() {
let data = parse_data(DATA); let data = parse_data(DATA);
assert_eq!(2, solve2(&data)); let scores = find_scores(&data);
assert_eq!(4, scores[1][2]);
assert_eq!(8, scores[3][2]);
assert_eq!(8, solve2(&data));
} }
} }