mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Compare commits
2 Commits
a01eb547d1
...
2022/day-0
| Author | SHA1 | Date | |
|---|---|---|---|
| bbfa367775 | |||
| 951ed73024 |
@@ -1,3 +1,5 @@
|
|||||||
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
|
||||||
@@ -63,19 +65,39 @@ pub fn part1(input: &[u8]) -> Result<String> {
|
|||||||
fn scenery<'a>(
|
fn scenery<'a>(
|
||||||
values: impl IntoIterator<Item = &'a u8>,
|
values: impl IntoIterator<Item = &'a u8>,
|
||||||
visible: impl IntoIterator<Item = &'a mut usize>,
|
visible: impl IntoIterator<Item = &'a mut usize>,
|
||||||
|
tree_stack: &mut Vec<(usize, u8)>,
|
||||||
) {
|
) {
|
||||||
let mut last_seen = [0; 10];
|
tree_stack.clear();
|
||||||
|
|
||||||
for (i, (&val, score)) in values.into_iter().zip(visible).enumerate() {
|
for (i, (&val, score)) in values.into_iter().zip(visible).enumerate() {
|
||||||
let val = val - b'0';
|
scenery_helper(i, tree_stack, val, score);
|
||||||
let visible = i - last_seen[val as usize];
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if i > 0 {
|
fn scenery_helper(i: usize, tree_stack: &mut Vec<(usize, u8)>, val: u8, score: &mut usize) {
|
||||||
*score *= visible;
|
if i > 0 {
|
||||||
last_seen[..=(val as usize)].fill(i);
|
let mut first = 0;
|
||||||
} else {
|
|
||||||
*score = 0;
|
while let Some((pos, other)) = tree_stack.pop() {
|
||||||
|
match other.cmp(&val) {
|
||||||
|
Ordering::Less => (),
|
||||||
|
Ordering::Equal => {
|
||||||
|
first = pos;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
Ordering::Greater => {
|
||||||
|
tree_stack.push((pos, other));
|
||||||
|
first = pos;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tree_stack.push((i, val));
|
||||||
|
|
||||||
|
*score *= i - first;
|
||||||
|
} else {
|
||||||
|
*score = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,15 +110,18 @@ pub fn part2(input: &[u8]) -> Result<String> {
|
|||||||
|
|
||||||
let mut score = vec![1; width * height];
|
let mut score = vec![1; width * height];
|
||||||
|
|
||||||
|
let mut tree_stack = Vec::with_capacity(10);
|
||||||
|
|
||||||
// Horizontal striping
|
// Horizontal striping
|
||||||
for (y, row) in input.chunks_exact(width + 1).enumerate() {
|
for (y, row) in input.chunks_exact(width + 1).enumerate() {
|
||||||
// First, left to right
|
// First, left to right
|
||||||
scenery(&row[..width], &mut score[(y * width)..]);
|
scenery(&row[..width], &mut score[(y * width)..], &mut tree_stack);
|
||||||
|
|
||||||
// Then right to left
|
// Then right to left
|
||||||
scenery(
|
scenery(
|
||||||
row[..width].iter().rev(),
|
row[..width].iter().rev(),
|
||||||
score[(y * width)..(y * width + width)].iter_mut().rev(),
|
score[(y * width)..(y * width + width)].iter_mut().rev(),
|
||||||
|
&mut tree_stack,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,12 +131,14 @@ pub fn part2(input: &[u8]) -> Result<String> {
|
|||||||
scenery(
|
scenery(
|
||||||
input[x..].iter().step_by(width + 1),
|
input[x..].iter().step_by(width + 1),
|
||||||
score[x..].iter_mut().step_by(width),
|
score[x..].iter_mut().step_by(width),
|
||||||
|
&mut tree_stack,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Bottom to top
|
// Bottom to top
|
||||||
scenery(
|
scenery(
|
||||||
input[x..].iter().step_by(width + 1).rev(),
|
input[x..].iter().step_by(width + 1).rev(),
|
||||||
score[x..].iter_mut().step_by(width).rev(),
|
score[x..].iter_mut().step_by(width).rev(),
|
||||||
|
&mut tree_stack,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user