fixed day06 test
This commit is contained in:
@@ -1,45 +1,59 @@
|
|||||||
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 {
|
pub fn solve(puzzle: String) -> u64 {
|
||||||
let mut solution: u64 = 0;
|
let mut solution: u64 = 0;
|
||||||
|
|
||||||
puzzle
|
let lines = puzzle
|
||||||
.lines()
|
.lines()
|
||||||
.map(|line| line.chars())
|
.map(|line| line.chars())
|
||||||
.tuple_windows::<(_, _, _, _, _)>()
|
.collect::<Vec<_>>();
|
||||||
.for_each(|(a, b, c, d, op)| {
|
|
||||||
let mut operator: char = ' ';
|
|
||||||
let mut temp: u64 = 0;
|
|
||||||
a
|
|
||||||
.zip(b)
|
|
||||||
.zip(c)
|
|
||||||
.zip(d)
|
|
||||||
.zip(op)
|
|
||||||
.map(|((((a, b), c), d), op)| {
|
|
||||||
let mut value: u64 = 0;
|
|
||||||
[a, b, c, d]
|
|
||||||
.iter()
|
|
||||||
.filter(|c| **c != ' ')
|
|
||||||
.for_each(|c| {value = value * 10 + (*c as u8 - b'0') as u64});
|
|
||||||
(value, op)
|
|
||||||
})
|
|
||||||
.for_each(|(value, op)| {
|
|
||||||
if op != ' ' {
|
|
||||||
operator = op;
|
|
||||||
solution += temp;
|
|
||||||
temp = if op == '*' {1} else {0};
|
|
||||||
}
|
|
||||||
if value == 0 { return }
|
|
||||||
match operator {
|
|
||||||
'+' => temp += value,
|
|
||||||
'*' => temp *= value,
|
|
||||||
_ => unreachable!(),
|
|
||||||
}
|
|
||||||
});
|
|
||||||
solution += temp;
|
|
||||||
});
|
|
||||||
|
|
||||||
solution
|
let mut operator: char = ' ';
|
||||||
|
let mut temp: u64 = 0;
|
||||||
|
ZipAll { iters: lines }
|
||||||
|
.map(|col| {
|
||||||
|
let mut value: u64 = 0;
|
||||||
|
col[..col.len() - 1]
|
||||||
|
.iter()
|
||||||
|
.filter(|c| **c != ' ')
|
||||||
|
.for_each(|c| {value = value * 10 + (*c as u8 - b'0') as u64});
|
||||||
|
(value, *col.last().unwrap())
|
||||||
|
})
|
||||||
|
.for_each(|(value, op)| {
|
||||||
|
if op != ' ' {
|
||||||
|
operator = op;
|
||||||
|
solution += temp;
|
||||||
|
temp = if op == '*' {1} else {0};
|
||||||
|
}
|
||||||
|
if value == 0 { return }
|
||||||
|
match operator {
|
||||||
|
'+' => temp += value,
|
||||||
|
'*' => temp *= value,
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
solution + temp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user