diff options
Diffstat (limited to '25')
-rw-r--r-- | 25/Cargo.toml | 11 | ||||
-rw-r--r-- | 25/input | 2 | ||||
-rw-r--r-- | 25/input.small | 2 | ||||
-rw-r--r-- | 25/src/main.rs | 69 | ||||
-rw-r--r-- | 25/tests/main.rs | 14 |
5 files changed, 98 insertions, 0 deletions
diff --git a/25/Cargo.toml b/25/Cargo.toml new file mode 100644 index 0000000..746f708 --- /dev/null +++ b/25/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "advent_25" +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/25/input b/25/input new file mode 100644 index 0000000..077ed30 --- /dev/null +++ b/25/input @@ -0,0 +1,2 @@ +14222596 +4057428 diff --git a/25/input.small b/25/input.small new file mode 100644 index 0000000..9cbfc23 --- /dev/null +++ b/25/input.small @@ -0,0 +1,2 @@ +5764801 +17807724 diff --git a/25/src/main.rs b/25/src/main.rs new file mode 100644 index 0000000..1fd0149 --- /dev/null +++ b/25/src/main.rs @@ -0,0 +1,69 @@ +use advent_lib::prelude::*; + +//use std::collections::BTreeSet; +//use std::convert::TryFrom; + + + +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 card_public = input[0].parse::<i64>().unwrap(); + let door_public = input[1].parse::<i64>().unwrap(); + let subject: i64 = 7; + + let mut i = 0; + let mut value = 1; + let mut card_loop_size = None; + let mut door_loop_size = None; + loop { + value = iterate(value, subject); + i += 1; + + if card_loop_size.is_none() { + if value == card_public { + card_loop_size = Some(i); + } + } + + if door_loop_size.is_none() { + if value == door_public { + door_loop_size = Some(i); + } + } + + if card_loop_size.is_some() && door_loop_size.is_some() { + break; + } + } + + let key = transform(door_public, card_loop_size.unwrap()); + + println!("{}", key); + + Ok(()) +} + + +fn iterate(input: i64, subject: i64) -> i64 { + let mut output = input * subject; + output %= 20201227; + output +} + + +fn transform(subject: i64, loop_count: usize) -> i64 { + let mut value = 1; + for _ in 0 .. loop_count { + value = iterate(value, subject); + } + value +} + diff --git a/25/tests/main.rs b/25/tests/main.rs new file mode 100644 index 0000000..4b8bde1 --- /dev/null +++ b/25/tests/main.rs @@ -0,0 +1,14 @@ +use assert_cmd::prelude::*; +use std::process::Command; + + +#[test] +fn personal_input() -> Result<(), Box<dyn std::error::Error>> { + let mut command = Command::cargo_bin("advent_23")?; + + command.arg("input"); + command.assert().success().stdout("24798635\n12757828710\n"); + + Ok(()) +} + |