Bladeren bron

collision detection

Johann Woelper 5 jaren geleden
bovenliggende
commit
38e7e3e736
1 gewijzigde bestanden met toevoegingen van 39 en 16 verwijderingen
  1. 39 16
      src/day_3.rs

+ 39 - 16
src/day_3.rs

@@ -1,6 +1,6 @@
 use std::collections::HashMap;
 
-fn spawn_points(start: (i32, i32), pattern: &str) -> Vec<(i32, i32)> {
+fn spawn_points(pattern: &str) -> Vec<(i32, i32)> {
     
     let mut pts = vec![];
 
@@ -16,30 +16,22 @@ fn spawn_points(start: (i32, i32), pattern: &str) -> Vec<(i32, i32)> {
     match direction.as_ref() {
         "R" => {
             for d in 0..digits {
-                let mut s = start;
-                s.0 += d;
-                pts.push(s);
+                pts.push((0,1));
             }
         }
         "L" => {
             for d in 0..digits {
-                let mut s = start;
-                s.0 -= d;
-                pts.push(s);
+                pts.push((0,-1));
             }
         }
         "U" => {
             for d in 0..digits {
-                let mut s = start;
-                s.1 += d;
-                pts.push(s);
+                pts.push((1,0));
             }
         }
         "D" => {
             for d in 0..digits {
-                let mut s = start;
-                s.1 -= d;
-                pts.push(s);
+                pts.push((-1,0));
             }
         }
         _ => {}
@@ -50,14 +42,45 @@ fn spawn_points(start: (i32, i32), pattern: &str) -> Vec<(i32, i32)> {
 }
 
 
+fn run_instruction_set(instructions: Vec<&str>, grid: &mut HashMap<(i32,i32), i32>) {
+    let mut cursor = (0,0);
+
+    for instruction in instructions {
+        
+        for pt in spawn_points(instruction) {
+            cursor.0 += pt.0;
+            cursor.1 += pt.1;
+            println!("Marching to {:?}", cursor);
+            if let Some(occupied_pt) = grid.get(&cursor) {
+                println!("Point present @{:?}", pt);
+
+                grid.insert(cursor, occupied_pt + 1);
+            } else {
+
+                grid.insert(cursor, 0);
+            }
+
+        }
+
+
+    }
+}
+
 fn solve() {
     // R75,D30,R83,U83,L12,D49,R71,U7,L72
 
     // Hashmap that has a 2d point as key and a vec for wire indices
-    let grid: HashMap<(i32,i32), Vec<usize>> = HashMap::new();
+    let mut grid: HashMap<(i32,i32), i32> = HashMap::new();
+
+    let w1 = vec!["R8","U5","L5","D3"];
+    let w2 = vec!["U7","R6","D4","L4"];
+
+
+    run_instruction_set(w1, &mut grid);
+    run_instruction_set(w2, &mut grid);
+
+    // dbg!(grid);
 
-    let p = spawn_points((0,0), "R10");
-    dbg!(p);
 }