Skip to content

Commit

Permalink
day21
Browse files Browse the repository at this point in the history
  • Loading branch information
vslinko committed Dec 21, 2024
1 parent c79f6f8 commit a16f2c0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 63 deletions.
24 changes: 12 additions & 12 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ macro_rules! benches {

#[rustfmt::skip]
benches!(
9, "9.txt",
10, "10.txt",
11, "11.txt",
12, "12.txt",
13, "13.txt", "13_simd", "13.txt",
14, "14.txt",
15, "15.txt",
16, "16.txt",
17, "17.txt",
18, "18.txt",
19, "19.txt",
20, "20.txt",
// 9, "9.txt",
// 10, "10.txt",
// 11, "11.txt",
// 12, "12.txt",
// 13, "13.txt", "13_simd", "13.txt",
// 14, "14.txt",
// 15, "15.txt",
// 16, "16.txt",
// 17, "17.txt",
// 18, "18.txt",
// 19, "19.txt",
// 20, "20.txt",
21, "21.txt"
);
44 changes: 14 additions & 30 deletions src/day21.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,29 @@
use std::mem::transmute;
use std::simd::prelude::*;

const LUT1: [u64; 1000] = unsafe { transmute(*include_bytes!("../luts/day21_1.bin")) };
const LUT2: [u64; 1000] = unsafe { transmute(*include_bytes!("../luts/day21_2.bin")) };

#[cfg_attr(
target_arch = "x86_64",
target_feature(enable = "popcnt,avx2,ssse3,bmi1,bmi2,lzcnt")
)]
#[cfg_attr(avx512_available, target_feature(enable = "avx512vl"))]
unsafe fn solve(input: &str, lut: &[u64]) -> u64 {
let input = input.as_bytes();

let u1 = usizex8::splat(1);
let u10 = usizex8::splat(10);
let u100 = usizex8::splat(100);
let u5328 = usizex8::splat(5328); // 100 * 48 + 10 * 48 + 48

let first_buttons_idxs = usizex8::from_array([0, 5, 10, 15, 20, 25, 30, 35]);
let second_buttons_idxs = first_buttons_idxs + u1;
let third_buttons_idxs = second_buttons_idxs + u1;

let first_buttons = u8x8::gather_or_default(&input, first_buttons_idxs);
let second_buttons = u8x8::gather_or_default(&input, second_buttons_idxs);
let third_buttons = u8x8::gather_or_default(&input, third_buttons_idxs);

let first_buttons = first_buttons.cast();
let second_buttons = second_buttons.cast();
let third_buttons = third_buttons.cast();

let num = first_buttons * u100 + second_buttons * u10 + third_buttons - u5328;
unsafe fn solve(input: &[u8], lut: &[u64; 1000]) -> u64 {
macro_rules! solve {
($a:expr, $b:expr, $c:expr) => {
lut.get_unchecked(
(*input.get_unchecked($a) as usize) * 100
+ (*input.get_unchecked($b) as usize) * 10
+ (*input.get_unchecked($c) as usize)
- 5328,
)
};
}

u64x8::gather_or_default(lut, num).reduce_sum()
solve!(0, 1, 2) + solve!(5, 6, 7) + solve!(10, 11, 12) + solve!(15, 16, 17) + solve!(20, 21, 22)
}

pub fn part1(input: &str) -> u64 {
unsafe { solve(input, &LUT1) }
unsafe { solve(input.as_bytes(), &LUT1) }
}

pub fn part2(input: &str) -> u64 {
unsafe { solve(input, &LUT2) }
unsafe { solve(input.as_bytes(), &LUT2) }
}

#[cfg(test)]
Expand Down
42 changes: 21 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
#![feature(portable_simd)]

pub mod day1;
pub mod day2;
pub mod day3;
pub mod day4;
pub mod day5;
pub mod day6;
pub mod day7;
pub mod day8;
pub mod day9;
// pub mod day1;
// pub mod day2;
// pub mod day3;
// pub mod day4;
// pub mod day5;
// pub mod day6;
// pub mod day7;
// pub mod day8;
// pub mod day9;

pub mod day10;
pub mod day11;
pub mod day12;
pub mod day13;
pub mod day13_simd;
pub mod day14;
pub mod day15;
pub mod day16;
pub mod day17;
pub mod day18;
pub mod day19;
pub mod day20;
// pub mod day10;
// pub mod day11;
// pub mod day12;
// pub mod day13;
// pub mod day13_simd;
// pub mod day14;
// pub mod day15;
// pub mod day16;
// pub mod day17;
// pub mod day18;
// pub mod day19;
// pub mod day20;
pub mod day21;

0 comments on commit a16f2c0

Please sign in to comment.