Skip to content

Commit

Permalink
day_7 part_1
Browse files Browse the repository at this point in the history
  • Loading branch information
0qln committed Dec 7, 2024
1 parent f89e84e commit fbf3a3b
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2021"

[dependencies]
anyhow = "1.0.94"
ilog = "1.0.1"
itertools = "0.13.0"
regex = "1.11.1"

Expand Down
40 changes: 40 additions & 0 deletions src/day_7/mod.rs
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))
}))
}
}
}
9 changes: 9 additions & 0 deletions src/day_7/part_1/dummy_input.txt
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
21 changes: 21 additions & 0 deletions src/day_7/part_1/mod.rs
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()
}
16 changes: 16 additions & 0 deletions src/day_7/part_1/test.rs
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);
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod day_3;
mod day_4;
mod day_5;
mod day_6;
mod day_7;

macro_rules! include_using_path {
($relative_path:expr) => {{
Expand Down

0 comments on commit fbf3a3b

Please sign in to comment.