1use crate::util::grid::*;
14use crate::util::point::*;
15
16type Input = (u64, u64);
17
18pub fn parse(input: &str) -> Input {
19 let grid = Grid::parse(input);
20 let bottom = grid.height - 1;
21 let mut right = grid.width;
22 let mut part_one = 0;
23 let mut part_two = 0;
24
25 for left in (0..grid.width).rev().filter(|&x| grid[Point::new(x, bottom)] != b' ') {
27 let rows = (0..bottom).map(|y| (left..right).fold(0, |num, x| acc(&grid, num, x, y)));
28 let cols = (left..right).map(|x| (0..bottom).fold(0, |num, y| acc(&grid, num, x, y)));
29
30 let plus = grid[Point::new(left, bottom)] == b'+';
32 let first: u64 = if plus { rows.sum() } else { rows.product() };
33 let second: u64 = if plus { cols.sum() } else { cols.product() };
34
35 right = left - 1;
36 part_one += first;
37 part_two += second;
38 }
39
40 (part_one, part_two)
41}
42
43pub fn part1(input: &Input) -> u64 {
44 input.0
45}
46
47pub fn part2(input: &Input) -> u64 {
48 input.1
49}
50
51fn acc(grid: &Grid<u8>, number: u64, x: i32, y: i32) -> u64 {
53 let digit = grid[Point::new(x, y)];
54 if digit == b' ' { number } else { 10 * number + u64::from(digit - b'0') }
55}