Browse Source

day_3 update

Johann Woelper 4 năm trước cách đây
mục cha
commit
db43a92565
2 tập tin đã thay đổi với 13 bổ sung17 xóa
  1. 11 15
      src/day_3.rs
  2. 2 2
      src/main.rs

+ 11 - 15
src/day_3.rs

@@ -1,9 +1,10 @@
 use std::collections::HashMap;
 
 fn spawn_points(pattern: &str) -> Vec<(i32, i32)> {
-    
+    // The points to return, for example R3 will return (0,1),(0,1),(0,1) (three steps to the right)
     let mut pts = vec![];
 
+    // the digits in pattern (3 in 'R3')
     let digits = pattern
         .chars()
         .filter(|x| x.is_numeric())
@@ -11,36 +12,39 @@ fn spawn_points(pattern: &str) -> Vec<(i32, i32)> {
         .parse()
         .unwrap();
 
+    // The direction of a pattern ('R' in 'R3')
     let direction: String = pattern.chars().filter(|x| !x.is_numeric()).collect();
 
     match direction.as_ref() {
         "R" => {
-            for d in 0..digits {
+            for _d in 0..digits {
                 pts.push((0,1));
             }
         }
         "L" => {
-            for d in 0..digits {
+            for _d in 0..digits {
                 pts.push((0,-1));
             }
         }
         "U" => {
-            for d in 0..digits {
+            for _d in 0..digits {
                 pts.push((1,0));
             }
         }
         "D" => {
-            for d in 0..digits {
+            for _d in 0..digits {
                 pts.push((-1,0));
             }
         }
         _ => {}
-
     }
     pts
 
 }
 
+fn manhattan_to_origin(pt: (i32,i32)) -> i32 {
+    pt.0.abs()+pt.1.abs()
+}
 
 fn run_instruction_set(instructions: Vec<String>, grid: &mut HashMap<(i32,i32), i32>) -> Option<i32>{
     let mut cursor = (0,0);
@@ -48,10 +52,9 @@ fn run_instruction_set(instructions: Vec<String>, grid: &mut HashMap<(i32,i32),
     for instruction in instructions {
         
         for pt in spawn_points(&instruction) {
-            // println!("Marching to {:?}", cursor);
             if let Some(occupied_pt) = grid.get(&cursor) {
                 println!("Point present @{:?}", cursor);
-                let dist = cursor.0.abs()+cursor.1.abs();
+                let dist = manhattan_to_origin(cursor);
                 if dist < closest && dist > 0 {
                     closest = dist;
                 }
@@ -63,8 +66,6 @@ fn run_instruction_set(instructions: Vec<String>, grid: &mut HashMap<(i32,i32),
             cursor.1 += pt.1;
 
         }
-
-
     }
 
     match closest {
@@ -74,7 +75,6 @@ fn run_instruction_set(instructions: Vec<String>, grid: &mut HashMap<(i32,i32),
 }
 
 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 mut grid: HashMap<(i32,i32), i32> = HashMap::new();
@@ -88,14 +88,10 @@ fn solve() {
     run_instruction_set(w1, &mut grid);
     dbg!(run_instruction_set(w2, &mut grid));
 
-    // dbg!(grid);
 
 }
 
 
-
-
-
 pub fn main() {
     solve();
 }

+ 2 - 2
src/main.rs

@@ -10,6 +10,6 @@ mod day_5;
 
 
 fn main() {
-    // day_3::main();
-    day_5::main();
+    day_3::main();
+    // day_5::main();
 }