Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Fadi88 committed Dec 19, 2024
1 parent 20a27ee commit dafd5b1
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions 2024/day18/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ where
result // Return the result of the function
}

fn is_in_bounds(np: (i32, i32), max_x: i32, max_y: i32) -> bool {
let x = np.0 >= 0 && np.0 <= max_x;
let y = np.1 >= 0 && np.1 <= max_y;

x && y
}

fn maze(pts: &[(i32, i32)]) -> Option<i32> {
let start = (0, 0);
let end = (70, 70);
Expand All @@ -19,28 +26,20 @@ fn maze(pts: &[(i32, i32)]) -> Option<i32> {
let mut to_visit = VecDeque::from([(start, 0)]);

while let Some((cp, cd)) = to_visit.pop_front() {
if seen.contains(&cp) {
continue;
}
if !seen.contains(&cp) {
seen.insert(cp);

if cp == end {
return Some(cd);
}
if cp == end {
return Some(cd);
}

for (dx, dy) in [(1, 0), (-1, 0), (0, 1), (0, -1)] {
let np = (cp.0 + dx, cp.1 + dy);
if 0 <= np.0
&& np.0 <= 70
&& 0 <= np.1
&& np.1 <= 70
&& !seen.contains(&np)
&& !pts.contains(&np)
{
to_visit.push_back((np, cd + 1));
for (dx, dy) in [(1, 0), (-1, 0), (0, 1), (0, -1)] {
let np = (cp.0 + dx, cp.1 + dy);
if is_in_bounds(np, 70, 70) && !pts.contains(&np) {
to_visit.push_back((np, cd + 1));
}
}
}

seen.insert(cp);
}

None
Expand Down

0 comments on commit dafd5b1

Please sign in to comment.