fixed day06 test

This commit is contained in:
2025-12-09 20:27:52 +01:00
parent 7928c2334f
commit 1bb8b40e36
2 changed files with 52 additions and 38 deletions

View File

@@ -1,27 +1,43 @@
use itertools::Itertools;
struct ZipAll<I> {
iters: Vec<I>,
}
impl<I> Iterator for ZipAll<I>
where
I: Iterator,
{
type Item = Vec<I::Item>;
fn next(&mut self) -> Option<Self::Item> {
let mut out = Vec::with_capacity(self.iters.len());
for it in &mut self.iters {
out.push(it.next()?); // return None if any iterator ends
}
Some(out)
}
}
pub fn solve(puzzle: String) -> u64 {
let mut solution: u64 = 0;
puzzle
let lines = puzzle
.lines()
.map(|line| line.chars())
.tuple_windows::<(_, _, _, _, _)>()
.for_each(|(a, b, c, d, op)| {
.collect::<Vec<_>>();
let mut operator: char = ' ';
let mut temp: u64 = 0;
a
.zip(b)
.zip(c)
.zip(d)
.zip(op)
.map(|((((a, b), c), d), op)| {
ZipAll { iters: lines }
.map(|col| {
let mut value: u64 = 0;
[a, b, c, d]
col[..col.len() - 1]
.iter()
.filter(|c| **c != ' ')
.for_each(|c| {value = value * 10 + (*c as u8 - b'0') as u64});
(value, op)
(value, *col.last().unwrap())
})
.for_each(|(value, op)| {
if op != ' ' {
@@ -36,10 +52,8 @@ pub fn solve(puzzle: String) -> u64 {
_ => unreachable!(),
}
});
solution += temp;
});
solution
solution + temp
}