Skip to content

Commit

Permalink
day18
Browse files Browse the repository at this point in the history
  • Loading branch information
vslinko committed Dec 18, 2024
1 parent 7134b00 commit 203b67f
Showing 1 changed file with 35 additions and 29 deletions.
64 changes: 35 additions & 29 deletions src/day18.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,25 @@ impl PartialOrd for Node {
}
}

const DIRECTIONS: [[usize; 4]; GRID_SIZE] = {
let mut dirs = [[0; 4]; GRID_SIZE];

const DIRECTIONS: [usize; GRID_SIZE * 4] = {
let mut dirs = [0; GRID_SIZE * 4];
let mut i = 0;

while i < GRID_SIZE {
if i >= WIDTH {
dirs[i][0] = i - WIDTH;
dirs[i * 4] = i - WIDTH;
}

if i % WIDTH != 0 {
dirs[i][1] = i - 1;
dirs[i * 4 + 1] = i - 1;
}

if i % WIDTH != WIDTH - 1 {
dirs[i][2] = i + 1;
dirs[i * 4 + 2] = i + 1;
}

if i < GRID_SIZE - WIDTH {
dirs[i][3] = i + WIDTH;
dirs[i * 4 + 3] = i + WIDTH;
}

i += 1;
Expand All @@ -84,13 +83,12 @@ const DIRECTIONS: [[usize; 4]; GRID_SIZE] = {

unsafe fn find_fastest_path_score(grid: &[bool; GRID_SIZE]) -> usize {
let mut open_set = BinaryHeap::with_capacity(1000);
let mut closed_set = [false; GRID_SIZE];
let mut closed_set = [true; GRID_SIZE];
let mut best_scores = [usize::MAX; GRID_SIZE];

let start_node = Node { score: 0, index: 0 };
open_set.push(start_node);
open_set.push(Node { score: 0, index: 0 });
*best_scores.get_unchecked_mut(START_INDEX) = 0;
*closed_set.get_unchecked_mut(START_INDEX) = true;
*closed_set.get_unchecked_mut(START_INDEX) = false;

while let Some(current) = open_set.pop() {
if current.index == END_INDEX {
Expand All @@ -101,23 +99,31 @@ unsafe fn find_fastest_path_score(grid: &[bool; GRID_SIZE]) -> usize {
continue;
}

for &next_index in DIRECTIONS.get_unchecked(current.index) {
if *grid.get_unchecked(next_index) || *closed_set.get_unchecked(next_index) {
continue;
}
macro_rules! check_direction {
($next_index:expr) => {
let next_index = $next_index;

let next_score = current.score + 1;
if *grid.get_unchecked(next_index) && *closed_set.get_unchecked(next_index) {
let next_score = current.score + 1;

if next_score < *best_scores.get_unchecked(next_index) {
*best_scores.get_unchecked_mut(next_index) = next_score;
open_set.push(Node {
score: next_score,
index: next_index,
});
}
if next_score < *best_scores.get_unchecked(next_index) {
*best_scores.get_unchecked_mut(next_index) = next_score;

open_set.push(Node {
score: next_score,
index: next_index,
});
}
}
};
}

*closed_set.get_unchecked_mut(current.index) = true;
check_direction!(*DIRECTIONS.get_unchecked(current.index * 4));
check_direction!(*DIRECTIONS.get_unchecked(current.index * 4 + 1));
check_direction!(*DIRECTIONS.get_unchecked(current.index * 4 + 2));
check_direction!(*DIRECTIONS.get_unchecked(current.index * 4 + 3));

*closed_set.get_unchecked_mut(current.index) = false;
}

usize::MAX
Expand All @@ -129,15 +135,15 @@ pub fn part1(input: &str) -> impl Display {

unsafe fn inner1(input: &str) -> impl Display {
let input = input.as_bytes();
let mut grid = [false; GRID_SIZE];
let mut grid = [true; GRID_SIZE];
let mut i = 0;

macro_rules! corrupt {
() => {
*grid.get_unchecked_mut(index!(
read_0_99_and_skip_next!(input, i),
read_0_99_and_skip_next!(input, i)
)) = true;
)) = false;
};
}

Expand All @@ -163,7 +169,7 @@ pub fn part2(input: &str) -> impl Display {

unsafe fn inner2(input: &str) -> impl Display {
let input = input.as_bytes();
let mut initial_grid = [false; GRID_SIZE];
let mut initial_grid = [true; GRID_SIZE];
let mut i = 0;

let mut corrupted = Vec::with_capacity(3500);
Expand All @@ -176,7 +182,7 @@ unsafe fn inner2(input: &str) -> impl Display {

let mut c = 0;
while c < PART1_BYTES {
*initial_grid.get_unchecked_mut(*corrupted.get_unchecked(c)) = true;
*initial_grid.get_unchecked_mut(*corrupted.get_unchecked(c)) = false;
c += 1;
}

Expand All @@ -188,7 +194,7 @@ unsafe fn inner2(input: &str) -> impl Display {
let mid = (left + right) / 2;

for i in PART1_BYTES..mid {
*grid.get_unchecked_mut(*corrupted.get_unchecked(i)) = true;
*grid.get_unchecked_mut(*corrupted.get_unchecked(i)) = false;
}

if find_fastest_path_score(&grid) == usize::MAX {
Expand Down

0 comments on commit 203b67f

Please sign in to comment.