summaryrefslogtreecommitdiff
path: root/2020/src/bin/2020day25.rs
blob: e50bde53ce1b4894c622ebda9fcc4abc762309bc (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
use aoc2020::*;

fn transform(subject_number: u64, loop_size: u64) -> u64 {
    let mut value = 1;
    for _ in 0..loop_size {
        value *= subject_number;
        value %= 20201227;
    }
    value
}

fn determine_loop_size(public: u64) -> u64 {
    // secret loop size is 7 transformed N times until equal to public
    let mut ret = 0;
    let mut value = 1;
    while value != public {
        value *= 7; // Initial subject number
        value %= 20201227;
        ret += 1;
    }
    ret
}

fn part1(input: &[u64]) -> u64 {
    let loops = determine_loop_size(input[0]);
    transform(input[1], loops)
}

fn part2(input: &[u64]) -> u64 {
    part1(input)
}

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

    #[test]
    fn det_loop_sz() {
        assert_eq!(determine_loop_size(5764801), 8);
        assert_eq!(determine_loop_size(17807724), 11);
    }

    #[test]
    fn transform_chk() {
        assert_eq!(transform(17807724, 8), 14897079);
        assert_eq!(transform(5764801, 11), 14897079);
    }

    const TEST_INPUT: &str = r#"5764801
17807724"#;

    #[test]
    fn testcase1() {
        let input = input_as_vec(TEST_INPUT).unwrap();
        assert_eq!(part1(&input), 14897079);
    }

    #[test]
    fn testcase2() {}
}

fn main() -> Result<()> {
    let input = read_input_as_vec(25)?;
    println!("Part 1: {}", part1(&input));
    println!("Part 2: {}", part2(&input));
    Ok(())
}