2015: day04: part 2
This commit is contained in:
parent
33b89f6269
commit
a5911369e5
|
@ -7,6 +7,7 @@ const INPUT: &str = include_str!("../input/day04.txt");
|
|||
|
||||
pub fn run() -> Result<()> {
|
||||
println!("part 1: {}", part1(INPUT)?);
|
||||
println!("part 2: {}", part2(INPUT)?);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -31,6 +32,26 @@ fn part1(input: &str) -> Result<u64> {
|
|||
Err(err!("couldn't find a suitable number"))
|
||||
}
|
||||
|
||||
fn part2(input: &str) -> Result<u64> {
|
||||
let input = input.trim_end();
|
||||
let mut content = String::from(input);
|
||||
|
||||
let mut hasher = Md5::new();
|
||||
|
||||
for i in 0.. {
|
||||
content.truncate(input.len());
|
||||
content.extend(i.to_string().chars());
|
||||
|
||||
hasher.input(&content);
|
||||
let res = hasher.result_reset();
|
||||
if &res[..3] == &[0, 0, 0] {
|
||||
return Ok(i);
|
||||
}
|
||||
}
|
||||
|
||||
Err(err!("couldn't find a suitable number"))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
@ -47,4 +68,10 @@ mod tests {
|
|||
fn part1_real() {
|
||||
assert_eq!(part1(INPUT).unwrap(), 282749);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore] // takes too long!
|
||||
fn part2_real() {
|
||||
assert_eq!(part2(INPUT).unwrap(), 9962624);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue