day 10 part 2

This commit is contained in:
Andy Teijelo 2023-12-10 15:08:42 -05:00
parent 725af40f39
commit eb98f2172a

View file

@ -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(())
}