summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGard Spreemann <gspr@nonempty.org>2021-12-08 12:27:27 +0100
committerGard Spreemann <gspr@nonempty.org>2021-12-08 12:27:27 +0100
commitd49d6dabc7c66263ae630de07a5ccf23a5c66c76 (patch)
treef8cb6fbb2aa8fad6b5df15cbbaacc2c68405deb8
parentb7edc77a1a0d2aeffcd1c5edd782b76f18573fc2 (diff)
Day 6
-rw-r--r--06/Cargo.toml15
-rw-r--r--06/input.txt1
-rw-r--r--06/src/part-1.rs40
-rw-r--r--06/src/part-2.rs49
-rw-r--r--06/test.txt2
5 files changed, 107 insertions, 0 deletions
diff --git a/06/Cargo.toml b/06/Cargo.toml
new file mode 100644
index 0000000..29ca8d8
--- /dev/null
+++ b/06/Cargo.toml
@@ -0,0 +1,15 @@
+[package]
+name = "day-06"
+version = "0.1.0"
+authors = ["Gard Spreemann <gspr@nonempty.org>"]
+edition = "2021"
+
+[[bin]]
+name = "part-1"
+path = "src/part-1.rs"
+
+[[bin]]
+name = "part-2"
+path = "src/part-2.rs"
+
+[dependencies]
diff --git a/06/input.txt b/06/input.txt
new file mode 100644
index 0000000..3e35679
--- /dev/null
+++ b/06/input.txt
@@ -0,0 +1 @@
+4,1,1,4,1,2,1,4,1,3,4,4,1,5,5,1,3,1,1,1,4,4,3,1,5,3,1,2,5,1,1,5,1,1,4,1,1,1,1,2,1,5,3,4,4,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,5,1,1,1,4,1,2,3,5,1,2,2,4,1,4,4,4,1,2,5,1,2,1,1,1,1,1,1,4,1,1,4,3,4,2,1,3,1,1,1,3,5,5,4,3,4,1,5,1,1,1,2,2,1,3,1,2,4,1,1,3,3,1,3,3,1,1,3,1,5,1,1,3,1,1,1,5,4,1,1,1,1,4,1,1,3,5,4,3,1,1,5,4,1,1,2,5,4,2,1,4,1,1,1,1,3,1,1,1,1,4,1,1,1,1,2,4,1,1,1,1,3,1,1,5,1,1,1,1,1,1,4,2,1,3,1,1,1,2,4,2,3,1,4,1,2,1,4,2,1,4,4,1,5,1,1,4,4,1,2,2,1,1,1,1,1,1,1,1,1,1,1,4,5,4,1,3,1,3,1,1,1,5,3,5,5,2,2,1,4,1,4,2,1,4,1,2,1,1,2,1,1,5,4,2,1,1,1,2,4,1,1,1,1,2,1,1,5,1,1,2,2,5,1,1,1,1,1,2,4,2,3,1,2,1,5,4,5,1,4
diff --git a/06/src/part-1.rs b/06/src/part-1.rs
new file mode 100644
index 0000000..5a1660e
--- /dev/null
+++ b/06/src/part-1.rs
@@ -0,0 +1,40 @@
+use std::io::{BufRead};
+
+pub fn main() {
+ let args: Vec<String> = std::env::args().collect();
+ if args.len() != 2 { panic!("Need exactly 1 argument"); }
+ let num_days: usize = args[1].parse().expect("Need number of days as argument");
+
+ let mut stdin = std::io::stdin();
+ let mut handle = stdin.lock();
+
+ let input: String = {
+ let mut buf = String::new();
+ handle.read_line(&mut buf).unwrap();
+ String::from(buf.trim_end())
+ };
+
+ let mut fishes: Vec<u64> = input.split(',').map(|w| w.parse().expect("Malformed input")).collect();
+
+ for i in 0..num_days {
+ let mut new_fish_count: usize = 0;
+ for fish in (&mut fishes).into_iter() {
+ if *fish == 0 {
+ new_fish_count += 1;
+ *fish = 6;
+ }
+ else { *fish -= 1; }
+ }
+ fishes.extend(std::iter::repeat(8).take(new_fish_count));
+
+ /*
+ print!("After day {:>width$}: ", i, width = format!("{}", num_days).len());
+ for fish in (& fishes).into_iter() {
+ print!("{} ", *fish);
+ }
+ println!("");
+ */
+ }
+
+ println!("After {} days there are {} fishies", num_days, fishes.len());
+}
diff --git a/06/src/part-2.rs b/06/src/part-2.rs
new file mode 100644
index 0000000..37d095e
--- /dev/null
+++ b/06/src/part-2.rs
@@ -0,0 +1,49 @@
+use std::io::{BufRead};
+
+#[derive(Clone, Copy)]
+struct FishSchool {
+ state: u64,
+ size: usize
+}
+
+pub fn main() {
+ let args: Vec<String> = std::env::args().collect();
+ if args.len() != 2 { panic!("Need exactly 1 argument"); }
+ let num_days: usize = args[1].parse().expect("Need number of days as argument");
+
+ let mut stdin = std::io::stdin();
+ let mut handle = stdin.lock();
+
+ let input: String = {
+ let mut buf = String::new();
+ handle.read_line(&mut buf).unwrap();
+ String::from(buf.trim_end())
+ };
+
+ let mut schools: Vec<FishSchool> = input.split(',').map(|w| FishSchool {
+ state: w.parse().expect("Malformed input"),
+ size: 1} ).collect();
+
+ for i in 0..num_days {
+ let mut new_fish_count: usize = 0;
+ for school in (&mut schools).into_iter() {
+ if school.state == 0 {
+ new_fish_count += school.size;
+ school.state = 6;
+ }
+ else { school.state -= 1; }
+ }
+ schools.push(FishSchool { state: 8, size: new_fish_count });
+
+ /*
+ print!("After day {:>width$}: ", i, width = format!("{}", num_days).len());
+ for school in (& schools).into_iter() {
+ for _ in 0..school.size { print!("{} ", school.state); }
+ }
+ println!("");
+ */
+ }
+
+ let n: usize = schools.into_iter().map(|school| school.size).sum();
+ println!("After {} days there are {} fishies", num_days, n);
+}
diff --git a/06/test.txt b/06/test.txt
new file mode 100644
index 0000000..9792d05
--- /dev/null
+++ b/06/test.txt
@@ -0,0 +1,2 @@
+3,4,3,1,2
+