From 84f096f97b40e7f7394b72e3b54b19804e9ab6ac Mon Sep 17 00:00:00 2001 From: Irene Knapp Date: Sun, 5 Dec 2021 21:23:12 -0800 Subject: 06 --- 06/Cargo.toml | 11 +++++++++ 06/input | 1 + 06/src/main.rs | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 06/tests/main.rs | 17 ++++++++++++++ Cargo.lock | 8 +++++++ Cargo.toml | 1 + 6 files changed, 106 insertions(+) create mode 100644 06/Cargo.toml create mode 100644 06/input create mode 100644 06/src/main.rs create mode 100644 06/tests/main.rs diff --git a/06/Cargo.toml b/06/Cargo.toml new file mode 100644 index 0000000..e5303bd --- /dev/null +++ b/06/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "advent_06" +version = "0.1.0" +authors = ["Irene Knapp "] +edition = "2018" + +[dependencies] +advent_lib = { path = "../lib" } + +[dev-dependencies] +assert_cmd = "0.10" diff --git a/06/input b/06/input new file mode 100644 index 0000000..e9ab820 --- /dev/null +++ b/06/input @@ -0,0 +1 @@ +1,1,1,1,2,1,1,4,1,4,3,1,1,1,1,1,1,1,1,4,1,3,1,1,1,5,1,3,1,4,1,2,1,1,5,1,1,1,1,1,1,1,1,1,1,3,4,1,5,1,1,1,1,1,1,1,1,1,3,1,4,1,1,1,1,3,5,1,1,2,1,1,1,1,4,4,1,1,1,4,1,1,4,2,4,4,5,1,1,1,1,2,3,1,1,4,1,5,1,1,1,3,1,1,1,1,5,5,1,2,2,2,2,1,1,2,1,1,1,1,1,3,1,1,1,2,3,1,5,1,1,1,2,2,1,1,1,1,1,3,2,1,1,1,4,3,1,1,4,1,5,4,1,4,1,1,1,1,1,1,1,1,1,1,2,2,4,5,1,1,1,1,5,4,1,3,1,1,1,1,4,3,3,3,1,2,3,1,1,1,1,1,1,1,1,2,1,1,1,5,1,3,1,4,3,1,3,1,5,1,1,1,1,3,1,5,1,2,4,1,1,4,1,4,4,2,1,2,1,3,3,1,4,4,1,1,3,4,1,1,1,2,5,2,5,1,1,1,4,1,1,1,1,1,1,3,1,5,1,2,1,1,1,1,1,4,4,1,1,1,5,1,1,5,1,2,1,5,1,1,1,1,1,1,1,1,1,1,1,1,3,2,4,1,1,2,1,1,3,2 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 +} + diff --git a/06/tests/main.rs b/06/tests/main.rs new file mode 100644 index 0000000..ad291d8 --- /dev/null +++ b/06/tests/main.rs @@ -0,0 +1,17 @@ +use assert_cmd::prelude::*; +//use predicates::prelude::*; +use std::process::Command; + + +#[test] +fn personal_input() -> Result<(), Box> { + let mut command = Command::cargo_bin("advent_05")?; + + command.arg("input"); + command.assert().success().stdout( + "4655\n\ + 20500\n"); + + Ok(()) +} + diff --git a/Cargo.lock b/Cargo.lock index 288f919..8f6d6d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -40,6 +40,14 @@ dependencies = [ "assert_cmd", ] +[[package]] +name = "advent_06" +version = "0.1.0" +dependencies = [ + "advent_lib", + "assert_cmd", +] + [[package]] name = "advent_lib" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 682e0be..6a830cc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,5 @@ members = [ "03", "04", "05", + "06", ] -- cgit 1.4.1