Skip to content

Commit

Permalink
send it
Browse files Browse the repository at this point in the history
  • Loading branch information
alion02 committed Dec 6, 2024
1 parent 5dcc092 commit bc67c7e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 16 deletions.
10 changes: 5 additions & 5 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ pub fn day5(c: &mut Criterion) {
let s = read_to_string("./inputs/5.txt").unwrap();
let s = s.as_str();

c.bench_function("day5 part1 wip", |b| b.iter(|| part1(black_box(s))));
c.bench_function("day5 part1", |b| b.iter(|| part1(black_box(s))));
// c.bench_function("day5 part2", |b| b.iter(|| part2(black_box(s))));

// assert_eq!(
// part1(s).to_string(),
// read_to_string("./outputs/5p1.txt").unwrap(),
// );
assert_eq!(
part1(s).to_string(),
read_to_string("./outputs/5p1.txt").unwrap(),
);
// assert_eq!(
// part2(s).to_string(),
// read_to_string("./outputs/5p2.txt").unwrap(),
Expand Down
68 changes: 57 additions & 11 deletions src/day5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,23 +109,69 @@ unsafe fn inner1(s: &[u8]) -> u32 {
zero = in(ymm_reg) u8x32::splat(0),
);

let mut sum = 0;
let mut i = 0;
let table: &mut [u8; 1600] = transmute(matrix);

macro_rules! parse {
($off:expr$(, $on_newline:expr)?) => {{
let v1 = *s.get_unchecked(i + $off) as u32;
if v1 == b'\n' as u32 {
$($on_newline)?
}
let v2 = *s.get_unchecked(i + $off + 1) as u32;
v1 * 10 + v2 - 528
}};
}

loop {
let x1 = *s.get_unchecked(i + 0) as u32;
if x1 == b'\n' as u32 {
break;
}
let x2 = *s.get_unchecked(i + 1) as u32;
let y1 = *s.get_unchecked(i + 3) as u32;
let y2 = *s.get_unchecked(i + 4) as u32;
let x = x1 * 10 + x2 - 528;
let y = y1 * 10 + y2 - 528;
*table.get_unchecked_mut((x + y / 8) as usize) |= 1u8.wrapping_shl(y);
let x = parse!(0, break);
let y = parse!(3);
*table.get_unchecked_mut((x * 16 + y / 8) as usize) |= 1u8.wrapping_shl(y);
i += 6;
}

0
i += 1;

'outer: loop {
let [a, b, c, d, e] = [parse!(0), parse!(3), parse!(6), parse!(9), parse!(12)];
let mut j = i + 6;
i += 15;
let mut wrong_order =
*table.get_unchecked((b * 16 + a / 8) as usize) & 1u8.wrapping_shl(a) != 0;
wrong_order |= *table.get_unchecked((c * 16 + b / 8) as usize) & 1u8.wrapping_shl(b) != 0;
wrong_order |= *table.get_unchecked((d * 16 + c / 8) as usize) & 1u8.wrapping_shl(c) != 0;
wrong_order |= *table.get_unchecked((e * 16 + d / 8) as usize) & 1u8.wrapping_shl(d) != 0;

let mut p = e;
loop {
if *s.get_unchecked(i - 1) == b'\n' {
if i == s.len() {
break 'outer;
}
break;
}
let n1 = parse!(0);
let n2 = parse!(3);
i += 6;
j += 3;
wrong_order |=
*table.get_unchecked((n1 * 16 + p / 8) as usize) & 1u8.wrapping_shl(p) != 0;
wrong_order |=
*table.get_unchecked((n2 * 16 + n1 / 8) as usize) & 1u8.wrapping_shl(n1) != 0;
p = n2;
}

if !wrong_order {
sum += {
let v1 = *s.get_unchecked(j) as u32;
let v2 = *s.get_unchecked(j + 1) as u32;
v1 * 10 + v2 - 528
};
}
}

sum
}

#[target_feature(enable = "avx2,bmi1,bmi2,cmpxchg16b,lzcnt,movbe,popcnt")]
Expand Down

0 comments on commit bc67c7e

Please sign in to comment.