Skip to content

Commit

Permalink
feat: 2023 day10 p2
Browse files Browse the repository at this point in the history
  • Loading branch information
joao-conde committed Dec 15, 2023
1 parent 802d22b commit 4135a73
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions 2023/src/bin/day10.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@ fn main() {
.unwrap();

let mut deque: VecDeque<(usize, usize, usize)> = VecDeque::new();

let mut visited: HashSet<(usize, usize)> = HashSet::new();

deque.push_back((si, sj, 0));

let mut max_steps = 0;

while let Some((i, j, distance)) = deque.pop_front() {
if visited.contains(&(i, j)) {
continue;
}

visited.insert((i, j));

max_steps = max_steps.max(distance);

let cur_tile = grid[i][j];
Expand All @@ -53,5 +54,36 @@ fn main() {
}
}

println!("{max_steps}");
println!("p1: {max_steps}");

let x: usize = (0..nrows).map(|i| inside_loop(&grid, &visited, i)).sum();
println!("p2: {x}");

inside_loop(&grid, &visited, 2);
}

fn inside_loop(grid: &[Vec<char>], visited: &HashSet<(usize, usize)>, i: usize) -> usize {
let mut counts = 0;
let mut inside = false;

for (j, &tile) in grid[i].iter().enumerate() {
if visited.contains(&(i, j)) {
inside = match tile {
'|' => !inside,
'J' => !inside,
'-' => inside,
'L' => !inside,
'7' => inside,
'F' => inside,
'S' => inside,
_ => unreachable!(),
}
};

if inside && !visited.contains(&(i, j)) {
counts += 1;
}
}

counts
}

0 comments on commit 4135a73

Please sign in to comment.