-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution_2017_15.rs
100 lines (82 loc) · 1.94 KB
/
solution_2017_15.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
type Value = u32;
type Values = (Value, Value);
#[inline]
fn next_value(value: Value, factor: Value) -> Value {
((u64::from(value) * u64::from(factor)) % 2_147_483_647) as Value
}
#[inline]
fn next_1(values: Values) -> Values {
(next_value(values.0, 16807), next_value(values.1, 48271))
}
fn next_value_with_constraint(value: Value, factor: Value, constraint: Value) -> Value {
let mut current = value;
loop {
current = next_value(current, factor);
if current % constraint == 0 {
return current;
}
}
}
#[inline]
fn next_2(values: Values) -> Values {
(
next_value_with_constraint(values.0, 16807, 4),
next_value_with_constraint(values.1, 48271, 8),
)
}
#[inline]
fn last_16_bits(value: Value) -> Value {
value & 0xFFFF
}
#[inline]
fn matches(values: Values) -> bool {
last_16_bits(values.0) == last_16_bits(values.1)
}
fn solve<F>(start: Values, times: usize, next: F) -> usize
where
F: Fn(Values) -> Values,
{
let mut result: usize = 0;
let mut current: Values = start;
for _ in 0 .. times {
current = next(current);
if matches(current) {
result += 1;
}
}
result
}
fn part_1(start: Values) -> usize {
solve(start, 40_000_000, next_1)
}
fn part_2(start: Values) -> usize {
solve(start, 5_000_000, next_2)
}
const DATA: Values = (873, 583);
fn main() {
let result_1 = part_1(DATA);
println!("Part 1: {result_1}");
let result_2 = part_2(DATA);
println!("Part 2: {result_2}");
}
#[cfg(test)]
mod tests {
use super::*;
const TEST: Values = (65, 8921);
#[test]
fn test_solve_1_test() {
assert_eq!(part_1(TEST), 588);
}
#[test]
fn test_solve_1_real() {
assert_eq!(part_1(DATA), 631);
}
#[test]
fn test_solve_2_test() {
assert_eq!(part_2(TEST), 309);
}
#[test]
fn test_solve_2_real() {
assert_eq!(part_2(DATA), 279);
}
}