Skip to content

Commit

Permalink
d6q1
Browse files Browse the repository at this point in the history
  • Loading branch information
SOF3 committed Dec 8, 2024
1 parent 751775f commit 608c8fa
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 5 deletions.
1 change: 1 addition & 0 deletions .github/workflows/bench.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ jobs:
name: run benchmark
runs-on: ubuntu-latest
steps:
- run: sudo apt install valgrind
- name: Check out base
uses: actions/checkout@v4
- name: Install Rust
Expand Down
10 changes: 10 additions & 0 deletions input/d6.sample.input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
....#.....
.........#
..........
..#.......
.......#..
..........
.#..^.....
........#.
#.........
......#...
7 changes: 7 additions & 0 deletions src/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,11 @@ macros::all! {
"btreemap-vec" => p1_btreemap_vec,
}
}
day 6 {
part 1 {
"ticked-fxhash-loc" => p1_ticked_fxhash_loc,
"ticked-fxhash-index" => p1_ticked_fxhash_index,
"ticked-bitvec" => p1_ticked_bitvec,
}
}
}
1 change: 0 additions & 1 deletion src/all/d5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ fn p1<DenyListsT: DenyLists, DisallowedSetT: DisallowedSet>(input: Input) -> u32
}

let mut result = 0;

let mut disallowed = DisallowedSetT::default();

'update: for Update(items) in input.updates {
Expand Down
89 changes: 89 additions & 0 deletions src/all/d6.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use std::{collections::HashSet, hash::BuildHasher};

use bitvec::vec::BitVec;
use rustc_hash::FxHashSet;

use crate::util::{DirectTaxicab, GridLoc, GridView};

trait Collector {
fn new(capacity: usize) -> Self;
fn insert(&mut self, loc: impl FnOnce() -> GridLoc, index: impl FnOnce() -> u32);
fn count(&self) -> u32;
}

fn p1_ticked<CollectorT: Collector>(input: String) -> u32 {
let grid = GridView::new(&input);

let mut loc = grid.index_to_loc(input.find('^').unwrap()).unwrap();
let mut direct = DirectTaxicab::Up;

let mut collector = CollectorT::new(input.len());
'ticks: loop {
collector.insert(|| loc, || grid.loc_to_index(loc));

'directs: loop {
match loc.direct(direct, grid) {
None => return collector.count(), // leave map
Some(new_loc) => {
match grid.get(new_loc).unwrap() {
b'^' | b'.' => {
loc = new_loc;
continue 'ticks
}
b'#' => {
direct = direct.clockwise();
continue 'directs
}
_ => unreachable!(),
}
}
}
}
}
}

impl<S: BuildHasher + Default> Collector for HashSet<GridLoc, S> {
fn new(capacity: usize) -> Self {
Self::with_capacity_and_hasher(capacity, S::default())
}

fn insert(&mut self, loc: impl FnOnce() -> GridLoc, _: impl FnOnce() -> u32) {
HashSet::insert(self, loc());
}

fn count(&self) -> u32 {
self.len() as u32
}
}

impl<S: BuildHasher + Default> Collector for HashSet<u32, S> {
fn new(capacity: usize) -> Self {
Self::with_capacity_and_hasher(capacity, S::default())
}

fn insert(&mut self, _: impl FnOnce() -> GridLoc, index: impl FnOnce() -> u32) {
HashSet::insert(self, index());
}

fn count(&self) -> u32 {
self.len() as u32
}
}

impl Collector for BitVec {
fn new(capacity: usize) -> Self {
Self::repeat(false, capacity)
}

fn insert(&mut self, _: impl FnOnce() -> GridLoc, index: impl FnOnce() -> u32) {
self.set(index() as usize, true);
}

fn count(&self) -> u32 {
self.count_ones() as u32
}
}

pub fn p1_ticked_fxhash_loc(input: String) -> u32 { p1_ticked::<FxHashSet<GridLoc>>(input) }
pub fn p1_ticked_fxhash_index(input: String) -> u32 { p1_ticked::<FxHashSet<u32>>(input) }
pub fn p1_ticked_bitvec(input: String) -> u32 { p1_ticked::<BitVec>(input) }
23 changes: 19 additions & 4 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ impl<'a> GridView<'a> {
}

pub fn get(&self, loc: GridLoc) -> Option<u8> {
self.input.get((loc.y * self.width + loc.x) as usize).copied()
self.input.get(self.loc_to_index(loc) as usize).copied()
}

pub fn loc_to_index(&self, loc: GridLoc) -> u32 {
loc.y * self.width + loc.x
}

pub fn index_to_loc(&self, index: usize) -> Option<GridLoc> {
Expand All @@ -31,10 +35,10 @@ impl<'a> GridView<'a> {
}
}

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct GridLoc {
x: u32,
y: u32,
pub x: u32,
pub y: u32,
}

impl GridLoc {
Expand Down Expand Up @@ -91,6 +95,17 @@ pub enum DirectTaxicab {
Down,
}

impl DirectTaxicab {
pub fn clockwise(self) -> Self {
match self {
Self::Left => Self::Up,
Self::Up => Self::Right,
Self::Right => Self::Down,
Self::Down => Self::Left,
}
}
}

impl Direct for DirectTaxicab {
const ALL: &[Self] = &[Self::Left, Self::Right, Self::Up, Self::Down];

Expand Down

0 comments on commit 608c8fa

Please sign in to comment.