From 84f096f97b40e7f7394b72e3b54b19804e9ab6ac Mon Sep 17 00:00:00 2001 From: Irene Knapp Date: Sun, 5 Dec 2021 21:23:12 -0800 Subject: 06 --- 06/src/main.rs | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 06/src/main.rs (limited to '06/src') diff --git a/06/src/main.rs b/06/src/main.rs new file mode 100644 index 0000000..fa48fb0 --- /dev/null +++ b/06/src/main.rs @@ -0,0 +1,68 @@ +use advent_lib::prelude::*; + + +fn main() -> Result<()> { + let mut args = std::env::args(); + if args.len() != 2 { + eprintln!("Usage: advent input"); + } + let _ = args.next(); + let filename = args.next().unwrap(); + + let input = advent_lib::read_lines_file(&filename)?; + + let mut time_buckets = Vec::new(); + for word in input[0].split(',') { + let time = word.parse::()?; + while time_buckets.len() <= time as usize { + time_buckets.push(0); + } + time_buckets[time as usize] += 1; + } + + for _ in 0 .. 80 { + time_buckets = iterate(&time_buckets); + } + + println!("{}", population_size(&time_buckets)); + + for _ in 80 .. 256 { + time_buckets = iterate(&time_buckets); + } + + println!("{}", population_size(&time_buckets)); + + Ok(()) +} + + +fn iterate(old_time_buckets: &Vec) -> Vec { + let mut new_time_buckets = Vec::new(); + + while new_time_buckets.len() <= 8 { + new_time_buckets.push(0); + } + + for (i, n) in old_time_buckets.iter().enumerate() { + if i == 0 { + new_time_buckets[6] += n; + new_time_buckets[8] += n; + } else { + new_time_buckets[i - 1] += n; + } + } + + new_time_buckets +} + + +fn population_size(time_buckets: &Vec) -> i64 { + let mut result = 0; + + for n in time_buckets { + result += n; + } + + result +} + -- cgit 1.4.1