From eb98f2172a9da739c5ba91767d1dd354d28b402e Mon Sep 17 00:00:00 2001 From: Andy Teijelo Date: Sun, 10 Dec 2023 15:08:42 -0500 Subject: [PATCH] day 10 part 2 --- src/main.rs | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index cf8b99b..eba0b4e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,8 +19,6 @@ fn main() -> Result<()> { max_y = std::cmp::max(max_y, y); grid.push(cells); } - // println!("{grid:?}"); - // println!("{start:?}"); let mut front = VecDeque::new(); let mut visited: HashSet<(usize, usize)> = HashSet::new(); front.push_back((start.0, start.1, 0)); @@ -32,21 +30,48 @@ fn main() -> Result<()> { continue; } visited.insert((x, y)); - println!("x: {x} y: {y} depth: {depth}"); - if x < max_x && "-J7".contains(grid[y][x + 1]) { + let c = grid[y][x]; + + if "S-FL".contains(c) && x < max_x && "-J7".contains(grid[y][x + 1]) { front.push_back((x + 1, y, depth + 1)); } - if x > 0 && "-LF".contains(grid[y][x - 1]) { + if "S-7J".contains(c) && x > 0 && "-LF".contains(grid[y][x - 1]) { front.push_back((x - 1, y, depth + 1)); } - if y > 0 && "|7F".contains(grid[y - 1][x]) { + if "S|JL".contains(c) && y > 0 && "|7F".contains(grid[y - 1][x]) { front.push_back((x, y - 1, depth + 1)); } - if y < max_y && "|JL".contains(grid[y + 1][x]) { + if "S|F7".contains(c) && y < max_y && "|JL".contains(grid[y + 1][x]) { front.push_back((x, y + 1, depth + 1)); } } + let mut enclosed = 0; + for j in 0..(max_x + max_y + 2) { + println!("j={j}"); + let mut outside = true; + for x in 0..=j { + let y = j - x; + let Some(row) = grid.get(y) else { + continue; + }; + let Some(c) = row.get(x) else { + continue; + }; + let o = outside; + // in my input, S is equivalent to 7, so outside flips + let v = visited.contains(&(x, y)); + if v { + if "-|SL7".contains(*c) { + outside = !outside; + } + } else if !outside { + enclosed += 1; + } + println!(" {x} {y} visited={v} c={c} outside={o} -> {outside} enclosed={enclosed}",); + } + } + println!("{enclosed}"); Ok(()) }