generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
781f289
commit df5230e
Showing
5 changed files
with
127 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
190: 10 19 | ||
3267: 81 40 27 | ||
83: 17 5 | ||
156: 15 6 | ||
7290: 6 8 6 15 | ||
161011: 16 10 13 | ||
192: 17 8 14 | ||
21037: 9 7 18 13 | ||
292: 11 6 16 20 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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().map(|x| *x).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)); | ||
} | ||
} |