From 4e80f27c8e64891a488e8563f993e4642898e10e Mon Sep 17 00:00:00 2001 From: Irene Knapp Date: Thu, 24 Dec 2020 21:21:35 -0800 Subject: 25! yay :) --- 25/src/main.rs | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 25/src/main.rs (limited to '25/src') 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 +} + -- cgit 1.4.1