-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday07.rs
65 lines (55 loc) · 1.84 KB
/
day07.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
pub struct Equation {
numbers: Vec<i64>,
result: i64,
}
pub fn parse(input_data: String) -> Vec<Equation> {
input_data
.lines()
.into_iter()
.map(|l| {
let parts: Vec<_> = l.splitn(2, ":").collect();
Equation {
result: parts[0]
.parse()
.expect(&format!("Invalid result to parse: \"{}\"", parts[0])),
numbers: parts[1]
.split_whitespace()
.map(|n| {
n.parse()
.expect(&format!("Invalid number to parse: \"{n}\""))
})
.collect(),
}
})
.collect()
}
pub fn is_valid_1(equation: &Equation, acc: i64, index: usize) -> bool {
if index == equation.numbers.len() {
return equation.result == acc;
}
let num = equation.numbers[index];
is_valid_1(equation, acc + num, index + 1) || is_valid_1(equation, acc * num, index + 1)
}
pub fn part_1(equations: &[Equation]) -> i64 {
equations
.iter()
.filter_map(|e| is_valid_1(e, e.numbers[0], 1).then_some(e.result))
.sum()
}
pub fn is_valid_2(equation: &Equation, acc: i64, index: usize) -> bool {
if index == equation.numbers.len() {
return equation.result == acc;
}
let num = equation.numbers[index];
let acc_len = (num as f32).log10() + 1.0;
let concatenated = acc * 10i64.pow(acc_len.floor() as u32) + num;
is_valid_2(equation, acc + num, index + 1)
|| is_valid_2(equation, if index == 0 { 0 } else { acc } * num, index + 1)
|| is_valid_2(equation, concatenated, index + 1)
}
pub fn part_2(equations: &[Equation]) -> i64 {
equations
.iter()
.filter_map(|e| is_valid_2(e, e.numbers[0], 1).then_some(e.result))
.sum()
}