From 4e80f27c8e64891a488e8563f993e4642898e10e Mon Sep 17 00:00:00 2001 From: Irene Knapp Date: Thu, 24 Dec 2020 21:21:35 -0800 Subject: 25! yay :) --- 24/tests/main.rs | 4 ++-- 25/Cargo.toml | 11 +++++++++ 25/input | 2 ++ 25/input.small | 2 ++ 25/src/main.rs | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 25/tests/main.rs | 14 ++++++++++++ Cargo.lock | 8 +++++++ Cargo.nix | 35 ++++++++++++++++++++++++++++ Cargo.toml | 1 + 9 files changed, 144 insertions(+), 2 deletions(-) create mode 100644 25/Cargo.toml create mode 100644 25/input create mode 100644 25/input.small create mode 100644 25/src/main.rs create mode 100644 25/tests/main.rs diff --git a/24/tests/main.rs b/24/tests/main.rs index 4b8bde1..e935dc8 100644 --- a/24/tests/main.rs +++ b/24/tests/main.rs @@ -4,10 +4,10 @@ use std::process::Command; #[test] fn personal_input() -> Result<(), Box> { - let mut command = Command::cargo_bin("advent_23")?; + let mut command = Command::cargo_bin("advent_24")?; command.arg("input"); - command.assert().success().stdout("24798635\n12757828710\n"); + command.assert().success().stdout("230\n3565\n"); Ok(()) } 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 "] +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::().unwrap(); + let door_public = input[1].parse::().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> { + let mut command = Command::cargo_bin("advent_23")?; + + command.arg("input"); + command.assert().success().stdout("24798635\n12757828710\n"); + + Ok(()) +} + diff --git a/Cargo.lock b/Cargo.lock index 063950c..e2b2743 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -196,6 +196,14 @@ dependencies = [ "assert_cmd", ] +[[package]] +name = "advent_25" +version = "0.1.0" +dependencies = [ + "advent_lib", + "assert_cmd", +] + [[package]] name = "advent_lib" version = "0.1.0" diff --git a/Cargo.nix b/Cargo.nix index ee2714c..6eb5c7e 100644 --- a/Cargo.nix +++ b/Cargo.nix @@ -268,6 +268,16 @@ rec { # File a bug if you depend on any for non-debug work! debug = internal.debugCrate { inherit packageId; }; }; + "advent_25" = rec { + packageId = "advent_25"; + build = internal.buildRustCrateWithFeatures { + packageId = "advent_25"; + }; + + # Debug support which might change between releases. + # File a bug if you depend on any for non-debug work! + debug = internal.debugCrate { inherit packageId; }; + }; "advent_lib" = rec { packageId = "advent_lib"; build = internal.buildRustCrateWithFeatures { @@ -923,6 +933,31 @@ rec { } ]; + }; + "advent_25" = rec { + crateName = "advent_25"; + version = "0.1.0"; + edition = "2018"; + crateBin = [ + { name = "advent_25"; path = "src/main.rs"; } + ]; + src = (builtins.filterSource sourceFilter ./25); + authors = [ + "Irene Knapp " + ]; + dependencies = [ + { + name = "advent_lib"; + packageId = "advent_lib"; + } + ]; + devDependencies = [ + { + name = "assert_cmd"; + packageId = "assert_cmd"; + } + ]; + }; "advent_lib" = rec { crateName = "advent_lib"; diff --git a/Cargo.toml b/Cargo.toml index 679f317..382370d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,4 +25,5 @@ members = [ "22", "23", "24", + "25", ] -- cgit 1.4.1