summaryrefslogtreecommitdiff
path: root/src/bin/day10.rs
blob: 562408bb4bb7582bee731ac8879450bd5b7ac0e7 (plain)
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
fn main() {
    let input = [1,3,2,1,1,3,1,1,1,2];
    println!("10a: {}", lookandsay_times(&input, 40).len());
    println!("10b: {}", lookandsay_times(&input, 50).len());
}

fn lookandsay(input: &[u8]) -> Vec<u8> {
    let mut say = Vec::new();

    let mut count = 1;
    let mut prev = input[0];
    for n in input.iter().skip(1) {
        if *n != prev {
            say.push(count);
            say.push(prev);
            prev = *n;
            count = 0;
        }
        count += 1;
    }
    say.push(count);
    say.push(prev);

    say
}

fn lookandsay_times(input: &[u8], times: u32) -> Vec<u8> {
    let mut data = Vec::from(input);
    for _ in 0..times {
        data = lookandsay(&data);
    }
    data
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test() {
        assert_eq!(lookandsay(&[1,1,1,2,2,1]), [3,1,2,2,1,1]);
        assert_eq!(lookandsay_times(&[1], 5), [3,1,2,2,1,1]);
    }
}