Skip to content

Commit f591f7c

Browse files
committed
Day 03 solutions
1 parent 1592153 commit f591f7c

File tree

5 files changed

+367
-1
lines changed

5 files changed

+367
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,5 @@ This should start the server at `localhost:8080`.
8787
## Solutions
8888

8989
❄️ [Day 01](aoc-solver/src/y2025/day01.rs)
90+
❄️ [Day 02](aoc-solver/src/y2025/day02.rs)
91+
❄️ [Day 03](aoc-solver/src/y2025/day03.rs)

aoc-solver/src/y2025/day03.rs

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
use crate::solution::{AocError, Solution};
2+
3+
pub struct Day03;
4+
5+
fn parse(input: &str) -> Result<Vec<Vec<u32>>, AocError> {
6+
input
7+
.trim()
8+
.lines()
9+
.map(|batteries| {
10+
batteries
11+
.chars()
12+
.map(|battery| {
13+
battery
14+
.to_digit(10)
15+
.ok_or(AocError::parse(battery, "invalid joltage"))
16+
})
17+
.collect()
18+
})
19+
.collect()
20+
}
21+
22+
fn find_max_joltage(banks: &[u32], digits: usize) -> u64 {
23+
let mut skip = 0;
24+
let mut result = 0;
25+
26+
for digit in 0..digits {
27+
let digit_pos = digits - digit - 1;
28+
let take = banks.len() - skip - digit_pos;
29+
30+
let mut banks_to_check = banks.iter().skip(skip).take(take);
31+
let max = banks_to_check.clone().max().unwrap();
32+
33+
skip += banks_to_check.position(|battery| battery == max).unwrap() + 1;
34+
result += 10u64.pow(digit_pos as u32) * *max as u64
35+
}
36+
37+
result
38+
}
39+
40+
impl Solution for Day03 {
41+
type Part1 = u64;
42+
type Part2 = u64;
43+
44+
fn default_input(&self) -> &'static str {
45+
include_str!("../../../inputs/2025/day03.txt")
46+
}
47+
48+
fn part_1(&self, input: &str) -> Result<u64, AocError> {
49+
let total = parse(input)?
50+
.iter()
51+
.map(|bank| find_max_joltage(bank, 2))
52+
.sum();
53+
54+
Ok(total)
55+
}
56+
57+
fn part_2(&self, input: &str) -> Result<u64, AocError> {
58+
let total = parse(input)?
59+
.iter()
60+
.map(|bank| find_max_joltage(bank, 12))
61+
.sum();
62+
63+
Ok(total)
64+
}
65+
}
66+
67+
#[cfg(test)]
68+
mod tests {
69+
use super::*;
70+
71+
#[test]
72+
fn it_finds_max_joltage_1() {
73+
assert_eq!(
74+
find_max_joltage(&[9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 1, 1, 1, 1, 1], 2),
75+
98
76+
);
77+
}
78+
79+
#[test]
80+
fn it_finds_max_joltage_2() {
81+
assert_eq!(
82+
find_max_joltage(&[8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9], 2),
83+
89
84+
)
85+
}
86+
87+
#[test]
88+
fn it_finds_max_joltage_3() {
89+
assert_eq!(
90+
find_max_joltage(&[2, 3, 4, 2, 3, 4, 2, 3, 4, 2, 3, 4, 2, 7, 8], 2),
91+
78
92+
)
93+
}
94+
95+
#[test]
96+
fn it_finds_max_joltage_4() {
97+
assert_eq!(
98+
find_max_joltage(&[8, 1, 8, 1, 8, 1, 9, 1, 1, 1, 1, 2, 1, 1, 1], 2),
99+
92
100+
)
101+
}
102+
103+
#[test]
104+
fn it_finds_max_joltage_5() {
105+
assert_eq!(
106+
find_max_joltage(&[9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 1, 1, 1, 1, 1], 12),
107+
987654321111
108+
);
109+
}
110+
111+
#[test]
112+
fn it_finds_max_joltage_6() {
113+
assert_eq!(
114+
find_max_joltage(&[8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9], 12),
115+
811111111119
116+
);
117+
}
118+
119+
#[test]
120+
fn it_finds_max_joltage_7() {
121+
assert_eq!(
122+
find_max_joltage(&[2, 3, 4, 2, 3, 4, 2, 3, 4, 2, 3, 4, 2, 7, 8], 12),
123+
434234234278
124+
);
125+
}
126+
127+
#[test]
128+
fn it_finds_max_joltage_8() {
129+
assert_eq!(
130+
find_max_joltage(&[8, 1, 8, 1, 8, 1, 9, 1, 1, 1, 1, 2, 1, 1, 1], 12),
131+
888911112111
132+
);
133+
}
134+
135+
#[test]
136+
fn it_solves_part1_example() {
137+
assert_eq!(
138+
Day03.part_1(
139+
"987654321111111\n\
140+
811111111111119\n\
141+
234234234234278\n\
142+
818181911112111"
143+
),
144+
Ok(357)
145+
);
146+
}
147+
148+
#[test]
149+
fn it_solves_part2_example() {
150+
assert_eq!(
151+
Day03.part_2(
152+
"987654321111111\n\
153+
811111111111119\n\
154+
234234234234278\n\
155+
818181911112111"
156+
),
157+
Ok(3121910778619)
158+
);
159+
}
160+
}

aoc-solver/src/y2025/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ use crate::solution::{Solution, Solver};
22

33
pub mod day01;
44
pub mod day02;
5+
pub mod day03;
56

6-
pub const MAX_DAYS: u8 = 2;
7+
pub const MAX_DAYS: u8 = 3;
78

89
pub struct Y2025;
910

@@ -12,6 +13,7 @@ impl Solver for Y2025 {
1213
match day {
1314
1 => day01::Day01.run(input, 1, 2025),
1415
2 => day02::Day02.run(input, 2, 2025),
16+
3 => day03::Day03.run(input, 3, 2025),
1517
_ => vec![String::from("Solution not implemented (yet?)")],
1618
}
1719
}
@@ -31,6 +33,7 @@ impl Solver for Y2025 {
3133
match day {
3234
1 => include_str!("./day01.rs"),
3335
2 => include_str!("./day02.rs"),
36+
3 => include_str!("./day03.rs"),
3437
_ => unimplemented!(),
3538
}
3639
}

aoc-web/src/header.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub fn header(props: &HeaderProps) -> Html {
2121
<>
2222
<NavLink route={Route::Solution { year: 2025, day: 1 }} current={props.route.clone()} text={"1"}/>
2323
<NavLink route={Route::Solution { year: 2025, day: 2 }} current={props.route.clone()} text={"2"}/>
24+
<NavLink route={Route::Solution { year: 2025, day: 3 }} current={props.route.clone()} text={"3"}/>
2425
</>
2526
}
2627
},

0 commit comments

Comments
 (0)