tangled
alpha
login
or
join now
nove.dev
/
aoc-2024
0
fork
atom
retroactive, to derust my rust
0
fork
atom
overview
issues
pulls
pipelines
day7 part2
nove.dev
3 months ago
3d465813
b8e154cf
+94
-3
3 changed files
expand all
collapse all
unified
split
input
day7_custom.test.txt
src
day7.rs
lib.rs
+4
input/day7_custom.test.txt
···
1
1
+
12345: 12 34 5
2
2
+
12034050: 120 340 50
3
3
+
15: 1 2 3 4 5
4
4
+
120: 1 2 3 4 5
+77
-3
src/day7.rs
···
1
1
pub fn day7_part1(input: &str) -> String {
2
2
let input = parse(input);
3
3
-
let sum: u64 = input.into_iter().filter(|(res, operands)| possible(*res, operands)).map(|(res, _)| res).sum();
3
3
+
let sum: u64 = input
4
4
+
.into_iter()
5
5
+
.filter(|(res, operands)| possible(*res, operands))
6
6
+
.map(|(res, _)| res)
7
7
+
.sum();
4
8
sum.to_string()
5
9
}
6
10
11
11
+
pub fn day7_part2(input: &str) -> String {
12
12
+
let input = parse(input);
13
13
+
let sum: u64 = input
14
14
+
.into_iter()
15
15
+
.filter(|(res, operands)| possible_with_concatenation(*res, operands))
16
16
+
.map(|(res, _)| res)
17
17
+
.sum();
18
18
+
sum.to_string()
19
19
+
}
20
20
+
21
21
+
fn possible_with_concatenation(res: u64, operands: &[u64]) -> bool {
22
22
+
let operator_count = operands.len() - 1;
23
23
+
let possibilities = 0..(3u64.pow(operator_count as u32));
24
24
+
for possibility in possibilities {
25
25
+
if evaluate_with_concatenation(possibility, operands) == res {
26
26
+
return true;
27
27
+
}
28
28
+
}
29
29
+
dbg!(res);
30
30
+
false
31
31
+
}
32
32
+
33
33
+
fn evaluate_with_concatenation(mut bitpattern: u64, operands: &[u64]) -> u64 {
34
34
+
// dbg!(bitpattern, operands);
35
35
+
let selected_option = bitpattern % 3;
36
36
+
let mut result = if selected_option == 0 {
37
37
+
operands[0] + operands[1]
38
38
+
} else if selected_option == 1 {
39
39
+
operands[0] * operands[1]
40
40
+
} else {
41
41
+
concatenate(operands[0], operands[1])
42
42
+
};
43
43
+
44
44
+
let mut debug_operators = vec![];
45
45
+
46
46
+
for operator in 2..operands.len() {
47
47
+
bitpattern /= 3;
48
48
+
let selected_option = bitpattern % 3;
49
49
+
50
50
+
result = if selected_option == 0 {
51
51
+
result + operands[operator]
52
52
+
} else if selected_option == 1 {
53
53
+
result * operands[operator]
54
54
+
} else {
55
55
+
concatenate(result, operands[operator])
56
56
+
};
57
57
+
58
58
+
debug_operators.push(if selected_option == 0 {
59
59
+
'+'
60
60
+
} else if selected_option == 1 {
61
61
+
'*'
62
62
+
} else {
63
63
+
'|'
64
64
+
});
65
65
+
}
66
66
+
67
67
+
// dbg!(result, bitpattern, debug_operators);
68
68
+
result
69
69
+
}
70
70
+
71
71
+
fn concatenate(left: u64, right: u64) -> u64 {
72
72
+
let decimal_places_shift = (right as f64).log10().ceil() as u32;
73
73
+
left * 10u64.pow(decimal_places_shift) + right
74
74
+
}
75
75
+
76
76
+
#[test]
77
77
+
fn test_concatenate() {
78
78
+
assert_eq!(concatenate(15, 6), 156)
79
79
+
}
80
80
+
7
81
fn possible(res: u64, operands: &[u64]) -> bool {
8
82
// dbg!(res, operands);
9
83
let operator_count = operands.len() - 1;
···
13
87
return true;
14
88
}
15
89
}
16
16
-
90
90
+
17
91
false
18
92
}
19
93
···
41
115
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
42
116
enum Operator {
43
117
Times,
44
44
-
Plus
118
118
+
Plus,
45
119
}
46
120
47
121
fn parse(input: &str) -> Vec<(u64, Vec<u64>)> {
+13
src/lib.rs
···
356
356
assert_eq!(result, "3598800864292");
357
357
}
358
358
359
359
+
// #[ignore]
360
360
+
#[test]
361
361
+
fn day7_part2_test() {
362
362
+
// let simple_result = day7::day7_part2(include_str!("../input/day7_custom.test.txt"));
363
363
+
// assert_eq!(simple_result, (12345 + 12034050 + 15 + 120).to_string());
364
364
+
365
365
+
// let test_result = day7::day7_part2(include_str!("../input/day7.test.txt"));
366
366
+
// assert_eq!(test_result, "11387");
367
367
+
let result = day7::day7_part2(include_str!("../input/day7.txt"));
368
368
+
let parsed_result: u64 = result.parse::<u64>().unwrap();
369
369
+
assert!(parsed_result > 318_910_516_761_637, "{parsed_result} was too low");
370
370
+
}
371
371
+
359
372
#[test]
360
373
fn day6_part1_test() {
361
374
let test_result = day6::day6_part1(include_str!("../input/day6.test.txt"));