-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday07.rs
108 lines (89 loc) · 2.56 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use crate::util::file::read;
fn process_file(filename: &str) -> Vec<(u64, Vec<u64>)> {
let mut vec = vec![];
read(filename)
.unwrap()
.into_iter()
.map(|line| line.unwrap())
.for_each(|line| {
let mut iter = line.split(": ");
// Get the total for the row
let total = iter.next().unwrap().parse::<u64>().unwrap();
// Find all the values associated with the total.
let values = iter
.next()
.unwrap()
.split(' ')
.map(|num| num.parse::<u64>().unwrap())
.collect::<Vec<u64>>();
vec.push((total, values));
});
return vec;
}
fn evaluate(
total: &u64,
current_value: u64,
values: &[u64],
check_concatenation: &bool,
) -> Option<u64> {
if values.len() == 0 {
if total == ¤t_value {
return Some(current_value);
} else {
return None;
}
}
// Addition and multiplication never lower the value, so if our
// value is greater then the total we know this case failed.
if ¤t_value > total {
return None;
}
let value = values[0];
// Evaluate the * case
let case = evaluate(
total,
¤t_value * &value,
&values[1..],
&check_concatenation,
);
if case.is_some() {
return case;
}
// Only enable this feature when we need it for evaluation.
if check_concatenation == &true {
let case = evaluate(
total,
[current_value.to_string(), value.to_string()]
.concat()
.parse::<u64>()
.unwrap(),
&values[1..],
&check_concatenation,
);
if case.is_some() {
return case;
}
}
// If it fails, evalaute the + case
let case = evaluate(
total,
¤t_value + &value,
&values[1..],
&check_concatenation,
);
return case;
}
fn get_calibration_result(vec: &Vec<(u64, Vec<u64>)>, check_concatenation: bool) -> u64 {
vec.iter()
.map(|(total, values)| evaluate(total, values[0], &values[1..], &check_concatenation))
.flatten()
.sum()
}
pub fn run() -> (usize, usize) {
let vec = process_file("input/year2024/day07.txt");
let part1_count = get_calibration_result(&vec, false);
let part2_count = get_calibration_result(&vec, true);
println!("{:?}", part1_count);
println!("{:?}", part2_count);
return (0, 0);
}