From 601d6fdd48f5b6fdde89c9835a7cc1b917fe58fe Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Fri, 25 Dec 2020 10:42:41 +0100 Subject: [PATCH] Don't compute unnecessary info. --- 2020/src/day25.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/2020/src/day25.rs b/2020/src/day25.rs index 266c4f9..dfa9d19 100644 --- a/2020/src/day25.rs +++ b/2020/src/day25.rs @@ -16,7 +16,7 @@ fn loop_count(public_key: u32) -> u64 { // Based on:https://en.wikipedia.org/wiki/Baby-step_giant-step#C++_algorithm_(C++17) fn discrete_log(g: u32, h: u32, mod_base: u32) -> Option { let m = (mod_base as f64).sqrt().ceil() as u32; - let mut table = HashMap::new(); + let mut table = HashMap::with_capacity(m as usize); let mut e: u32 = 1; for i in 0..m { @@ -65,10 +65,9 @@ impl Solution for Day25 { fn part1(&mut self, input: &mut dyn Read) -> String { let nums: Vec<_> = from_lines(input); - let exponent: u64 = nums.into_iter().map(loop_count).product(); - let result = mod_exp(SUBJECT_NUMBER as u64, exponent, MOD_BASE as u64); + let key_exponent = loop_count(nums[0]); - result.to_string() + mod_exp(nums[1] as u64, key_exponent, MOD_BASE as u64).to_string() } fn part2(&mut self, _input: &mut dyn Read) -> String {