diff options
author | Irene Knapp <ireneista@gmail.com> | 2021-12-05 21:23:12 -0800 |
---|---|---|
committer | Irene Knapp <ireneista@gmail.com> | 2021-12-05 21:23:12 -0800 |
commit | 84f096f97b40e7f7394b72e3b54b19804e9ab6ac (patch) | |
tree | 45fd47ebdcb1fcf67ec02a4cc9bc786f80f4c690 /06 | |
parent | e5db5a78aa0bdaf9066df2c0316663e3365de209 (diff) |
06
Diffstat (limited to '06')
-rw-r--r-- | 06/Cargo.toml | 11 | ||||
-rw-r--r-- | 06/input | 1 | ||||
-rw-r--r-- | 06/src/main.rs | 68 | ||||
-rw-r--r-- | 06/tests/main.rs | 17 |
4 files changed, 97 insertions, 0 deletions
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 <ireneista@gmail.com>"] +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::<i64>()?; + 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<i64>) -> Vec<i64> { + 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>) -> 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<dyn std::error::Error>> { + let mut command = Command::cargo_bin("advent_05")?; + + command.arg("input"); + command.assert().success().stdout( + "4655\n\ + 20500\n"); + + Ok(()) +} + |