generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
07.rs
80 lines (69 loc) · 2.35 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
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
use rayon::iter::ParallelIterator;
use itertools::Itertools;
use rayon::prelude::ParallelString;
advent_of_code::solution!(7);
fn parse_and_solve<F>(input: &str, equation_checker: F) -> u64
where F: Fn(&[u64], &u64) -> bool + Sync
{
input.par_lines()
.map(|line| {
let mut split = line.split_whitespace();
let Some(expected) = split.next() else { panic!() };
let expected = expected[0..expected.len() - 1].parse::<u64>().unwrap();
let terms = split.map(|num| num.parse::<u64>().unwrap()).collect_vec();
(terms, expected)
})
.filter_map(|(terms, expected)| {
if equation_checker(&terms, &expected) { Some(expected) } else { None }
})
.sum()
}
fn is_valid_equation(terms: &[u64], expected: &u64) -> bool {
if terms.len() == 1 {
return &terms[0] == expected;
}
let x = terms.last().unwrap();
let slice = &terms[..terms.len() - 1];
if expected % x == 0 && is_valid_equation(slice, &(expected / x)) {
true
} else {
expected >= x && is_valid_equation(slice, &(expected - x))
}
}
fn is_valid_equation_with_concat(terms: &[u64], expected: &u64) -> bool {
if terms.len() == 1 {
return &terms[0] == expected;
}
let right = terms.last().unwrap();
let slice = &terms[..terms.len() - 1];
if expected % right == 0 && is_valid_equation_with_concat(slice, &(expected / right)) {
true
} else if expected >= right && is_valid_equation_with_concat(slice, &(expected - right)) {
true
} else {
let n_digits = right.ilog10() + 1;
let y = 10u64.pow(n_digits);
let suffix = expected % y;
&suffix == right && is_valid_equation_with_concat(slice, &(expected / y))
}
}
pub fn part_one(input: &str) -> Option<u64> {
Some(parse_and_solve(input, is_valid_equation))
}
pub fn part_two(input: &str) -> Option<u64> {
Some(parse_and_solve(input, is_valid_equation_with_concat))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(3749));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(11387));
}
}