|
1 | 1 | use crate::solutions::Solution; |
| 2 | +use itertools::Itertools; |
| 3 | +use std::str::FromStr; |
2 | 4 |
|
3 | 5 | pub struct Day06; |
4 | 6 |
|
5 | 7 | impl Solution for Day06 { |
6 | | - fn part_one(&self, _input: &str) -> String { |
7 | | - String::from("0") |
| 8 | + fn part_one(&self, input: &str) -> String { |
| 9 | + let (numbers, operations) = self.parse(input); |
| 10 | + let column_count = numbers.first().unwrap().len(); |
| 11 | + |
| 12 | + (0..column_count) |
| 13 | + .map(|column| { |
| 14 | + let operation = operations.get(column).unwrap(); |
| 15 | + let numbers_in_column = numbers.iter().map(|n_vec| n_vec[column]); |
| 16 | + |
| 17 | + match operation { |
| 18 | + Operation::Add => numbers_in_column.sum::<u64>(), |
| 19 | + Operation::Multiply => numbers_in_column.product(), |
| 20 | + } |
| 21 | + }) |
| 22 | + .sum::<u64>() |
| 23 | + .to_string() |
8 | 24 | } |
9 | 25 |
|
10 | 26 | fn part_two(&self, _input: &str) -> String { |
11 | 27 | String::from("0") |
12 | 28 | } |
13 | 29 | } |
14 | 30 |
|
| 31 | +impl Day06 { |
| 32 | + fn parse(&self, input: &str) -> (Vec<Vec<u64>>, Vec<Operation>) { |
| 33 | + let mut lines = input.lines().collect_vec(); |
| 34 | + let operations_str = lines.pop().unwrap(); |
| 35 | + |
| 36 | + let numbers = lines |
| 37 | + .iter() |
| 38 | + .map(|line| { |
| 39 | + line.split_whitespace() |
| 40 | + .map(|x| x.parse::<u64>().unwrap()) |
| 41 | + .collect_vec() |
| 42 | + }) |
| 43 | + .collect_vec(); |
| 44 | + |
| 45 | + let operations = operations_str |
| 46 | + .split_whitespace() |
| 47 | + .map(|x| x.parse::<Operation>().unwrap()) |
| 48 | + .collect_vec(); |
| 49 | + |
| 50 | + (numbers, operations) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +#[derive(Debug)] |
| 55 | +enum Operation { |
| 56 | + Add, |
| 57 | + Multiply, |
| 58 | +} |
| 59 | + |
| 60 | +impl FromStr for Operation { |
| 61 | + type Err = (); |
| 62 | + |
| 63 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 64 | + match s { |
| 65 | + "+" => Ok(Operation::Add), |
| 66 | + "*" => Ok(Operation::Multiply), |
| 67 | + _ => Err(()), |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
15 | 72 | #[cfg(test)] |
16 | 73 | mod tests { |
17 | 74 | use crate::solutions::year2025::day06::Day06; |
18 | 75 | use crate::solutions::Solution; |
19 | 76 |
|
20 | | - const EXAMPLE: &str = r#""#; |
| 77 | + const EXAMPLE: &str = r#"123 328 51 64 |
| 78 | + 45 64 387 23 |
| 79 | + 6 98 215 314 |
| 80 | +* + * + "#; |
21 | 81 |
|
22 | 82 | #[test] |
23 | 83 | fn part_one_example_test() { |
24 | | - assert_eq!("0", Day06.part_one(EXAMPLE)); |
| 84 | + assert_eq!("4277556", Day06.part_one(EXAMPLE)); |
25 | 85 | } |
26 | 86 | } |
0 commit comments