-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday17.rs
47 lines (41 loc) · 1.09 KB
/
day17.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
use std::fs;
pub(crate) fn day17() {
let input = fs::read_to_string("input/2017/day17/input.txt").unwrap().trim().parse::<usize>().unwrap();
println!("{}", spinlock(input));
println!("{}", spinlock_more(input));
}
fn spinlock(moves: usize) -> usize {
let mut buffer = vec![0];
let mut pos = 0;
for i in 1..=2017 {
pos = (pos + moves) % i;
buffer.insert(pos + 1, i);
pos += 1;
}
buffer[(pos + 1) % buffer.len()]
}
fn spinlock_more(moves: usize) -> usize {
let mut ans = 0;
let mut pos = 0;
for i in 1..=50000000 {
pos = (pos + moves) % i;
if pos == 0 { ans = i }
pos += 1;
}
ans
}
#[cfg(test)]
mod day17_tests {
use std::fs;
use crate::y2017::day17::{spinlock, spinlock_more};
#[test]
fn test_works() {
assert_eq!(638, spinlock(3));
}
#[test]
fn input_works() {
let input = fs::read_to_string("input/2017/day17/input.txt").unwrap().trim().parse::<usize>().unwrap();
assert_eq!(204, spinlock(input));
assert_eq!(28954211, spinlock_more(input));
}
}