summaryrefslogtreecommitdiff
path: root/2016/aoc22/src/main.rs
blob: bd374d50c6b1b12cfdc9f78a8b1739b965f2dbc1 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
extern crate regex;

use std::io::BufReader;
use std::io::prelude::*;
use std::fs::File;
use std::collections::HashMap;
use std::collections::HashSet;

use regex::Regex;

use std::str::FromStr;

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
struct Node {
    x: i8,
    y: i8,
    size: i16,
    used: i16,
    avail: i16,
    blocker: bool
}

impl FromStr for Node {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let reg = Regex::new(r"/dev/grid/node-x(\d+)-y(\d+) +(\d+)T +(\d+)T +(\d+)T +(\d+)%").unwrap();
        let cap = match reg.captures(s) {
            Some(cap) => cap,
            None => return Err("Does not match regex".to_string())
        };
        Ok(Node {
            x: cap.at(1).unwrap().parse().unwrap(),
            y: cap.at(2).unwrap().parse().unwrap(),
            size: cap.at(3).unwrap().parse().unwrap(),
            used: cap.at(4).unwrap().parse().unwrap(),
            avail: cap.at(5).unwrap().parse().unwrap(),
            blocker: false
        })
    }
}

impl Node {
    fn is_viable_pair(&self, other: &Node) -> bool {
        (self.x != other.x || self.y != other.y) && self.used > 0 && self.used <= other.avail
    }

    fn recalculate_avail(&mut self) {
        self.avail = self.size - self.used;
        debug_assert!(self.avail >= 0);
        debug_assert!(self.used >= 0);
    }
}

#[derive(Clone, Hash, PartialEq, Eq)]
struct Grid {
    nodes: Vec<Vec<Node>>,
    goal_x: i8,
    goal_y: i8
}

impl Grid {
    fn new(nodes: Vec<Node>) -> Grid {
        let mut grid_nodes = Vec::new();
        let mut next_col = Vec::new();

        let mut current_x = 0;
        
        for node in nodes {
            if current_x != node.x {
                grid_nodes.push(next_col);
                next_col = Vec::new();
                current_x += 1;
            }
            
            next_col.push(node);
        }
        grid_nodes.push(next_col);
        grid_nodes = Grid::normalize(grid_nodes);

        Grid {
            nodes: grid_nodes,
            goal_x: current_x,
            goal_y: 0
        }
    }

    fn normalize(mut nodes: Vec<Vec<Node>>) -> Vec<Vec<Node>> {
        for x in 0..nodes.len() {
            for y in 0..nodes[x].len() {
                nodes[x][y].size = ((nodes[x][y].size + 50)/100) * 100;
                nodes[x][y].used = ((nodes[x][y].used + 50)/100) * 100;
                nodes[x][y].avail = ((nodes[x][y].avail + 50)/100) * 100;
            }
        }
        nodes
    }

    fn is_final(&self) -> bool {
        self.goal_x == 0 && self.goal_y == 0
    }

    fn make_move(&self, x: i8, y: i8, dx: i8, dy: i8) -> Option<Grid> {
        if x+dx < 0 || x+dx >= self.nodes.len() as i8 || y+dy < 0 || y+dy >= self.nodes[x as usize].len() as i8 {
            return None;
        }
        if !self.nodes[x as usize][y as usize].is_viable_pair(&self.nodes[(x+dx) as usize][(y+dy) as usize]) {
            return None;
        }

        let mut new_grid = self.clone();
        new_grid.nodes[(x+dx) as usize][(y+dy) as usize].used += new_grid.nodes[x as usize][y as usize].used;
        new_grid.nodes[(x+dx) as usize][(y+dy) as usize].recalculate_avail();
        new_grid.nodes[x as usize][y as usize].used = 0;
        new_grid.nodes[x as usize][y as usize].recalculate_avail();
        
        if new_grid.goal_x == x && new_grid.goal_y == y {
            new_grid.goal_x = x+dx;
            new_grid.goal_y = y+dy;
        }

        Some(new_grid)
    }
    
    fn available_moves(&self) -> Vec<Grid> {
        let mut moves = Vec::with_capacity(4);
        
        for x in 0..self.nodes.len() as i8 {
            for y in 0..self.nodes[x as usize].len() as i8 {
                match self.make_move(x, y, -1, 0) {
                    Some(grid) => { moves.push(grid); },
                    None => {}
                };
                match self.make_move(x, y, 0, -1) {
                    Some(grid) => { moves.push(grid); },
                    None => {}
                };
                match self.make_move(x, y, 1, 0) {
                    Some(grid) => { moves.push(grid); },
                    None => {}
                };
                match self.make_move(x, y, 0, 1) {
                    Some(grid) => { moves.push(grid); },
                    None => {}
                };
            }
        }

        moves
    }
}


fn main() {
    let nodes = read_input();

    let initial = Grid::new(nodes);
    println!("Initial grid has {} possible moves", initial.available_moves().len());
    
    let mut explored: HashSet<Grid> = HashSet::new();
    let mut frontier: HashMap<Grid, u32> = HashMap::new();
    frontier.insert(initial, 0);
    let mut found_final = false;

    while !found_final {
        let (best_frontier, moves) = find_best_frontiers(&frontier);
        
        let new_states = best_frontier.available_moves();
        found_final = new_states.iter().any(|ref s| s.is_final());

        for state in new_states {
            if !(explored.contains(&state) || frontier.contains_key(&state)) {
                frontier.insert(state, moves+1);
            }
        }
        
        frontier.remove(&best_frontier);
        explored.insert(best_frontier);
    }

    let (final_frontier, moves) = frontier.iter().find(|&(s, _)| s.is_final()).unwrap();
    println!("It took {} moves to get the data", moves);
}

fn read_input() -> Vec<Node> {
    let file = BufReader::new(File::open("input.txt").unwrap());

    file.lines()
        .skip(2)
        .filter_map(|line| Node::from_str(line.unwrap().as_ref()).ok())
        .collect()
}

fn find_best_frontiers(frontier: &HashMap<Grid, u32>) -> (Grid, u32) {
    frontier.iter().min_by_key(|&(ref grid, &moves)| {
        grid.goal_x as u32 + grid.goal_y as u32 + moves
    }).map(|(&ref grid, &moves)| (grid.clone(), moves.clone())).unwrap()
}