]> aoc.elinar.fr Git - aoc_2024/commitdiff
Day08 refactor
authoralex <>
Sun, 8 Dec 2024 07:12:26 +0000 (08:12 +0100)
committeralex <>
Sun, 8 Dec 2024 07:18:53 +0000 (08:18 +0100)
src/day08.rs

index d7d680daba71b3e1bacc1377656298d18bbd22ea..d9f976e372b3aa1b800aa217543c3159eeadae62 100644 (file)
@@ -22,10 +22,7 @@ impl Puzzle {
     }
 }
 
-fn run_part1(input: &str) -> Result<u32, Box<dyn Error>> {
-    println!("Running {} - part 1", get_day());
-
-    let puzzle = Puzzle::new(input);
+fn solve(puzzle: &Puzzle, depth: &[isize]) -> u32 {
     let mut visited: HashSet<(isize, isize)> = HashSet::new();
     let mut antinodes: HashSet<(isize, isize)> = HashSet::new();
 
@@ -38,55 +35,36 @@ fn run_part1(input: &str) -> Result<u32, Box<dyn Error>> {
                 puzzle.map.iter()
                     .filter(|(k, v)| *v == value && *k != key)
                     .for_each(|(k, v)| {
-                        let x1 = if k <= key { k } else { key };
-                        let x2 = if k <= key { key } else { k };
+                        let (x1, x2) = if k <= key { (k, key) } else { (key, k) };
                         let d = (x1.0 - x2.0, x1.1 - x2.1);
-                        for a in [(x1.0 + d.0, x1.1 + d.1), (x2.0 - d.0, x2.1 - d.1)] {
-                            if puzzle.map.contains_key(&a) {
-                                antinodes.insert(a);
+                        for i in depth {
+                            for a in [(x1.0 + i*d.0, x1.1 + i*d.1), (x2.0 - i*d.0, x2.1 - i*d.1)] {
+                                if puzzle.map.contains_key(&a) {
+                                    antinodes.insert(a);
+                                }
                             }
                         }
                     });
             }
         });
 
-    let res = antinodes.len() as u32;
-    Ok(res)
+    antinodes.len() as u32
 }
 
-fn run_part2(input: &str) -> Result<u32, Box<dyn Error>> {
-    println!("Running {} - part 2", get_day());
 
-    let puzzle = Puzzle::new(input);
-    let mut visited: HashSet<(isize, isize)> = HashSet::new();
-    let mut antinodes: HashSet<(isize, isize)> = HashSet::new();
+fn run_part1(input: &str) -> Result<u32, Box<dyn Error>> {
+    println!("Running {} - part 1", get_day());
 
-    puzzle.map.iter()
-        .filter(|(_, value)| **value != '.')
-        .for_each(|(key, value)| {
-            if !visited.contains(key) {
-                visited.insert(*key);
+    let puzzle = Puzzle::new(input);
+    Ok(solve(&puzzle, &[1]))
+}
 
-                puzzle.map.iter()
-                    .filter(|(k, v)| *v == value && *k != key)
-                    .for_each(|(k, v)| {
-                        let x1 = if k <= key { k } else { key };
-                        let x2 = if k <= key { key } else { k };
-                        let d = (x1.0 - x2.0, x1.1 - x2.1);
-                        for i in 0..puzzle.map.len() {
-                            let i = i as isize;
-                            for a in [(x1.0 + i*d.0, x1.1 + i*d.1), (x2.0 - i*d.0, x2.1 - i*d.1)] {
-                                if puzzle.map.contains_key(&a) {
-                                    antinodes.insert(a);
-                                }
-                            }
-                        }
-                    });
-            }
-        });
+fn run_part2(input: &str) -> Result<u32, Box<dyn Error>> {
+    println!("Running {} - part 2", get_day());
 
-    let res = antinodes.len() as u32;
-    Ok(res)
+    let puzzle = Puzzle::new(input);
+    let depth = (0..puzzle.map.len()).map(|v| v as isize).collect::<Vec<isize>>();
+    Ok(solve(&puzzle, &depth))
 }
 
 pub fn run(input: &str) -> Result<(), Box<dyn Error>> {