|
| 1 | +use itertools::Itertools; |
| 2 | + |
| 3 | +advent_of_code::solution!(6); |
| 4 | + |
| 5 | +fn split_input(input: &str) -> (&str, Vec<&str>) { |
| 6 | + let lines = input.lines().collect_vec(); |
| 7 | + // trim because of part 2 final coumn width calculation |
| 8 | + let ops_line = &lines[lines.len() - 1].trim(); |
| 9 | + let num_lines = lines[..lines.len() - 1].to_vec(); |
| 10 | + (ops_line, num_lines) |
| 11 | +} |
| 12 | + |
| 13 | +fn calc_total(ops: &[&str], nums: &[Vec<u64>]) -> u64 { |
| 14 | + nums.iter() |
| 15 | + .enumerate() |
| 16 | + .fold(vec![0; ops.len()], |mut totals, (i, v)| { |
| 17 | + let t = &mut totals[i]; |
| 18 | + v.iter().for_each(|x| match ops[i] { |
| 19 | + _ if *t == 0 => *t = *x, |
| 20 | + "*" => *t *= *x, |
| 21 | + "+" => *t += *x, |
| 22 | + other => panic!("Should exist: {}", other), |
| 23 | + }); |
| 24 | + totals |
| 25 | + }) |
| 26 | + .iter() |
| 27 | + .sum() |
| 28 | +} |
| 29 | + |
| 30 | +pub fn part_one(input: &str) -> Option<u64> { |
| 31 | + let (ops_line, num_lines) = split_input(input); |
| 32 | + let ops = ops_line.split_whitespace().collect_vec(); |
| 33 | + let ops_count = ops.len(); |
| 34 | + let mut nums = vec![vec![0; num_lines.len()]; ops_count]; |
| 35 | + num_lines.iter().enumerate().for_each(|(i, &s)| { |
| 36 | + s.split_whitespace() |
| 37 | + .enumerate() |
| 38 | + .for_each(|(col, x)| nums[col][i] = x.parse::<u64>().expect("should be ints")) |
| 39 | + }); |
| 40 | + Some(calc_total(&ops, &nums)) |
| 41 | +} |
| 42 | + |
| 43 | +pub fn part_two(input: &str) -> Option<u64> { |
| 44 | + let (ops_line, num_lines) = split_input(input); |
| 45 | + let ops = ops_line.split_whitespace().collect_vec(); |
| 46 | + let ops_count = ops.len(); |
| 47 | + let mut col_numbers = vec![0; ops_count]; |
| 48 | + // each op is the first index in the number column for its set of numbers |
| 49 | + // use this to get the count of columns (numbers) in the number column |
| 50 | + ops_line.chars().skip(1).fold(0, |col, c| match c { |
| 51 | + '*' | '+' => col + 1, |
| 52 | + ' ' => { |
| 53 | + col_numbers[col] += 1; |
| 54 | + col |
| 55 | + } |
| 56 | + other => panic!("Shouldn't exist: {}", other), |
| 57 | + }); |
| 58 | + // last col_width can't be known from ops row alone |
| 59 | + let max_width = num_lines |
| 60 | + .iter() |
| 61 | + .map(|s| s.len()) |
| 62 | + .max() |
| 63 | + .expect("Sum should exist"); |
| 64 | + col_numbers[ops_count - 1] = max_width - ops_line.len() + 1; |
| 65 | + |
| 66 | + let mut nums = col_numbers.iter().map(|x| vec![0; *x]).collect_vec(); |
| 67 | + num_lines.iter().for_each(|&s| { |
| 68 | + s.chars().fold((0, 0), |(col_idx, number_idx), c| { |
| 69 | + if number_idx >= col_numbers[col_idx] { |
| 70 | + // we are in-between number columns |
| 71 | + return (col_idx + 1, 0); |
| 72 | + } |
| 73 | + let update_num = &mut nums[col_idx][number_idx]; |
| 74 | + if let Some(x) = c.to_digit(10) { |
| 75 | + *update_num = *update_num * 10 + x as u64; |
| 76 | + } |
| 77 | + (col_idx, number_idx + 1) |
| 78 | + }); |
| 79 | + }); |
| 80 | + Some(calc_total(&ops, &nums)) |
| 81 | +} |
| 82 | + |
| 83 | +#[cfg(test)] |
| 84 | +mod tests { |
| 85 | + use super::*; |
| 86 | + |
| 87 | + #[test] |
| 88 | + fn test_part_one() { |
| 89 | + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); |
| 90 | + assert_eq!(result, Some(4277556)); |
| 91 | + } |
| 92 | + |
| 93 | + #[test] |
| 94 | + fn test_part_two() { |
| 95 | + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); |
| 96 | + assert_eq!(result, Some(3263827)); |
| 97 | + } |
| 98 | +} |
0 commit comments