-
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
Showing
7 changed files
with
95 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use core::f64; | ||
use std::iter; | ||
|
||
use itertools::Itertools; | ||
use regex::Regex; | ||
|
||
pub mod part_1; | ||
pub mod part_2; | ||
|
||
pub fn parse(input: &str) -> Vec<(u64, Vec<u64>)> { | ||
let re_groups = Regex::new(r"(?P<test_val>\d+): (?P<nums>[\d ]+)").unwrap(); | ||
re_groups | ||
.captures_iter(input) | ||
.map(|cap| { | ||
let test_val = cap["test_val"].parse().unwrap(); | ||
let nums = cap["nums"].split(' ').map(|x| x.parse().unwrap()).collect_vec(); | ||
(test_val, nums) | ||
}) | ||
.collect_vec() | ||
} | ||
|
||
pub fn solutions<'a, 'b, 'c>( | ||
vals: &'a [u64], | ||
ops: &'b [fn(u64, u64) -> u64], | ||
) -> Box<dyn Iterator<Item = u64> + 'c> | ||
where | ||
'a: 'c, | ||
'b: 'c, | ||
{ | ||
match vals.len() { | ||
1 => Box::new(iter::once(vals[0])), | ||
n => { | ||
let val = vals[n - 1]; | ||
Box::new(ops.iter().flat_map(move |op| { | ||
let lefts = solutions(&vals[..n - 1], ops); | ||
lefts.map(move |l| op(l, val)) | ||
})) | ||
} | ||
} | ||
} |
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,21 @@ | ||
use std::iter; | ||
|
||
use itertools::Itertools; | ||
|
||
use super::{parse, solutions}; | ||
|
||
#[cfg(test)] | ||
mod test; | ||
|
||
pub fn solve(input: &str) -> u64 { | ||
let equations = parse(input); | ||
const OPS: [fn(u64, u64) -> u64; 2] = [ | ||
u64::saturating_mul, | ||
u64::saturating_add | ||
]; | ||
equations | ||
.iter() | ||
.filter(|eq| solutions(&eq.1, &OPS).any(|sol| sol == eq.0)) | ||
.map(|eq| eq.0) | ||
.sum() | ||
} |
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,16 @@ | ||
use super::solve; | ||
use crate::include_using_path; | ||
|
||
#[test] | ||
fn dummy_input() { | ||
let input = include_using_path!("dummy_input.txt"); | ||
let result = solve(input); | ||
assert_eq!(result, 3749); | ||
} | ||
|
||
#[test] | ||
fn solve_puzzle() { | ||
let input = include_using_path!("input.txt"); | ||
let result = solve(input); | ||
println!("Result: {}", result); | ||
} |
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