diff options
| author | Aleksa Vuckovic <aleksa@vuckovic.cc> | 2023-12-16 06:55:42 +0100 |
|---|---|---|
| committer | Aleksa Vuckovic <aleksa@vuckovic.cc> | 2023-12-16 06:55:42 +0100 |
| commit | 457c2af984ac34e6615a8b49b953c4499809255c (patch) | |
| tree | 1cbfbb97d9ec829b7924a20c2d3ab22f8209cb15 /src/day16pt1.rs | |
| parent | b2c6978311010cd522d2680e7078e496fab28744 (diff) | |
day16
Diffstat (limited to 'src/day16pt1.rs')
| -rw-r--r-- | src/day16pt1.rs | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/src/day16pt1.rs b/src/day16pt1.rs new file mode 100644 index 0000000..5377074 --- /dev/null +++ b/src/day16pt1.rs @@ -0,0 +1,65 @@ +use std::collections::HashMap; + +fn bfs( + input: &Vec<Vec<char>>, + was: &mut Vec<Vec<bool>>, + x: i32, + y: i32, + dx: i32, + dy: i32, + comb: &mut HashMap<(i32, i32, i32, i32), i32>, +) { + if x < 0 + || x >= input[0].len() as i32 + || y < 0 + || y >= input.len() as i32 + || comb.contains_key(&(x, y, dx, dy)) + { + return; + } + + let c = input[y as usize][x as usize]; + was[y as usize][x as usize] = true; + comb.insert((x, y, dx, dy), 1); + + if c == '/' { + bfs(input, was, x - dy, y - dx, -dy, -dx, comb); + } else if c == '\\' { + bfs(input, was, x + dy, y + dx, dy, dx, comb); + } else if c == '|' { + if dy != 0 { + bfs(input, was, x + dx, y + dy, dx, dy, comb); + } else { + bfs(input, was, x, y - 1, 0, -1, comb); + bfs(input, was, x, y + 1, 0, 1, comb); + } + } else if c == '-' { + if dx != 0 { + bfs(input, was, x + dx, y + dy, dx, dy, comb); + } else { + bfs(input, was, x - 1, y, -1, 0, comb); + bfs(input, was, x + 1, y, 1, 0, comb); + } + } else if c == '.' { + bfs(input, was, x + dx, y + dy, dx, dy, comb); + } +} + +pub fn main() { + let txt = std::fs::read_to_string("./input/day16.txt").unwrap(); + let input = txt + .lines() + .map(|s| s.chars().collect::<Vec<char>>()) + .collect::<Vec<Vec<char>>>(); + + let mut was = vec![vec![false; input[0].len()]; input.len()]; + let mut comb = HashMap::new(); + + bfs(&input, &mut was, 0, 0, 1, 0, &mut comb); + + let mut sum = 0; + for i in was { + sum += i.iter().filter(|&n| *n == true).count(); + } + println!("{}", sum); +} |
