summaryrefslogtreecommitdiff
path: root/src/day_6.rs
blob: c9d7d86d688d5dcc1b3c632925aa176b60d06e37 (plain) (blame)
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
#[derive(Copy, Clone, Debug)]
struct Fish(u8);

fn simulate(swarm: &[Fish], days: usize) -> usize
{
    let mut new_count = [0; 2];
    let mut count = [0; 7];
    for fish in swarm {
        count[fish.0 as usize] += 1;
    }

    let mut day = 0;
    let mut birth = 0;
    for i in 0..days {
        let today = count[day] + new_count[birth];
        new_count[birth] = count[day];
        count[day] = today;
        day = (day + 1) % 7;
        birth = (birth + 1) % 2;
    }

    new_count.into_iter().sum::<usize>() + count.into_iter().sum::<usize>()
}

fn part1(swarm: &[Fish])
{
    println!("Solution to part 1: {}", simulate(swarm, 80));
}

fn part2(swarm: &[Fish])
{
    println!("Solution to part 2: {}", simulate(swarm, 256));
}

pub fn run(input: Vec<String>)
{
    let initial_swarm: Vec<Fish> = input
        .iter()
        .map(|s| s.split(',').map(|s| Fish::new(s.parse().unwrap())))
        .flatten()
        .collect();

    part1(&initial_swarm);
    part2(&initial_swarm);
}

impl Fish
{
    pub fn new(timer: u8) -> Self { Self(timer) }
}

impl Default for Fish
{
    fn default() -> Self { Self(8) }
}