Implement 2025 - Day 6 - Part 2
This commit is contained in:
58
2025/06.rs
58
2025/06.rs
@@ -10,32 +10,50 @@ enum Operation {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let raw_grid: Vec<_> = CONTENT.lines().map(|l| l.trim()).filter(|l| !l.is_empty()).map(|l| {
|
||||
l.split(' ').map(|c| c.trim()).filter(|c| !c.is_empty()).collect::<Vec<_>>()
|
||||
let raw_grid: Vec<_> = CONTENT.lines().map(|l| {
|
||||
l.chars().collect::<Vec<_>>()
|
||||
}).collect();
|
||||
|
||||
println!("Raw-Grid\n{:?}", raw_grid);
|
||||
|
||||
let height = raw_grid.len();
|
||||
let width = raw_grid[0].len();
|
||||
|
||||
let separators: Vec<_> = (0..width).filter(|pidx| {
|
||||
raw_grid.iter().map(|row| row[*pidx]).all(|c| c == ' ')
|
||||
}).collect();
|
||||
println!("Separators {:?}", separators);
|
||||
|
||||
let ranges: Vec<_> = (core::iter::once(0).chain(separators.iter().copied().map(|v| v+1))).zip(separators.iter().copied().chain(core::iter::once(width))).map(|(s, e)| s..e).collect();
|
||||
println!("Ranges {:?}", ranges);
|
||||
|
||||
let mut result = 0;
|
||||
|
||||
let problem_count = raw_grid[0].len();
|
||||
for p_idx in 0..problem_count {
|
||||
let op = match raw_grid.last().map(|r| r[p_idx]) {
|
||||
Some("+") => Operation::Add,
|
||||
Some("*") => Operation::Mul,
|
||||
other => unreachable!("Unexpected Operation {:?}", other),
|
||||
};
|
||||
for r in ranges.iter().cloned() {
|
||||
println!("Range {:?}", r);
|
||||
|
||||
let elem_iter = raw_grid.iter().take(raw_grid.len()-1).map(|r| r[p_idx]).map(|v| v.parse::<usize>().unwrap());
|
||||
|
||||
let value = match op {
|
||||
Operation::Add => elem_iter.sum::<usize>(),
|
||||
Operation::Mul => elem_iter.product::<usize>()
|
||||
};
|
||||
println!("V {}", value);
|
||||
|
||||
result += value;
|
||||
let values: Vec<_> = r.clone().rev().map(|p| {
|
||||
raw_grid.iter().enumerate().take(height-1).map(|(i, r)| (i, r[p])).fold(0usize, |acc, (i, v)| {
|
||||
if v == ' ' {
|
||||
return acc;
|
||||
}
|
||||
|
||||
println!("Result {}", result);
|
||||
acc * 10usize + v.to_digit(10).unwrap() as usize
|
||||
})
|
||||
}).collect();
|
||||
println!("Values: {:?}", values);
|
||||
|
||||
let operation = raw_grid[height-1][r].iter().find(|c| **c != ' ').unwrap();
|
||||
println!("Operation {:?}", operation);
|
||||
|
||||
let calc_res = match operation {
|
||||
'+' => values.into_iter().sum::<usize>(),
|
||||
'*' => values.into_iter().product::<usize>(),
|
||||
other => unreachable!("Unexpected Operation {:?}", other),
|
||||
};
|
||||
println!("Calculation {:?}", calc_res);
|
||||
|
||||
result += calc_res;
|
||||
}
|
||||
|
||||
println!("result {:?}", result);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user