-
Notifications
You must be signed in to change notification settings - Fork 0
/
day06.rs
59 lines (46 loc) · 1.12 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 aoc_runner_derive::{aoc, aoc_generator};
type Parsed = Vec<usize>;
#[aoc_generator(day6)]
fn parse_input(input: &str) -> Parsed {
input.split(',').map(|s| s.parse().unwrap()).collect()
}
#[aoc(day6, part1)]
fn part1(input: &Parsed) -> usize {
let mut states = input.iter().fold(vec![0; 9], |mut v, n| {
*v.get_mut(*n).unwrap() += 1;
v
});
for _ in 0..80 {
let zero = states.remove(0);
states.push(zero);
states[6] += zero;
}
states.iter().sum()
}
#[aoc(day6, part2)]
fn part2(input: &Parsed) -> usize {
let mut states = input.iter().fold(vec![0; 9], |mut v, n| {
*v.get_mut(*n).unwrap() += 1;
v
});
for _ in 0..256 {
let zero = states.remove(0);
states.push(zero);
states[6] += zero;
}
states.iter().sum()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sample1() {
let input = "3,4,3,1,2";
assert_eq!(part1(&parse_input(input)), 5934);
}
#[test]
fn sample2() {
let input = "3,4,3,1,2";
assert_eq!(part2(&parse_input(input)), 26984457539);
}
}