From 7357cbdbd4424a06db8fb11546626f97783135a6 Mon Sep 17 00:00:00 2001 From: Irene Knapp Date: Tue, 15 Dec 2020 00:31:26 -0800 Subject: 15 --- 15/Cargo.toml | 11 +++++++++++ 15/src/main.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 15/tests/main.rs | 14 ++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 15/Cargo.toml create mode 100644 15/src/main.rs create mode 100644 15/tests/main.rs (limited to '15') diff --git a/15/Cargo.toml b/15/Cargo.toml new file mode 100644 index 0000000..dcc368c --- /dev/null +++ b/15/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "advent_15" +version = "0.1.0" +authors = ["Irene Knapp "] +edition = "2018" + +[dependencies] +advent_lib = { path = "../lib" } + +[dev-dependencies] +assert_cmd = "0.10" diff --git a/15/src/main.rs b/15/src/main.rs new file mode 100644 index 0000000..12bf0fa --- /dev/null +++ b/15/src/main.rs @@ -0,0 +1,52 @@ +use advent_lib::prelude::*; + +use std::collections::BTreeMap; + + + +fn main() -> Result<()> { + let args = std::env::args(); + if args.len() != 1 { + eprintln!("Usage: advent"); + } + + let starting_numbers = vec![2, 15, 0, 9, 1, 20]; + + let mut history: BTreeMap = BTreeMap::new(); + let mut output = 0; + let mut next_output = 0; + + for i in 0 .. starting_numbers.len() { + output = starting_numbers[i]; + next_output = match history.get(&output) { + Some(previous) => i - previous, + None => 0, + }; + history.insert(output, i); + } + + for i in starting_numbers.len() .. 2020 { + output = next_output; + next_output = match history.get(&output) { + Some(previous) => i - previous, + None => 0, + }; + history.insert(output, i); + } + + println!("{}", output); + + for i in 2020 .. 30000000 { + output = next_output; + next_output = match history.get(&output) { + Some(previous) => i - previous, + None => 0, + }; + history.insert(output, i); + } + + println!("{}", output); + + Ok(()) +} + diff --git a/15/tests/main.rs b/15/tests/main.rs new file mode 100644 index 0000000..51cb39b --- /dev/null +++ b/15/tests/main.rs @@ -0,0 +1,14 @@ +use assert_cmd::prelude::*; +use std::process::Command; + + +#[test] +fn personal_input() -> Result<(), Box> { + let mut command = Command::cargo_bin("advent_15")?; + + command.arg("input"); + command.assert().success().stdout("1280\n651639\n"); + + Ok(()) +} + -- cgit 1.4.1