-
Notifications
You must be signed in to change notification settings - Fork 0
/
day4.rs
67 lines (57 loc) · 1.68 KB
/
day4.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// vi: set shiftwidth=4 tabstop=4 expandtab:
use common::input::check_answer;
use common::input::get_answers;
use common::input::get_first_line_from_file;
use std::time::Instant;
const INPUT_FILEPATH: &str = "../resources/year2015_day4_input.txt";
const ANSWERS_FILEPATH: &str = "../resources/year2015_day4_answer.txt";
type Int = u32;
const SKIP_SLOW: bool = true;
fn digest_starts_with(data: &str, prefix: &str) -> bool {
let digest = format!("{:?}", md5::compute(data));
digest.starts_with(prefix)
}
fn find_coin(data: &str, prefix: &str) -> Int {
for i in 0.. {
let s = format!("{data}{i}");
if digest_starts_with(&s, prefix) {
return i;
}
}
panic!("No value found");
}
fn part1(input: &str) -> Int {
find_coin(input, "00000")
}
fn part2(input: &str) -> Int {
find_coin(input, "000000")
}
fn main() {
let before = Instant::now();
let data = get_first_line_from_file(INPUT_FILEPATH);
let (ans, ans2) = get_answers(ANSWERS_FILEPATH);
let solved = true;
let res = part1(&data);
check_answer(&res.to_string(), ans, solved);
if !SKIP_SLOW {
let res2 = part2(&data);
check_answer(&res2.to_string(), ans2, solved);
}
println!("Elapsed time: {:.2?}", before.elapsed());
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_digest_starts_with() {
assert!(digest_starts_with("abcdef609043", "00000"));
assert!(!digest_starts_with("abcdef609043", "000000"));
}
#[test]
fn test_find_coin() {
assert_eq!(find_coin("abcdef", "00000"), 609_043);
if !SKIP_SLOW {
assert_eq!(find_coin("pqrstuv", "00000"), 1_048_970);
}
}
}