summaryrefslogtreecommitdiff
path: root/2016/aoc11/src/main.rs
blob: 39a6e2f7db47d2fc2e47fe22b370ff0fcc730d7c (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166

use std::collections::HashMap;

//const MICROS: usize = 2; //example
const MICROS: usize = 7;

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
struct State {
    microchips: [[bool; MICROS]; 4],
    generators: [[bool; MICROS]; 4],
    elevator: usize
}

impl State {
    fn is_final(&self) -> bool {
        let floor = 3;
        
        for i in 0..MICROS {
            if !self.microchips[floor][i] || !self.generators[floor][i] {
                return false;
            }
        }
        true
    }

    fn is_safe(&self) -> bool {
        for floor in 0..4 {
            for i in 0..MICROS {
                for j in 0..MICROS {
                    //need to be on same floor, if any other micro is there
                    if self.generators[floor][j] && !self.generators[floor][i] && self.microchips[floor][i] {
                        return false;
                    }
                }
            }
        }
        true
    }

    fn valid_moves(&self) -> Vec<State> {
        let mut moves = Vec::new();

        let mut new_floors = Vec::new();
        if self.elevator > 0 {
            new_floors.push(self.elevator-1);
        }
        if self.elevator < 3 {
            new_floors.push(self.elevator+1);
        }
        for &new_floor in new_floors.iter() {

            //items to take can be: one micro, one generator, two micros, two generator, one of each

            //one micro
            for i in 0..MICROS {
                if self.microchips[self.elevator][i] {
                    moves.push(self.move_micro(i, self.elevator, new_floor));
                }
            }
            //one generator
            for i in 0..MICROS {
                if self.generators[self.elevator][i] {
                    moves.push(self.move_generator(i, self.elevator, new_floor));
                }
            }
            //two micros
            for i in 0..MICROS {
                for j in i+1..MICROS {
                    if self.microchips[self.elevator][i] && self.microchips[self.elevator][j] {
                        moves.push(self.move_micro(i, self.elevator, new_floor).move_micro(j, self.elevator, new_floor));
                    }
                }
            }
            //two generators
            for i in 0..MICROS {
                for j in i+1..MICROS {
                    if self.generators[self.elevator][i] && self.generators[self.elevator][j] {
                        moves.push(self.move_generator(i, self.elevator, new_floor).move_generator(j, self.elevator, new_floor));
                    }
                }
            }
            //one of each
            for i in 0..MICROS {
                for j in 0..MICROS {
                    if self.microchips[self.elevator][i] && self.generators[self.elevator][j] {
                        moves.push(self.move_micro(i, self.elevator, new_floor).move_generator(j, self.elevator, new_floor));
                    }
                }
            }
        }

        moves.iter().filter(|x| x.is_safe()).cloned().collect()
    }

    fn move_micro(&self, micro: usize, floor: usize, new_floor: usize) -> State {
        let mut new_state = self.clone();
        new_state.microchips[floor][micro] = false;
        new_state.microchips[new_floor][micro] = true;
        new_state.elevator = new_floor;
        new_state
    }
    fn move_generator(&self, gen: usize, floor: usize, new_floor: usize) -> State {
        let mut new_state = self.clone();
        new_state.generators[floor][gen] = false;
        new_state.generators[new_floor][gen] = true;
        new_state.elevator = new_floor;
        new_state
    }
}

fn main() {
    //Stronium, plutonium, thulium, ruthenium, curium, electrium, dilithium
    let initial = State {
        microchips:
        [[true, true, false, false, false, true, true],
         [false, false, false, true, true, false, false],
         [false, false, true, false, false, false, false],
         [false, false, false, false, false, false, false]],
        generators:
        [[true, true, false, false, false, true, true],
         [false, false, true, true, true, false, false],
         [false, false, false, false, false, false, false],
         [false, false, false, false, false, false, false]],
        elevator: 0
    };

    /*
    //example
    let initial = State {
        microchips:
        [[true, true],
         [false, false],
         [false, false],
         [false, false]],
        generators:
        [[false, false],
         [true, false],
         [false, true],
         [false, false]],
        elevator: 0
    };
    */
    
    let mut states: HashMap<State, u32> = HashMap::new();
    states.insert(initial, 0);

    let mut moves = 0;
    loop {
        if states.iter().any(|(state, _)| state.is_final()) {
            break;
        }

        let new_states: Vec<State> = states.iter().filter(|&(_, &x)| x == moves).flat_map(|(state, _)| state.valid_moves()).collect();

        moves += 1;

        for state in new_states {
            if !states.contains_key(&state) {
                states.insert(state, moves);
            }
        }
    }
    
    
    println!("Moves required: {}", moves);
}