-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
192 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
MMMSXXMASM | ||
MSAMXMSMSA | ||
AMXSXMAAMM | ||
MSAMASMSMX | ||
XMASAMXAMM | ||
XXAMMXXAMA | ||
SMSMSASXSS | ||
SAXAMASAAA | ||
MAMMMXMMMM | ||
MXMXAXMASX |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -216,4 +216,9 @@ main! { | |
"jq" => jq!("d3.jq", "d3q2"), | ||
} | ||
} | ||
day 4 { | ||
part 1 { | ||
"brute" => d4::p1_brute, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use std::array; | ||
|
||
use crate::util::{DirectDiagonal, GridView}; | ||
|
||
pub fn p1_brute(input: String) -> u32 { | ||
let grid = GridView::new(&input); | ||
|
||
let mut count = 0; | ||
|
||
for (index, _) in input.match_indices('X') { | ||
let loc = grid.index_to_loc(index).unwrap(); | ||
for dir in DirectDiagonal::ALL { | ||
let mut iter = loc.direct_diagonal_iter(dir, grid).skip(1).take(3).map(|loc| grid.get(loc)); | ||
let chars: [_; 3] = array::from_fn(|_| iter.next().flatten()); | ||
if chars == [Some(b'M'), Some(b'A'), Some(b'S')] { | ||
println!("Match: {dir:?} {loc:?}"); | ||
count += 1; | ||
} | ||
} | ||
} | ||
|
||
count | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
pub mod all; | ||
use all::Parse; | ||
pub use all::{bench, run}; | ||
|
||
mod util; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
#![allow(dead_code)] | ||
|
||
use std::iter; | ||
|
||
#[derive(Clone, Copy)] | ||
pub struct GridView<'a> { | ||
input: &'a [u8], | ||
width: u32, | ||
height: u32, | ||
} | ||
|
||
impl<'a> GridView<'a> { | ||
pub fn new(input: &'a impl AsRef<[u8]>) -> Self { | ||
let input = input.as_ref(); | ||
let width= input.iter().position(|&b| b == b'\n').unwrap() as u32+1; | ||
Self { | ||
input, | ||
width, | ||
height: (input.len() as u32).div_ceil(width), | ||
} | ||
} | ||
|
||
pub fn get(&self, loc: GridLoc) -> Option<u8> { | ||
self.input.get((loc.y * self.width + loc.x) as usize).copied() | ||
} | ||
|
||
pub fn index_to_loc(&self, index: usize) -> Option<GridLoc> { | ||
let y = index as u32 / self.width; | ||
let x = index as u32 % self.width; | ||
if x < self.width - 1 && y < self.height { | ||
Some(GridLoc { x, y }) | ||
} else { | ||
None | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
pub struct GridLoc { | ||
x: u32, | ||
y: u32, | ||
} | ||
|
||
impl GridLoc { | ||
pub fn left(self) -> Option<Self> { | ||
Some(Self { | ||
x: self.x.checked_sub(1)?, | ||
y: self.y, | ||
}) | ||
} | ||
pub fn right(self, grid: GridView) -> Option<Self> { | ||
Some(Self { | ||
x: match self.x.checked_add(1)? { | ||
x if x < grid.width - 1 => x, | ||
_ => return None, | ||
}, | ||
y: self.y, | ||
}) | ||
} | ||
pub fn up(self) -> Option<Self> { | ||
Some(Self { | ||
x: self.x, | ||
y: self.y.checked_sub(1)?, | ||
}) | ||
} | ||
pub fn down(self, grid: GridView) -> Option<Self> { | ||
Some(Self { | ||
x: self.x, | ||
y: match self.y.checked_add(1)? { | ||
y if y < grid.height - 1 => y, | ||
_ => return None, | ||
}, | ||
}) | ||
} | ||
|
||
pub fn direct(self, direct: Direct, grid: GridView) -> Option<Self> { | ||
match direct { | ||
Direct::Left => self.left(), | ||
Direct::Right => self.right(grid), | ||
Direct::Up => self.up(), | ||
Direct::Down => self.down(grid), | ||
} | ||
} | ||
|
||
pub fn direct_diagonal(self, direct: DirectDiagonal, grid: GridView) -> Option<Self> { | ||
match direct { | ||
DirectDiagonal::Left => self.left(), | ||
DirectDiagonal::Right => self.right(grid), | ||
DirectDiagonal::Up => self.up(), | ||
DirectDiagonal::Down => self.down(grid), | ||
DirectDiagonal::LeftUp => self.left().and_then(Self::up), | ||
DirectDiagonal::RightUp => self.right(grid).and_then(Self::up), | ||
DirectDiagonal::LeftDown => self.left().and_then(|loc| loc.down(grid)), | ||
DirectDiagonal::RightDown => self.right(grid).and_then(|loc| loc.down(grid)), | ||
} | ||
} | ||
|
||
fn direct_any_iter<F>(self, mutate: F, grid: GridView) -> impl Iterator<Item = Self> + use<'_, F> | ||
where F: Fn(GridLoc, GridView) -> Option<GridLoc>{ | ||
let mut loc = Some(self); | ||
iter::from_fn(move || { | ||
let output = loc?; | ||
loc = mutate(output, grid); | ||
Some(output) | ||
}) | ||
} | ||
|
||
pub fn direct_iter(self, direct: Direct, grid: GridView) -> impl Iterator<Item = Self> + use<'_> { | ||
self.direct_any_iter(move |loc, grid| loc.direct(direct, grid), grid) | ||
} | ||
|
||
pub fn direct_diagonal_iter(self, direct: DirectDiagonal, grid: GridView) -> impl Iterator<Item = Self> + use<'_> { | ||
self.direct_any_iter(move |loc, grid| loc.direct_diagonal(direct, grid), grid) | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
pub enum Direct { | ||
Left, | ||
Right, | ||
Up, | ||
Down, | ||
} | ||
|
||
impl Direct { | ||
pub const ALL: [Self; 4] = [Self::Left, Self::Right, Self::Up, Self::Down]; | ||
} | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
pub enum DirectDiagonal { | ||
Left, | ||
Right, | ||
Up, | ||
Down, | ||
LeftUp, | ||
RightUp, | ||
LeftDown, | ||
RightDown, | ||
} | ||
|
||
impl DirectDiagonal { | ||
pub const ALL: [Self; 8] = [ | ||
Self::Left, Self::Right, Self::Up, Self::Down, | ||
Self::LeftUp, Self::RightUp, Self::LeftDown, Self::RightDown, | ||
]; | ||
} |