generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07.rs
79 lines (71 loc) · 2.29 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
advent_of_code::solution!(7);
use itertools::Itertools;
use rayon::prelude::*;
use strum::{EnumIter, IntoEnumIterator};
#[derive(Debug, EnumIter, Clone)]
enum Operation {
Concat,
Add,
Mul,
}
fn solve(input: &str, skip: usize) -> Option<u64> {
Some(
input
.par_lines()
.map(|line| {
let mut parts = line.split(": ");
let result = parts.next().unwrap().parse::<u64>().unwrap();
let numbers = parts
.next()
.unwrap()
.split_whitespace()
.map(|n| n.parse::<u64>().unwrap())
.collect::<Vec<u64>>();
(result, numbers)
})
.filter(|(result, numbers)| {
(0..numbers.len() - 1)
.map(|_| Operation::iter().skip(skip))
.multi_cartesian_product()
.any(|c| {
let mut c = c.into_iter();
numbers.iter().copied().reduce(|acc, n| {
let op = c.next().unwrap();
match op {
Operation::Add => acc + n,
Operation::Mul => acc * n,
Operation::Concat => {
let mut spacer = 1;
while n / spacer > 0 {
spacer *= 10;
}
acc * spacer + n
}
}
}) == Some(*result)
})
})
.map(|(result, _)| result)
.sum::<u64>(),
)
}
pub fn part_one(input: &str) -> Option<u64> {
solve(input, 1)
}
pub fn part_two(input: &str) -> Option<u64> {
solve(input, 0)
}
#[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));
}
}