Skip to content

Commit

Permalink
Optimize 2024-20
Browse files Browse the repository at this point in the history
  • Loading branch information
fornwall committed Dec 21, 2024
1 parent d7c0b57 commit c91b2db
Showing 1 changed file with 18 additions and 31 deletions.
49 changes: 18 additions & 31 deletions crates/core/src/year2024/day20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ pub fn solve(input: &Input) -> Result<u32, String> {
return Err("No end location".to_string());
}

let mut to_visit = ArrayDeque::<{ WORK_QUEUE_MAX_SIZE }, (u16, (i16, i16))>::new();
let mut costs = [[u16::MAX; MAX_GRID_SIZE]; MAX_GRID_SIZE];

let mut to_visit = ArrayDeque::<{ WORK_QUEUE_MAX_SIZE }, (u16, (i16, i16))>::new();
to_visit.push_back((0, start_location))?;

while let Some((cost, position)) = to_visit.pop_front() {
Expand All @@ -58,38 +57,26 @@ pub fn solve(input: &Input) -> Result<u32, String> {
}
}

// Second pass to find cheats:
let diamond_size: i16 = input.part_values(2, 20);

// Second pass to find cheats
let mut visited = [[false; MAX_GRID_SIZE]; MAX_GRID_SIZE];
let mut to_visit = ArrayDeque::<{ WORK_QUEUE_MAX_SIZE }, (i16, i16)>::new();
to_visit.push_back(start_location)?;
let mut num_great_cheats = 0;

while let Some(position) = to_visit.pop_front() {
if visited[position.1 as usize][position.0 as usize] {
continue;
}
let cost = costs[position.1 as usize][position.0 as usize];
visited[position.1 as usize][position.0 as usize] = true;
for (dx, dy) in [(0, -1), (1, 0), (0, 1), (-1, 0)] {
let next = (position.0 + dx, position.1 + dy);
if grid.at(next) != b'#' {
to_visit.push_back(next)?;
for y in 1..(grid.width - 1) {
for x in 1..(grid.width - 1) {
let cost = costs[y as usize][x as usize];
if cost == u16::MAX {
continue;
}
}

// Cheat:
for dx in -diamond_size..=diamond_size {
for dy in (dx.abs() - diamond_size)..=(diamond_size - dx.abs()) {
let manhattan_distance = dx.abs() + dy.abs();
let cheat = (position.0 + dx, position.1 + dy);
if grid.at(cheat) != b'#' {
if let Some(gain) = costs[cheat.1 as usize][cheat.0 as usize]
.checked_sub(cost + manhattan_distance as u16)
{
if gain >= 100 {
num_great_cheats += 1;
for dy in -diamond_size..=diamond_size {
for dx in (dy.abs() - diamond_size)..=(diamond_size - dy.abs()) {
let manhattan_distance = dx.abs() + dy.abs();
let cheat = (x + dx, y + dy);
if grid.at(cheat) != b'#' {
if let Some(gain) = costs[cheat.1 as usize][cheat.0 as usize]
.checked_sub(cost + manhattan_distance as u16)
{
if gain >= 100 {
num_great_cheats += 1;
}
}
}
}
Expand Down

1 comment on commit c91b2db

@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_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,088            1,041,088            0
   2024_1_2            1,070,180            1,070,180            0
   2024_2_1            1,741,514            1,741,514            0
   2024_2_2            1,962,905            1,962,905            0
   2024_3_1              227,379              227,379            0
   2024_3_2              317,752              317,752            0
   2024_4_1            1,213,771            1,213,771            0
   2024_4_2              453,901              453,901            0
   2024_5_1            1,605,046            1,605,046            0
   2024_5_2            1,718,988            1,718,988            0
   2024_6_1              641,454              641,454            0
   2024_6_2          141,310,078          141,310,078            0
   2024_7_1            2,466,893            2,466,893            0
   2024_7_2            3,758,402            3,758,402            0
   2024_8_1               44,405               44,405            0
   2024_8_2               86,862               86,862            0
   2024_9_1              897,540              897,540            0
   2024_9_2            2,497,020            2,497,020            0
  2024_10_1            1,087,487            1,087,487            0
  2024_10_2            1,089,386            1,089,386            0
  2024_11_1            3,848,345            3,848,414            0
  2024_11_2           13,462,531           13,462,517            0
  2024_12_1            7,663,694            7,663,694            0
  2024_12_2            7,942,245            7,942,245            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,992            1,913,992            0
  2024_15_2           59,667,647           59,667,647            0
  2024_16_1           18,233,703           18,233,703            0
  2024_16_2           18,321,269           18,321,269            0
  2024_17_1               11,725               11,725            0
  2024_17_2              218,087              218,087            0
  2024_18_1            1,103,427            1,103,427            0
  2024_18_2            1,427,027            1,427,027            0
  2024_19_1            2,242,630            2,242,630            0
  2024_19_2            2,242,690            2,242,690            0
  2024_20_1            8,370,882            7,199,422          -13
  2024_20_2          262,352,166          241,095,069           -8
Benchmark Instructions (count) Instructions (%)
2024_20_2 241,095,069 36.7
2024_6_2 141,310,078 21.5
2024_15_2 59,667,647 9.1
2024_16_2 18,321,269 2.8
2024_16_1 18,233,703 2.8
2024_11_2 13,462,517 2.0
2024_14_2 13,416,623 2.0
2024_25_1 10,000,000 1.5
2024_24_2 10,000,000 1.5
2024_24_1 10,000,000 1.5
2024_23_2 10,000,000 1.5
2024_23_1 10,000,000 1.5
2024_22_2 10,000,000 1.5
2024_22_1 10,000,000 1.5
2024_21_2 10,000,000 1.5
2024_21_1 10,000,000 1.5
2024_12_2 7,942,245 1.2
2024_12_1 7,663,694 1.2
2024_20_1 7,199,422 1.1
2024_11_1 3,848,414 0.6
2024_7_2 3,758,402 0.6
2024_9_2 2,497,020 0.4
2024_7_1 2,466,893 0.4
2024_19_2 2,242,690 0.3
2024_19_1 2,242,630 0.3
2024_2_2 1,962,905 0.3
2024_15_1 1,913,992 0.3
2024_2_1 1,741,514 0.3
2024_5_2 1,718,988 0.3
2024_5_1 1,605,046 0.2
2024_18_2 1,427,027 0.2
2024_4_1 1,213,771 0.2
2024_18_1 1,103,427 0.2
2024_10_2 1,089,386 0.2
2024_10_1 1,087,487 0.2
2024_1_2 1,070,180 0.2
2024_1_1 1,041,088 0.2
2024_13_1 910,454 0.1
2024_13_2 910,208 0.1
2024_9_1 897,540 0.1
2024_6_1 641,454 0.1
2024_14_1 593,112 0.1
2024_4_2 453,901 0.1
2024_3_2 317,752 0.0
2024_3_1 227,379 0.0
2024_17_2 218,087 0.0
2024_8_2 86,862 0.0
2024_8_1 44,405 0.0
2024_17_1 11,725 0.0

Please sign in to comment.