tangled
alpha
login
or
join now
nove.dev
/
aoc-2025
1
fork
atom
:)
1
fork
atom
overview
issues
pulls
pipelines
day3 part2
nove.dev
3 months ago
a81a2ce7
be71653d
+39
-12
2 changed files
expand all
collapse all
unified
split
src
day3.rs
lib.rs
+32
-5
src/day3.rs
···
1
1
pub fn day3_part1(input: &str) -> String {
2
2
let batteries = parse(input);
3
3
-
let sum: u32 = batteries.iter().map(|bank| max_joltage(bank)).sum();
3
3
+
let sum: u64 = batteries.iter().map(|bank| max_joltage(bank)).sum();
4
4
sum.to_string()
5
5
}
6
6
pub fn day3_part2(input: &str) -> String {
7
7
let batteries = parse(input);
8
8
-
todo!()
8
8
+
let sum: u64 = batteries.iter().map(|bank| max_12_joltage(bank)).sum();
9
9
+
sum.to_string()
10
10
+
}
11
11
+
12
12
+
fn max_12_joltage(bank: &[u64]) -> u64 {
13
13
+
let bank_length = bank.len();
14
14
+
let mut digits = Vec::with_capacity(12);
15
15
+
let mut cursor = 0;
16
16
+
for place in 0..12 {
17
17
+
let max_digit = bank[cursor..bank_length - (12 - (place + 1))]
18
18
+
.iter()
19
19
+
.max()
20
20
+
.unwrap();
21
21
+
let index_of_max_digit = bank[cursor..bank_length - (12 - (place + 1))]
22
22
+
.iter()
23
23
+
.enumerate()
24
24
+
.find(|(_, digit)| *digit == max_digit)
25
25
+
.unwrap()
26
26
+
.0;
27
27
+
cursor = index_of_max_digit + cursor + 1;
28
28
+
digits.push(max_digit)
29
29
+
}
30
30
+
31
31
+
digits.into_iter().fold(0, |acc, digit| acc * 10 + digit)
9
32
}
10
33
11
11
-
fn max_joltage(bank: &[u32]) -> u32 {
34
34
+
fn max_joltage(bank: &[u64]) -> u64 {
12
35
let bank_length = bank.len();
13
36
let max_digit_not_final = bank[..bank_length - 1].iter().max().unwrap();
14
37
let index_of_max_digit = bank
···
21
44
*max_digit_not_final * 10 + max_ones
22
45
}
23
46
24
24
-
fn parse(input: &str) -> Vec<Vec<u32>> {
47
47
+
fn parse(input: &str) -> Vec<Vec<u64>> {
25
48
input
26
49
.lines()
27
27
-
.map(|bank| bank.chars().map(|c| c.to_digit(10).unwrap()).collect())
50
50
+
.map(|bank| {
51
51
+
bank.chars()
52
52
+
.map(|c| c.to_digit(10).unwrap() as u64)
53
53
+
.collect()
54
54
+
})
28
55
.collect()
29
56
}
+7
-7
src/lib.rs
···
128
128
let result = day3::day3_part1(include_str!("../input/day3.txt"));
129
129
assert_eq!(result, "17613");
130
130
}
131
131
-
// #[test]
132
132
-
// fn day3_part2_test() {
133
133
-
// let test_result = day3::day3_part2(include_str!("../input/day3.test.txt"));
134
134
-
// assert_eq!(test_result, "4174379265");
135
135
-
// let result = day3::day3_part2(include_str!("../input/day3.txt"));
136
136
-
// assert_eq!(result, "45283684555");
137
137
-
// }
131
131
+
#[test]
132
132
+
fn day3_part2_test() {
133
133
+
let test_result = day3::day3_part2(include_str!("../input/day3.test.txt"));
134
134
+
assert_eq!(test_result, "3121910778619");
135
135
+
let result = day3::day3_part2(include_str!("../input/day3.txt"));
136
136
+
assert_eq!(result, "175304218462560");
137
137
+
}
138
138
139
139
#[test]
140
140
fn day2_part1_test() {