AOC part 2
authorJustin Worthe <justin.worthe@gmail.com>
Thu, 22 Dec 2016 17:32:32 +0000 (19:32 +0200)
committerJustin Worthe <justin.worthe@gmail.com>
Thu, 22 Dec 2016 17:32:32 +0000 (19:32 +0200)
The trick is to round to the nearest 100, so states can be considered
'equivalent' through multiple paths.

aoc22/src/main.rs

index c07d628..bd374d5 100644 (file)
@@ -76,32 +76,7 @@ impl Grid {
             next_col.push(node);
         }
         grid_nodes.push(next_col);
-
-        for x in 0..grid_nodes.len() {
-            for y in 0..grid_nodes[x].len() {
-                let mut is_blocker = true;
-                if x > 0 && grid_nodes[x-1][y].size >= grid_nodes[x][y].used {
-                    is_blocker = false;
-                }
-                if x < grid_nodes.len()-1 && grid_nodes[x+1][y].size >= grid_nodes[x][y].used {
-                    is_blocker = false;
-                }
-                if y > 0 && grid_nodes[x][y-1].size >= grid_nodes[x][y].used {
-                    is_blocker = false;
-                }
-                if y < grid_nodes[x].len()-1 && grid_nodes[x][y+1].size >= grid_nodes[x][y].used {
-                    is_blocker = false;
-                }
-                grid_nodes[x][y].blocker = is_blocker;
-            }
-        }
-        for x in 0..grid_nodes.len() {
-            for y in 0..grid_nodes[x].len() {
-                grid_nodes[x][y].size = if grid_nodes[x][y].blocker { 2 } else { 1 };
-                grid_nodes[x][y].used = if grid_nodes[x][y].used > 0 { grid_nodes[x][y].size } else { 0 };
-                grid_nodes[x][y].avail = grid_nodes[x][y].size - grid_nodes[x][y].used;
-            }
-        }
+        grid_nodes = Grid::normalize(grid_nodes);
 
         Grid {
             nodes: grid_nodes,
@@ -110,6 +85,17 @@ impl Grid {
         }
     }
 
+    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
     }