-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
51 lines (42 loc) · 1.1 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#![warn(clippy::all, clippy::pedantic, clippy::nursery)]
use md5::Digest;
pub fn main() {
let data = include_str!("input.txt");
println!("Part 1: {}", part_one(data));
println!("Part 2: {}", part_two(data));
}
fn part_one(data: &str) -> usize {
let prefix = data.trim_end_matches('\n');
for i in 0.. {
let input = format!("{prefix}{i}");
if five_leading_zeros(md5::Md5::digest(input)) {
return i;
}
}
0
}
fn part_two(data: &str) -> usize {
let prefix = data.trim_end_matches('\n');
for i in 0.. {
let input = format!("{prefix}{i}");
if six_leading_zeros(md5::Md5::digest(input)) {
return i;
}
}
0
}
fn five_leading_zeros(digest: impl AsRef<[u8]>) -> bool {
digest.as_ref()[..2] == [0, 0] && digest.as_ref()[2] < 16
}
fn six_leading_zeros(digest: impl AsRef<[u8]>) -> bool {
digest.as_ref()[..3] == [0, 0, 0]
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn one() {
assert_eq!(609_043, part_one("abcdef"));
assert_eq!(1_048_970, part_one("pqrstuv"));
}
}