diff options
| author | nekineki <nekineki@nekineki.net> | 2024-12-10 15:02:41 +0100 |
|---|---|---|
| committer | nekineki <nekineki@nekineki.net> | 2024-12-10 15:02:41 +0100 |
| commit | ee778427cfdf3717427b97eba2dc583fd0ba0f64 (patch) | |
| tree | 88fb894e8a8088cc1566aa9585a8500cce297ab5 | |
| parent | bbb77a864e2a12d8ef32ccc75657d6b4d8b6510c (diff) | |
improve day10
| -rw-r--r-- | 2024/day10.rs | 22 |
1 files changed, 6 insertions, 16 deletions
diff --git a/2024/day10.rs b/2024/day10.rs index 7c8576d..ffe9f00 100644 --- a/2024/day10.rs +++ b/2024/day10.rs @@ -29,35 +29,26 @@ fn dfs(x0: i64, y0: i64, map: &Vec<Vec<i64>>, mut hs: HashSet<(i64, i64)>) -> Ha return hs; } -fn dfs2( - x0: i64, - y0: i64, - map: &Vec<Vec<i64>>, - mut path: Vec<(i64, i64)>, - mut hs: HashSet<Vec<(i64, i64)>>, -) -> HashSet<Vec<(i64, i64)>> { - //println!("{} {}", x0, y0); +fn dfs2(x0: i64, y0: i64, map: &Vec<Vec<i64>>) -> i64 { let h = map.len() as i64; let w = map[0].len() as i64; - path.push((x0, y0)); - let n = map[y0 as usize][x0 as usize]; if n == 9 { - hs.insert(path); - return hs; + return 1; } + let mut score = 0; for (dx, dy) in [(0, 1), (0, -1), (1, 0), (-1, 0)] { let x = x0 + dx as i64; let y = y0 + dy as i64; if (0 <= x) && (x < w) && (0 <= y) && (y < h) { if map[y as usize][x as usize] == n + 1 { - hs = dfs2(x, y, map, path.clone(), hs); + score += dfs2(x, y, map); } } } - return hs; + return score; } fn main() { @@ -92,8 +83,7 @@ fn main() { let hs = dfs(x0, y0, &map, HashSet::new()); res1 += hs.len(); - let hs2 = dfs2(x0, y0, &map, Vec::new(), HashSet::new()); - res2 += hs2.len(); + res2 += dfs2(x0, y0, &map); } println!("res1: {}", res1); |
