Skip to content

Commit

Permalink
Optimize 2024-06
Browse files Browse the repository at this point in the history
  • Loading branch information
fornwall committed Dec 7, 2024
1 parent ae2519b commit 044ac34
Showing 1 changed file with 56 additions and 1 deletion.
57 changes: 56 additions & 1 deletion crates/core/src/year2024/day06.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::common::u256::U256;
use crate::input::{on_error, Input};

const DIRECTIONS: [(i32, i32); 4] = [(0, -1), (1, 0), (0, 1), (-1, 0)];
const JUMP_MAP_WIDTH: usize = 192;

pub fn solve(input: &Input) -> Result<u32, String> {
let width = input
Expand All @@ -23,6 +24,8 @@ pub fn solve(input: &Input) -> Result<u32, String> {
let mut visited = [U256::default(); 192];
// Indexed by direction idx:
let mut repeated_visit = [[U256::default(); 192]; 4];
// Indexed by direction idx - how many positions to jump if not obstacle placed:
let mut jump_map = [[u8::MAX; JUMP_MAP_WIDTH * JUMP_MAP_WIDTH]; 4];

let mut current_direction_idx = 0;
let mut current_position = (0, 0);
Expand All @@ -43,6 +46,9 @@ pub fn solve(input: &Input) -> Result<u32, String> {

let mut num_loops = 0;

let mut current_jumps_len = 0;
let mut current_jumps_start_position = (0, 0);

loop {
let dir = DIRECTIONS[current_direction_idx];
let new_position = (current_position.0 + dir.0, current_position.1 + dir.1);
Expand All @@ -51,6 +57,11 @@ pub fn solve(input: &Input) -> Result<u32, String> {
break;
}
b'#' => {
jump_map[current_direction_idx][current_jumps_start_position.1 as usize
* JUMP_MAP_WIDTH
+ current_jumps_start_position.0 as usize] = current_jumps_len;
current_jumps_len = 0;

current_direction_idx = (current_direction_idx + 1) % 4;
repeated_visit[current_direction_idx][current_position.1 as usize]
.set_bit(current_position.0 as usize);
Expand All @@ -67,6 +78,7 @@ pub fn solve(input: &Input) -> Result<u32, String> {
placed_obstacles[new_position.1 as usize].set_bit(new_position.0 as usize);
if does_movements_repeat(
&grid,
&mut jump_map,
&repeated_visit,
new_position,
current_position,
Expand All @@ -75,6 +87,10 @@ pub fn solve(input: &Input) -> Result<u32, String> {
num_loops += 1;
}
}
if current_jumps_len == 0 {
current_jumps_start_position = current_position;
}
current_jumps_len += 1;
current_position = new_position;
visited[current_position.1 as usize].set_bit(current_position.0 as usize);
}
Expand All @@ -90,6 +106,7 @@ pub fn solve(input: &Input) -> Result<u32, String> {

fn does_movements_repeat(
grid: &Grid,
jump_map: &mut [[u8; JUMP_MAP_WIDTH * JUMP_MAP_WIDTH]; 4],
repeated_visit: &[[U256; 192]; 4],
obstacle_position: (i32, i32),
mut current_position: (i32, i32),
Expand All @@ -99,11 +116,22 @@ fn does_movements_repeat(
repeated_visit[current_direction_idx][current_position.1 as usize]
.set_bit(current_position.0 as usize);

let mut current_jumps_len = 0;
let mut current_jumps_start_position = (0, 0);

loop {
let dir = DIRECTIONS[current_direction_idx];
let new_position = (current_position.0 + dir.0, current_position.1 + dir.1);
match (new_position == obstacle_position, grid.at(new_position)) {
let stopped_by_placed_obstacle = new_position == obstacle_position;

match (stopped_by_placed_obstacle, grid.at(new_position)) {
(true, _) | (_, b'#') => {
if !stopped_by_placed_obstacle {
jump_map[current_direction_idx][current_jumps_start_position.1 as usize
* JUMP_MAP_WIDTH
+ current_jumps_start_position.0 as usize] = current_jumps_len;
}

current_direction_idx = (current_direction_idx + 1) % 4;
if repeated_visit[current_direction_idx][current_position.1 as usize]
.is_bit_set(current_position.0 as usize)
Expand All @@ -112,11 +140,38 @@ fn does_movements_repeat(
}
repeated_visit[current_direction_idx][current_position.1 as usize]
.set_bit(current_position.0 as usize);

let mut jump_len = jump_map[current_direction_idx]
[current_position.1 as usize * JUMP_MAP_WIDTH + current_position.0 as usize];
if jump_len != u8::MAX {
let jump_dir = DIRECTIONS[current_direction_idx];
// Check if placed obstacle blocks jump:
let dist = (
obstacle_position.0 - current_position.0,
obstacle_position.1 - current_position.1,
);
if (dist.0 == 0 || dist.1 == 0)
&& (dist.0.signum(), dist.1.signum())
== (jump_dir.0.signum(), jump_dir.1.signum())
{
jump_len = (dist.0.abs() + dist.1.abs() - 1).min(jump_len as i32) as u8;
}

current_position = (
current_position.0 + jump_dir.0 * jump_len as i32,
current_position.1 + jump_dir.1 * jump_len as i32,
);
}
current_jumps_len = 0;
}
(false, b'L') => {
return false;
}
_ => {
if current_jumps_len == 0 {
current_jumps_start_position = current_position;
}
current_jumps_len += 1;
current_position = new_position;
}
}
Expand Down

1 comment on commit 044ac34

@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_8_1           10,000,000           10,000,000            0
   2024_8_2           10,000,000           10,000,000            0
   2024_9_1           10,000,000           10,000,000            0
   2024_9_2           10,000,000           10,000,000            0
  2024_10_1           10,000,000           10,000,000            0
  2024_10_2           10,000,000           10,000,000            0
  2024_11_1           10,000,000           10,000,000            0
  2024_11_2           10,000,000           10,000,000            0
  2024_12_1           10,000,000           10,000,000            0
  2024_12_2           10,000,000           10,000,000            0
  2024_13_1           10,000,000           10,000,000            0
  2024_13_2           10,000,000           10,000,000            0
  2024_14_1           10,000,000           10,000,000            0
  2024_14_2           10,000,000           10,000,000            0
  2024_15_1           10,000,000           10,000,000            0
  2024_15_2           10,000,000           10,000,000            0
  2024_16_1           10,000,000           10,000,000            0
  2024_16_2           10,000,000           10,000,000            0
  2024_17_1           10,000,000           10,000,000            0
  2024_17_2           10,000,000           10,000,000            0
  2024_18_1           10,000,000           10,000,000            0
  2024_18_2           10,000,000           10,000,000            0
  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,082            1,041,054            0
   2024_1_2            1,070,174            1,070,146            0
   2024_2_1            1,641,500            1,641,472            0
   2024_2_2            1,863,578            1,863,550            0
   2024_3_1              227,376              227,348            0
   2024_3_2              317,748              317,720            0
   2024_4_1            1,213,767            1,213,739            0
   2024_4_2              453,897              453,869            0
   2024_5_1            1,605,042            1,605,014            0
   2024_5_2            1,718,984            1,718,956            0
-  2024_6_1              421,265              641,421          +52
+  2024_6_2          223,808,430          141,389,053          -36
   2024_7_1            2,466,859            2,466,859            0
   2024_7_2            3,758,368            3,758,368            0
Benchmark Instructions (count) Instructions (%)
2024_6_2 141,389,053 27.8
2024_9_2 10,000,000 2.0
2024_9_1 10,000,000 2.0
2024_8_2 10,000,000 2.0
2024_8_1 10,000,000 2.0
2024_25_1 10,000,000 2.0
2024_24_2 10,000,000 2.0
2024_24_1 10,000,000 2.0
2024_23_2 10,000,000 2.0
2024_23_1 10,000,000 2.0
2024_22_2 10,000,000 2.0
2024_22_1 10,000,000 2.0
2024_21_2 10,000,000 2.0
2024_21_1 10,000,000 2.0
2024_20_2 10,000,000 2.0
2024_20_1 10,000,000 2.0
2024_19_2 10,000,000 2.0
2024_19_1 10,000,000 2.0
2024_18_2 10,000,000 2.0
2024_18_1 10,000,000 2.0
2024_17_2 10,000,000 2.0
2024_17_1 10,000,000 2.0
2024_16_2 10,000,000 2.0
2024_16_1 10,000,000 2.0
2024_15_2 10,000,000 2.0
2024_15_1 10,000,000 2.0
2024_14_2 10,000,000 2.0
2024_14_1 10,000,000 2.0
2024_13_2 10,000,000 2.0
2024_13_1 10,000,000 2.0
2024_12_2 10,000,000 2.0
2024_12_1 10,000,000 2.0
2024_11_2 10,000,000 2.0
2024_11_1 10,000,000 2.0
2024_10_2 10,000,000 2.0
2024_10_1 10,000,000 2.0
2024_7_2 3,758,368 0.7
2024_7_1 2,466,859 0.5
2024_2_2 1,863,550 0.4
2024_5_2 1,718,956 0.3
2024_2_1 1,641,472 0.3
2024_5_1 1,605,014 0.3
2024_4_1 1,213,739 0.2
2024_1_2 1,070,146 0.2
2024_1_1 1,041,054 0.2
2024_6_1 641,421 0.1
2024_4_2 453,869 0.1
2024_3_2 317,720 0.1
2024_3_1 227,348 0.0

Please sign in to comment.