diff options
Diffstat (limited to '06/src/part-2.rs')
-rw-r--r-- | 06/src/part-2.rs | 49 |
1 files changed, 49 insertions, 0 deletions
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); +} |