summaryrefslogtreecommitdiff
path: root/d19/src/main.rs
blob: 91d9524ff865228058b2af5fb8f4d28878222798 (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
// Find the next Elf still in the game, to the left of `i'
fn next_elf(elves: &[bool], i: usize) -> usize {
    let mut n = (i + 1) % elves.len();
    while !elves[n] {
        n = (n + 1) % elves.len();
    }
    n
}

fn solve_puzzle(
    num_elves: usize,
    victim1: usize,
    next_victim: fn(&[bool], usize, usize, usize) -> usize,
) -> usize {
    // We only need to store whether an Elf is still in the game;
    // we don't care how many presents each one has.
    let mut elves = [true].repeat(num_elves);

    // Thief and victim indices, respectively
    let mut t = 0;
    let mut v = victim1;

    let mut rem = num_elves;
    while rem > 1 {
        // Perform the act of stealing
        elves[v] = false;
        rem -= 1;

        // Who does the stealing next?
        t = next_elf(&elves, t);

        // Who gets stolen from next?
        v = next_victim(&elves, rem, t, v);
    }

    // Elves should be numbered from 1 instead of 0
    t + 1
}

// Part 1: victim is always to the left of thief.
fn next_victim_part1(elves: &[bool], _rem: usize, nthief: usize, _victim: usize) -> usize {
    next_elf(elves, nthief)
}

// Part 2: victim is always across from thief.
// It's less clear that this function is correct, but it is!
fn next_victim_part2(elves: &[bool], rem: usize, _nthief: usize, victim: usize) -> usize {
    let mut nv = next_elf(elves, victim);
    if rem % 2 == 0 {
        nv = next_elf(elves, nv);
    }
    nv
}

fn main() {
    // My personal puzzle input
    let input = 3014603;

    // Part 1 gives 1834903 for me
    println!(
        "Part 1 solution: {}",
        solve_puzzle(input, 1, next_victim_part1)
    );

    // Part 2 gives 1420280 for me
    println!(
        "Part 2 solution: {}",
        solve_puzzle(input, input / 2, next_victim_part2)
    );
}

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

    #[test]
    fn part1_example1() {
        assert_eq!(solve_puzzle(5, 1, next_victim_part1), 3);
    }

    #[test]
    fn part2_example1() {
        assert_eq!(solve_puzzle(5, 5 / 2, next_victim_part2), 2);
    }
}