~ttt/aoc2015

bbae8874fc82cdd41f3bd0dfee590b6f9a628c34 — Tomasz Kłak 4 years ago 2a9b654
Day 17
2 files changed, 33 insertions(+), 0 deletions(-)

A day17/Cargo.toml
A day17/src/main.rs
A day17/Cargo.toml => day17/Cargo.toml +9 -0
@@ 0,0 1,9 @@
[package]
name = "day17"
version = "0.1.0"
authors = ["Tomasz Kłak <tomasz@tomaszklak.pl>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

A day17/src/main.rs => day17/src/main.rs +24 -0
@@ 0,0 1,24 @@
fn powerset<T>(s: &[T]) -> Vec<Vec<T>> where T: Clone {
    (0..2usize.pow(s.len() as u32)).map(|i| {
        s.iter().enumerate().filter(|&(t, _)| (i >> t) % 2 == 1)
            .map(|(_, element)| element.clone())
            .collect()
    }).collect()
}   

fn count(target: i32, cont: &[i32]) -> usize {
    powerset(cont).iter().filter(|v| v.iter().sum::<i32>() == target).count()
}

fn count2(target: i32, cont: &[i32]) -> usize {
    let all_correct : Vec<_> = powerset(cont).iter().filter(|v| v.iter().sum::<i32>() == target).cloned().collect();
    let min = all_correct.iter().map(|v| v.len()).min().unwrap();
    all_correct.iter().filter(|v| v.len() == min).count()
}

fn main() {
    let input = vec![43, 3, 4, 10, 21, 44, 4, 6, 47, 41, 34, 17, 17, 44, 36, 31, 46, 9, 27, 38];
    dbg!(count(150, &input));
    dbg!(count2(150, &input));

}