Function aoc::y15d04::y15d04

source ·
pub fn y15d04(input: String, leading_zeros: u32) -> Option<u64>
Expand description

The solution for the day four challenge.

This function takes the input string and the number of leading zeros to find as arguments. The logic is otherwise fairly simple, as described above we batch the work into threads. Each thread works on its chunk of 100,000 numbers combining the input string and each number one after the other to compute the MD5 hash until it either finds a hash that has the correct number of leading zeros or it exhausts its numbers. After all threads have finished we check to see if any results were found and if so we add them to a vector, sort it, and return the smallest value (this is important because in theory two threads could find a number that results in a has with the correct number of leading zeros and we want the smallest of those numbers). If no answer was found we spawn another batch of the threads and continue in this way until we either find a match or reach the maximum integer size (in which case we will return None).

§Example

let input = "a"; // probably read this from the input file...
assert_eq!(y15d04(input.to_string(), 1), Some(27));