Skip to content

Commit 9031d21

Browse files
committed
Day 6 done
1 parent 29c15e7 commit 9031d21

4 files changed

Lines changed: 104 additions & 2 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.
1010

1111
| Year | Completed |
1212
| :---: | :---: |
13-
| [2025](aoc2025) | 10/24 |
13+
| [2025](aoc2025) | 12/24 |
1414
| [2024](aoc2024) | 50/50 |
1515
| [2023](aoc2023) | 50/50 |
1616

aoc2025/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.
1111
| [Day 3](https://adventofcode.com/2025/day/3) | [code](src/bin/03.rs) |||
1212
| [Day 4](https://adventofcode.com/2025/day/4) | [code](src/bin/04.rs) |||
1313
| [Day 5](https://adventofcode.com/2025/day/5) | [code](src/bin/05.rs) |||
14-
| [Day 6](https://adventofcode.com/2025/day/6) | [code](src/bin/06.rs) | _ | _ |
14+
| [Day 6](https://adventofcode.com/2025/day/6) | [code](src/bin/06.rs) | | |
1515
| [Day 7](https://adventofcode.com/2025/day/7) | [code](src/bin/07.rs) | _ | _ |
1616
| [Day 8](https://adventofcode.com/2025/day/8) | [code](src/bin/08.rs) | _ | _ |
1717
| [Day 9](https://adventofcode.com/2025/day/9) | [code](src/bin/09.rs) | _ | _ |

aoc2025/data/examples/06.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
123 328 51 64
2+
45 64 387 23
3+
6 98 215 314
4+
* + * +

aoc2025/src/bin/06.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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

Comments
 (0)