Slightly better ICACHE hits

This commit is contained in:
2022-12-09 09:20:34 +01:00
parent 951ed73024
commit bbfa367775

View File

@@ -70,30 +70,34 @@ fn scenery<'a>(
tree_stack.clear(); 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() {
if i > 0 { scenery_helper(i, tree_stack, val, score);
let mut first = 0; }
}
while let Some((pos, other)) = tree_stack.pop() { fn scenery_helper(i: usize, tree_stack: &mut Vec<(usize, u8)>, val: u8, score: &mut usize) {
match other.cmp(&val) { if i > 0 {
Ordering::Less => (), let mut first = 0;
Ordering::Equal => {
first = pos; while let Some((pos, other)) = tree_stack.pop() {
break; match other.cmp(&val) {
} Ordering::Less => (),
Ordering::Greater => { Ordering::Equal => {
tree_stack.push((pos, other)); first = pos;
first = pos; break;
break; }
} Ordering::Greater => {
tree_stack.push((pos, other));
first = pos;
break;
} }
} }
tree_stack.push((i, val));
*score *= i - first;
} else {
*score = 0;
} }
tree_stack.push((i, val));
*score *= i - first;
} else {
*score = 0;
} }
} }
@@ -106,7 +110,7 @@ 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::new(); 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() {