Skip to content

Commit 68666de

Browse files
committed
refactor(06/2025): simplify
1 parent 79d1eae commit 68666de

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

src/solutions/year2025/day06.rs

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,14 @@ pub struct Day06;
77
impl Solution for Day06 {
88
fn part_one(&self, input: &str) -> String {
99
let (numbers, operations) = self.parse_part_one(input);
10-
let column_count = operations.len();
1110

12-
(0..column_count)
13-
.map(|column| {
14-
let operation = operations.get(column).unwrap();
11+
operations
12+
.iter()
13+
.enumerate()
14+
.map(|(column, operation)| {
1515
let numbers_in_column = numbers.iter().map(|n_vec| n_vec[column]);
1616

17-
match operation {
18-
Operation::Add => numbers_in_column.sum::<u64>(),
19-
Operation::Multiply => numbers_in_column.product(),
20-
}
17+
operation.calculate(numbers_in_column)
2118
})
2219
.sum::<u64>()
2320
.to_string()
@@ -31,12 +28,9 @@ impl Solution for Day06 {
3128
.rev()
3229
.enumerate()
3330
.map(|(column, operation)| {
34-
let numbers_in_column = numbers.get(column).unwrap();
31+
let numbers_in_column = numbers.get(column).unwrap().iter().copied();
3532

36-
match operation {
37-
Operation::Add => numbers_in_column.iter().sum::<u64>(),
38-
Operation::Multiply => numbers_in_column.iter().product(),
39-
}
33+
operation.calculate(numbers_in_column)
4034
})
4135
.sum::<u64>()
4236
.to_string()
@@ -47,6 +41,7 @@ impl Day06 {
4741
fn parse_part_one(&self, input: &str) -> (Vec<Vec<u64>>, Vec<Operation>) {
4842
let mut lines = input.lines().collect_vec();
4943
let operations_str = lines.pop().unwrap();
44+
let operations = self.parse_operations(operations_str);
5045

5146
let numbers = lines
5247
.iter()
@@ -57,25 +52,17 @@ impl Day06 {
5752
})
5853
.collect_vec();
5954

60-
let operations = operations_str
61-
.split_whitespace()
62-
.map(|x| x.parse::<Operation>().unwrap())
63-
.collect_vec();
64-
6555
(numbers, operations)
6656
}
6757

6858
fn parse_part_two(&self, input: &str) -> (Vec<Vec<u64>>, Vec<Operation>) {
6959
let mut lines = input.lines().collect_vec();
7060
let operations_str = lines.pop().unwrap();
71-
let operations = operations_str
72-
.split_whitespace()
73-
.map(|x| x.parse::<Operation>().unwrap())
74-
.collect_vec();
61+
let operations = self.parse_operations(operations_str);
7562

7663
let column_width = operations_str.len();
7764

78-
let chunks = (0..column_width)
65+
let column_results: Vec<_> = (0..column_width)
7966
.rev()
8067
.map(|column| {
8168
lines
@@ -85,19 +72,23 @@ impl Day06 {
8572
.join("")
8673
.parse::<u64>()
8774
})
88-
.chunk_by(|x| x.is_err());
89-
90-
let mut groups = Vec::new();
91-
for (_, rest) in &chunks {
92-
let numbers = rest.flatten().collect_vec();
75+
.collect();
9376

94-
if !numbers.is_empty() {
95-
groups.push(numbers);
96-
}
97-
}
77+
let groups = column_results
78+
.split(|r| r.is_err())
79+
.filter(|slice| !slice.is_empty())
80+
.map(|slice| slice.iter().map(|r| *r.as_ref().unwrap()).collect())
81+
.collect();
9882

9983
(groups, operations)
10084
}
85+
86+
fn parse_operations(&self, input: &str) -> Vec<Operation> {
87+
input
88+
.split_whitespace()
89+
.map(|x| x.parse::<Operation>().unwrap())
90+
.collect_vec()
91+
}
10192
}
10293

10394
#[derive(Debug)]
@@ -106,6 +97,15 @@ enum Operation {
10697
Multiply,
10798
}
10899

100+
impl Operation {
101+
fn calculate(&self, numbers: impl Iterator<Item = u64>) -> u64 {
102+
match self {
103+
Operation::Add => numbers.sum(),
104+
Operation::Multiply => numbers.product(),
105+
}
106+
}
107+
}
108+
109109
impl FromStr for Operation {
110110
type Err = ();
111111

0 commit comments

Comments
 (0)