-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07.rs
54 lines (44 loc) · 1.23 KB
/
07.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
#![feature(test)]
type Input = Vec<Equation>;
#[derive(Debug)]
struct Equation {
result: i64,
numbers: Vec<i64>,
}
fn setup(input: &str) -> Input {
input
.trim()
.lines()
.map(|line| {
let mut nums = line.split_whitespace();
let result = nums.next().unwrap().trim_end_matches(':').parse().unwrap();
let numbers = nums.map(|n| n.parse().unwrap()).collect();
Equation { result, numbers }
})
.collect()
}
fn solve<const P2: bool>(nums: &[i64], goal: i64) -> bool {
if nums.len() == 1 {
return nums[0] == goal;
}
let (&last, init) = nums.split_last().unwrap();
let p = 10i64.pow(last.ilog10() + 1);
(P2 && goal % p == last && solve::<P2>(init, goal / p))
|| (goal % last == 0 && solve::<P2>(init, goal / last))
|| solve::<P2>(init, goal - last)
}
fn part1(input: &Input) -> i64 {
input
.iter()
.filter(|eq| solve::<false>(&eq.numbers, eq.result))
.map(|eq| eq.result)
.sum()
}
fn part2(input: &Input) -> i64 {
input
.iter()
.filter(|eq| solve::<true>(&eq.numbers, eq.result))
.map(|eq| eq.result)
.sum()
}
aoc::main!(2024, 7, ex: 1);