Skip to content

Commit

Permalink
Optimize 2024-18
Browse files Browse the repository at this point in the history
  • Loading branch information
fornwall committed Dec 18, 2024
1 parent 03b4259 commit 8f23cd0
Showing 1 changed file with 44 additions and 11 deletions.
55 changes: 44 additions & 11 deletions crates/core/src/year2024/day18.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::common::array_deque::ArrayDeque;
use crate::common::priority_queueu::PriorityQueue;
use crate::input::{on_error, Input};

pub fn solve(input: &Input) -> Result<String, String> {
let mut grid = [[false; 71]; 71];
let mut grid = [[u16::MAX; 71]; 71];
for (count, line) in input.text.lines().enumerate() {
let (x, y) = line.split_once(',').ok_or_else(on_error)?;
let (x, y) = (
Expand All @@ -13,21 +14,17 @@ pub fn solve(input: &Input) -> Result<String, String> {
if !(0..71).contains(&x) && !(0..71).contains(&y) {
return Err(format!("Coordinate out of bounds: {},{}", x, y));
}
grid[y][x] = true;
grid[y][x] = count as u16;

if input.is_part_one() {
if count == 1023 {
return Ok(format!("{}", shortest_path(&grid).ok_or_else(on_error)?));
}
} else if count >= 1024 && shortest_path(&grid).is_none() {
return Ok(format!("{},{}", x, y));
if input.is_part_one() && count == 1023 {
return Ok(format!("{}", shortest_path(&grid).ok_or_else(on_error)?));
}
}

Err("No solution found".to_string())
find_first_blocker_byte(&grid)
}

fn shortest_path(grid: &[[bool; 71]; 71]) -> Option<i32> {
fn shortest_path(grid: &[[u16; 71]; 71]) -> Option<i32> {
let mut visited = [[false; 71]; 71];
let mut to_visit = ArrayDeque::<1024, (i32, (i8, i8))>::new();
to_visit.push_back((0, (0, 0))).ok()?;
Expand All @@ -42,7 +39,10 @@ fn shortest_path(grid: &[[bool; 71]; 71]) -> Option<i32> {
visited[y as usize][x as usize] = true;
for (dx, dy) in [(0, -1), (1, 0), (0, 1), (-1, 0)] {
let (nx, ny) = (x + dx, y + dy);
if (0..71).contains(&nx) && (0..71).contains(&ny) && !grid[ny as usize][nx as usize] {
if (0..71).contains(&nx)
&& (0..71).contains(&ny)
&& grid[ny as usize][nx as usize] == u16::MAX
{
to_visit.push_back((cost + 1, (nx, ny))).ok()?;
}
}
Expand All @@ -51,6 +51,39 @@ fn shortest_path(grid: &[[bool; 71]; 71]) -> Option<i32> {
None
}

fn find_first_blocker_byte(grid: &[[u16; 71]; 71]) -> Result<String, String> {
let mut visited = [[false; 71]; 71];
let mut to_visit = ArrayDeque::<1024, (i8, i8)>::new();
let mut found_blockers = PriorityQueue::<5000, (i32, (i8, i8))>::new();
found_blockers.push((-(u16::MAX as i32), (0, 0)))?;
while let Some((time, (block_x, block_y))) = found_blockers.pop() {
let time = (-time) as u16;
to_visit.push_back((block_x, block_y))?;
while let Some((x, y)) = to_visit.pop_front() {
if (x, y) == (70, 70) {
return Ok(format!("{block_x},{block_y}"));
}
if visited[y as usize][x as usize] {
continue;
}
visited[y as usize][x as usize] = true;
for (dx, dy) in [(0, -1), (1, 0), (0, 1), (-1, 0)] {
let (nx, ny) = (x + dx, y + dy);
if (0..71).contains(&nx) && (0..71).contains(&ny) {
let grid_time = grid[ny as usize][nx as usize];
if grid_time < time {
found_blockers.push((-(grid_time as i32), (nx, ny)))?;
} else {
to_visit.push_back((nx, ny))?;
}
}
}
}
}

Err("No solution found".to_string())
}

#[test]
pub fn tests() {
use crate::input::{test_part_one, test_part_two};
Expand Down

1 comment on commit 8f23cd0

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@                     Benchmark Difference                     @@
#      Name   Old (instructions)   New (instructions)   Change (%)
  2024_19_1           10,000,000           10,000,000            0
  2024_19_2           10,000,000           10,000,000            0
  2024_20_1           10,000,000           10,000,000            0
  2024_20_2           10,000,000           10,000,000            0
  2024_21_1           10,000,000           10,000,000            0
  2024_21_2           10,000,000           10,000,000            0
  2024_22_1           10,000,000           10,000,000            0
  2024_22_2           10,000,000           10,000,000            0
  2024_23_1           10,000,000           10,000,000            0
  2024_23_2           10,000,000           10,000,000            0
  2024_24_1           10,000,000           10,000,000            0
  2024_24_2           10,000,000           10,000,000            0
  2024_25_1           10,000,000           10,000,000            0
   2024_1_1            1,041,087            1,041,087            0
   2024_1_2            1,070,179            1,070,179            0
   2024_2_1            1,741,515            1,741,515            0
   2024_2_2            1,962,906            1,962,906            0
   2024_3_1              227,380              227,380            0
   2024_3_2              317,753              317,753            0
   2024_4_1            1,213,772            1,213,772            0
   2024_4_2              453,902              453,902            0
   2024_5_1            1,605,047            1,605,047            0
   2024_5_2            1,718,989            1,718,989            0
   2024_6_1              641,455              641,455            0
   2024_6_2          141,310,079          141,310,079            0
   2024_7_1            2,466,892            2,466,892            0
   2024_7_2            3,758,401            3,758,401            0
   2024_8_1               44,406               44,406            0
   2024_8_2               86,863               86,863            0
   2024_9_1              897,539              897,539            0
   2024_9_2            2,497,019            2,497,019            0
  2024_10_1            1,087,488            1,087,488            0
  2024_10_2            1,089,387            1,089,387            0
  2024_11_1            3,848,456            3,848,427            0
  2024_11_2           13,462,234           13,462,486            0
  2024_12_1            7,663,695            7,663,695            0
  2024_12_2            7,942,246            7,942,246            0
  2024_13_1              910,454              910,454            0
  2024_13_2              910,208              910,208            0
  2024_14_1              593,112              593,112            0
  2024_14_2           13,416,623           13,416,623            0
  2024_15_1            1,913,993            1,913,993            0
  2024_15_2           59,667,648           59,667,648            0
  2024_16_1           18,233,704           18,233,704            0
  2024_16_2           18,321,270           18,321,270            0
  2024_17_1               11,725               11,725            0
  2024_17_2              218,093              218,093            0
  2024_18_1            1,125,422            1,103,427           -1
+ 2024_18_2          804,149,191            1,427,027          -99
Benchmark Instructions (count) Instructions (%)
2024_6_2 141,310,079 31.8
2024_15_2 59,667,648 13.4
2024_16_2 18,321,270 4.1
2024_16_1 18,233,704 4.1
2024_11_2 13,462,486 3.0
2024_14_2 13,416,623 3.0
2024_25_1 10,000,000 2.2
2024_24_2 10,000,000 2.2
2024_24_1 10,000,000 2.2
2024_23_2 10,000,000 2.2
2024_23_1 10,000,000 2.2
2024_22_2 10,000,000 2.2
2024_22_1 10,000,000 2.2
2024_21_2 10,000,000 2.2
2024_21_1 10,000,000 2.2
2024_20_2 10,000,000 2.2
2024_20_1 10,000,000 2.2
2024_19_2 10,000,000 2.2
2024_19_1 10,000,000 2.2
2024_12_2 7,942,246 1.8
2024_12_1 7,663,695 1.7
2024_11_1 3,848,427 0.9
2024_7_2 3,758,401 0.8
2024_9_2 2,497,019 0.6
2024_7_1 2,466,892 0.6
2024_2_2 1,962,906 0.4
2024_15_1 1,913,993 0.4
2024_2_1 1,741,515 0.4
2024_5_2 1,718,989 0.4
2024_5_1 1,605,047 0.4
2024_18_2 1,427,027 0.3
2024_4_1 1,213,772 0.3
2024_18_1 1,103,427 0.2
2024_10_2 1,089,387 0.2
2024_10_1 1,087,488 0.2
2024_1_2 1,070,179 0.2
2024_1_1 1,041,087 0.2
2024_13_1 910,454 0.2
2024_13_2 910,208 0.2
2024_9_1 897,539 0.2
2024_6_1 641,455 0.1
2024_14_1 593,112 0.1
2024_4_2 453,902 0.1
2024_3_2 317,753 0.1
2024_3_1 227,380 0.1
2024_17_2 218,093 0.0
2024_8_2 86,863 0.0
2024_8_1 44,406 0.0
2024_17_1 11,725 0.0

Please sign in to comment.