summaryrefslogtreecommitdiff
path: root/2019/rust/day16/src/main.rs
blob: 543be535a7a5b41870842016c32d827d0cda4f8b (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use std::io::Read;

struct FlawedPattern {
    base: Vec<i8>,
    repeat: usize,
    pos: usize,
}

impl FlawedPattern {
    fn new(base: Vec<i8>, repeat: usize) -> FlawedPattern {
        FlawedPattern {
            base,
            repeat,
            pos: 0,
        }
    }
}

impl Iterator for FlawedPattern {
    type Item = i8;

    fn next(&mut self) -> Option<Self::Item> {
        let ret = self.base[(self.pos / (self.repeat + 1)) % self.base.len()];
        self.pos += 1;

        Some(ret)
    }
}

fn read_input() -> Vec<i8> {
    let stdin = std::io::stdin();

    stdin.lock().bytes().map(|byte| {
        match byte {
            Ok(b) => if b >= b'0' && b <= b'9' {
                (b-b'0') as i8
            } else if b == b'\n' || b == b'\r' {
                -1
            } else {
                panic!("Invalid digit: {:?}", b);
            },
            _ => panic!("Invalid indata. {:?}", byte),
        }
    }).filter(|n| *n >= 0).map(|n| n as i8).collect()
}

fn calculate_digits(input: &Vec<i8>) -> Vec<i8> {
    input.iter().rev()
        .scan(0, |s, n| { *s += *n; *s %= 10; Some(*s)})
        .collect::<Vec<i8>>().into_iter()
        .rev().map(|n| n.abs() % 10)
        .collect()
}

fn calculate_digit(input: &Vec<i8>, n: usize) -> i8 {
    let base_pattern = vec![0, 1, 0, -1];

    let fft = FlawedPattern::new(base_pattern, n);
    let factors: Vec<i8> = input.iter()
        .zip(fft.skip(1).take(input.len()).collect::<Vec<i8>>())
        .map(|(n, m)| (*n)*m).collect();

    (factors.iter().map(|n| i64::from(*n)).sum::<i64>().abs() % 10) as i8
}

fn main() {
    let input = read_input();
    let mut num = input.clone();

    for _ in 0..100 {
        num = (0..num.len())
            .map(|n| calculate_digit(&num, n))
            .collect();
    }
    let part1: usize = num.iter().take(8).fold(0, |r, n| r*10 + (*n as usize));
    println!("part1: {}", part1);

    num = input;
    let offset = num.iter().take(7).fold(0, |r, n| r*10 + (*n as usize));
    let repeat = 10000;
    let len = num.len();
    num = num.into_iter().cycle().take(len*repeat).skip(offset).collect();

    for _ in 0..100 { num = calculate_digits(&num); }
    let part2: usize = num.iter().take(8).fold(0, |r, n| r*10 + (*n as usize));
    println!("part2: {}", part2);
}