Skip to content

Commit

Permalink
Solve part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
alion02 committed Dec 6, 2024
1 parent 11ced7a commit 3a9c86f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 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 day6(c: &mut Criterion) {
let s = read_to_string("./inputs/6.txt").unwrap();
let s = s.as_str();

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

// assert_eq!(
// part1(s).to_string(),
// read_to_string("./outputs/6p1.txt").unwrap(),
// );
assert_eq!(
part1(s).to_string(),
read_to_string("./outputs/6p1.txt").unwrap(),
);
// assert_eq!(
// part2(s).to_string(),
// read_to_string("./outputs/6p2.txt").unwrap(),
Expand Down
55 changes: 54 additions & 1 deletion src/day6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,60 @@ use super::*;
#[target_feature(enable = "avx2,bmi1,bmi2,cmpxchg16b,lzcnt,movbe,popcnt")]
#[allow(unreachable_code)]
unsafe fn inner1(s: &[u8]) -> u32 {
0
let mut loc = s.iter().position(|b| *b == b'^').unwrap_unchecked();
let mut new = [true; 131 * 130];
let mut total = 0;
'outer: loop {
loop {
total += *new.get_unchecked(loc) as u32;
*new.get_unchecked_mut(loc) = false;
let next = loc.wrapping_sub(131);
if next >= s.len() {
break 'outer;
}
if *s.get_unchecked(next) == b'#' {
break;
}
loc = next;
}
loop {
total += *new.get_unchecked(loc) as u32;
*new.get_unchecked_mut(loc) = false;
let next = loc.wrapping_add(1);
if *s.get_unchecked(next) == b'\n' {
break 'outer;
}
if *s.get_unchecked(next) == b'#' {
break;
}
loc = next;
}
loop {
total += *new.get_unchecked(loc) as u32;
*new.get_unchecked_mut(loc) = false;
let next = loc.wrapping_add(131);
if next >= s.len() {
break 'outer;
}
if *s.get_unchecked(next) == b'#' {
break;
}
loc = next;
}
loop {
total += *new.get_unchecked(loc) as u32;
*new.get_unchecked_mut(loc) = false;
let next = loc.wrapping_sub(1);
if *s.get_unchecked(next) == b'\n' {
break 'outer;
}
if *s.get_unchecked(next) == b'#' {
break;
}
loc = next;
}
}
total
}

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

0 comments on commit 3a9c86f

Please sign in to comment.