-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday06.rs
59 lines (52 loc) · 1.58 KB
/
day06.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
use std::collections::HashMap;
use std::fs;
pub(crate) fn day06() {
let input = fs::read_to_string("input/2017/day06/input.txt").unwrap();
let banks = input.split_whitespace().map(|line| line.parse().unwrap()).collect();
let (part_a, part_b) = redistribute(banks);
println!("{}", part_a);
println!("{}", part_b);
}
fn redistribute(mut banks: Vec<usize>) -> (usize, usize) {
let banks_len = banks.len();
let mut ctr = 0;
let mut seen_states = HashMap::new();
seen_states.insert(banks.clone(), ctr);
loop {
let mut pos = 0;
let mut max = usize::MIN;
for i in 0..banks_len {
if banks[i] > max {
pos = i;
max = banks[i];
}
}
banks[pos] = 0;
for i in 1..=max {
banks[(pos + i as usize) % banks_len] += 1;
}
ctr += 1;
if let Some(prev_ctr) = seen_states.insert(banks.clone(), ctr) {
return (ctr, ctr - prev_ctr);
}
}
}
#[cfg(test)]
mod day06_tests {
use std::fs;
use crate::y2017::day06::redistribute;
#[test]
fn redistribute_works() {
let (part_a, part_b) = redistribute(vec![0, 2, 7, 0]);
assert_eq!(5, part_a);
assert_eq!(4, part_b);
}
#[test]
fn input_works() {
let input = fs::read_to_string("input/2017/day06/input.txt").unwrap();
let banks = input.split_whitespace().map(|line| line.parse().unwrap()).collect();
let (part_a, part_b) = redistribute(banks);
assert_eq!(6681, part_a);
assert_eq!(2392, part_b);
}
}