Don't compute unnecessary info.

This commit is contained in:
2020-12-25 10:42:41 +01:00
parent 40a9cb63ff
commit 601d6fdd48

View File

@@ -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<u32> {
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 {