summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorReiner Herrmann <reiner@reiner-h.de>2022-10-02 21:48:53 +0200
committerReiner Herrmann <reiner@reiner-h.de>2022-10-02 21:48:53 +0200
commitf3833dc637840fca55f253c9f362641499aa42be (patch)
treeda2a8001fe112d604e0a693946b4e29fcb84d9bb
parentf582b42c798f2f9f80e41e6bcc7ce4d637a239e7 (diff)
day17
-rw-r--r--inputs/day1720
-rw-r--r--src/bin/day17.rs41
-rw-r--r--src/lib.rs9
3 files changed, 70 insertions, 0 deletions
diff --git a/inputs/day17 b/inputs/day17
new file mode 100644
index 0000000..6b25a72
--- /dev/null
+++ b/inputs/day17
@@ -0,0 +1,20 @@
+33
+14
+18
+20
+45
+35
+16
+35
+1
+13
+18
+13
+50
+44
+48
+6
+24
+41
+30
+42
diff --git a/src/bin/day17.rs b/src/bin/day17.rs
new file mode 100644
index 0000000..1f3d009
--- /dev/null
+++ b/src/bin/day17.rs
@@ -0,0 +1,41 @@
+use itertools::Itertools;
+
+fn main() {
+ let input = advent::read_numbers(17);
+ println!("17a: {}", container_fittings(&input, 150));
+ println!("17b: {}", minimum_containers(&input, 150));
+}
+
+fn container_fittings(input: &[u16], target: u16) -> usize {
+ input.iter()
+ .powerset()
+ .filter(|containers| containers.iter().copied().sum::<u16>() == target)
+ .count()
+}
+
+fn minimum_containers(input: &[u16], target: u16) -> usize {
+ let min_count = input.iter()
+ .powerset()
+ .filter(|containers| containers.iter().copied().sum::<u16>() == target)
+ .map(|containers| containers.len())
+ .min()
+ .unwrap();
+
+ input.iter()
+ .powerset()
+ .filter(|containers| containers.len() == min_count)
+ .filter(|containers| containers.iter().copied().sum::<u16>() == target)
+ .count()
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test() {
+ let input = [20, 15, 10, 5, 5];
+ assert_eq!(container_fittings(&input, 25), 4);
+ assert_eq!(minimum_containers(&input, 25), 3);
+ }
+}
diff --git a/src/lib.rs b/src/lib.rs
index 501b2f7..f0199c7 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,3 +1,6 @@
+use std::str::FromStr;
+use std::fmt::Debug;
+
pub fn read_file(day: u8) -> String {
let filename = format!("inputs/day{}", day);
std::fs::read_to_string(filename).unwrap()
@@ -8,3 +11,9 @@ pub fn read_lines(day: u8) -> Vec<String> {
.map(String::from)
.collect()
}
+
+pub fn read_numbers<T: FromStr>(day: u8) -> Vec<T> where <T as FromStr>::Err: Debug {
+ read_lines(day).iter()
+ .map(|n| n.parse::<T>().unwrap())
+ .collect::<Vec<T>>()
+}