This commit is contained in:
2025-12-04 11:38:32 +01:00
parent 50f6487846
commit 2c36198ba8
3 changed files with 188 additions and 47 deletions

View File

@@ -1,4 +1,4 @@
use std::mem;
// The use of these 2 could be optimized but not fatal performance
fn is_paperroll(slice: &[u8], idx: usize) -> bool {
slice.get(idx).is_some_and(|&x| x == b'@')
@@ -21,28 +21,40 @@ fn count_rolls_at_postns(
#[unsafe(no_mangle)]
fn challenge_usize(buf: &[u8]) -> usize {
let mut prev_line = None;
challenge_usize_inplace(&buf[0..buf.len() - 1])
}
fn challenge_usize_inplace(buf: &[u8]) -> usize {
let mut prev_line: Option<&[u8]> = None;
let mut surrounded_line: Option<&[u8]> = None;
let mut total = 0;
for ln in buf[0..(buf.len() - 1)].split(|&b| b == b'\n') {
if let Some(fln) = surrounded_line
&& prev_line.is_none()
{
for (i, _) in fln.iter().enumerate().filter(|(_, b)| **b == b'@') {
let adj_count = count_rolls_at_postns(fln, i, [-1, 1])
+ count_rolls_at_postns(ln, i, [-1, 0, 1]);
if adj_count < 4 {
total += 1;
for ln in buf.split(|&b| b == b'\n') {
if let Some(fln) = surrounded_line {
if prev_line.is_none() {
for i in 0..fln.len() {
if fln[i] != b'@' {
continue;
}
let adj_count = count_rolls_at_postns(fln, i, [-1, 1])
+ count_rolls_at_postns(ln, i, [-1, 0, 1]);
if adj_count < 4 {
total += 1;
}
}
}
surrounded_line = Some(fln);
}
if let Some(pln) = prev_line {
let sln = surrounded_line.unwrap();
for (i, _) in sln.iter().enumerate().filter(|(_, b)| **b == b'@') {
for i in 0..sln.len() {
if sln[i] != b'@' {
continue;
}
let adj_count = count_rolls_at_postns(pln, i, [-1, 0, 1])
+ count_rolls_at_postns(sln, i, [-1, 1])
+ count_rolls_at_postns(ln, i, [-1, 0, 1]);
@@ -50,15 +62,22 @@ fn challenge_usize(buf: &[u8]) -> usize {
total += 1;
}
}
surrounded_line = Some(sln);
}
prev_line = surrounded_line;
prev_line = surrounded_line.take();
surrounded_line = Some(ln);
}
let pln = prev_line.unwrap();
let lln = surrounded_line.unwrap();
for (i, _) in lln.iter().enumerate().filter(|(_, b)| **b == b'@') {
for i in 0..lln.len() {
if lln[i] != b'@' {
continue;
}
let adj_count =
count_rolls_at_postns(pln, i, [-1, 0, 1]) + count_rolls_at_postns(lln, i, [-1, 1]);
if adj_count < 4 {