Skip to content

Commit

Permalink
Migrate all days to sqrid 0.0.24
Browse files Browse the repository at this point in the history
  • Loading branch information
lpenz committed Dec 23, 2023
1 parent f6432ea commit 214f56b
Show file tree
Hide file tree
Showing 16 changed files with 108 additions and 143 deletions.
40 changes: 8 additions & 32 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion day03/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ edition = "2021"
aoc = { path = "../aoc" }
color-eyre = "0.6.2"
nom = "7.1.3"
sqrid = "0.0.18"
sqrid = "0.0.24"
10 changes: 5 additions & 5 deletions day03/src/bin/day03a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ fn process(bufin: impl BufRead) -> Result<u32> {
let mut grid = Grid::default();
for (y, line) in input.into_iter().enumerate() {
for (x, cell) in line.into_iter().enumerate() {
let qa = Qa::try_from((x as u16, y as u16))?;
grid[qa] = cell;
let pos = Pos::try_from((x as u16, y as u16))?;
grid[pos] = cell;
}
}
let mut numbers = vec![];
// Look for symbols:
for qa_symbol in Qa::iter() {
for qa_symbol in Pos::iter() {
if !matches!(grid[qa_symbol], Cell::Symbol(_)) {
continue;
}
// Check adjacencies, with diagonals:
for qr in Qr::iter::<true>() {
let Ok(qa_adj) = qa_symbol + qr else { continue };
for dir in Dir::iter::<true>() {
let Ok(qa_adj) = qa_symbol + dir else { continue };
let Ok(number) = grid_get_number(&mut grid, qa_adj) else {
continue;
};
Expand Down
10 changes: 5 additions & 5 deletions day03/src/bin/day03b.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ fn process(bufin: impl BufRead) -> Result<u32> {
let mut grid = Grid::default();
for (y, line) in input.into_iter().enumerate() {
for (x, cell) in line.into_iter().enumerate() {
let qa = Qa::try_from((x as u16, y as u16))?;
grid[qa] = cell;
let pos = Pos::try_from((x as u16, y as u16))?;
grid[pos] = cell;
}
}
let mut numbers = vec![];
// Look for symbols:
for qa_symbol in Qa::iter() {
for qa_symbol in Pos::iter() {
if grid[qa_symbol] != Cell::Symbol('*') {
continue;
}
// Check adjacencies, with diagonals:
let mut this_gear = vec![];
for qr in Qr::iter::<true>() {
let Ok(qa_adj) = qa_symbol + qr else { continue };
for dir in Dir::iter::<true>() {
let Ok(qa_adj) = qa_symbol + dir else { continue };
let Ok(number) = grid_get_number(&mut grid, qa_adj) else {
continue;
};
Expand Down
22 changes: 11 additions & 11 deletions day03/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ impl fmt::Display for Cell {
}
}

pub use sqrid::Qr;
pub use sqrid::Dir;
pub type Sqrid = sqrid::sqrid_create!(140, 140, true);
pub type Qa = sqrid::qa_create!(Sqrid);
pub type Pos = sqrid::pos_create!(Sqrid);
pub type Grid = sqrid::grid_create!(Sqrid, Cell);

pub fn grid_get_number(grid: &mut Grid, qa_digit: Qa) -> Result<u32> {
pub fn grid_get_number(grid: &mut Grid, qa_digit: Pos) -> Result<u32> {
if !matches!(grid[qa_digit], Cell::Digit(_)) {
return Err(eyre!(
"cell {:?} with {} doesn't have a digit",
Expand All @@ -60,23 +60,23 @@ pub fn grid_get_number(grid: &mut Grid, qa_digit: Qa) -> Result<u32> {
));
}
// Found a digit, go left:
let qa_start = std::iter::successors(Some(qa_digit), |qa| {
(*qa + Qr::W)
let qa_start = std::iter::successors(Some(qa_digit), |pos| {
(*pos + Dir::W)
.ok()
.filter(|qa| matches!(grid[qa], Cell::Digit(_)))
.filter(|pos| matches!(grid[pos], Cell::Digit(_)))
})
.last()
.ok_or_else(|| eyre!("could not find first digit from {:?}", qa_digit))?;
// We are at the start of the number, pick up the digits
let number_str = Qa::iter_range(qa_start, Qa::BOTTOM_RIGHT)
.map_while(|qa| grid[qa].digit().ok())
let number_str = Pos::iter_range(qa_start, Pos::BOTTOM_RIGHT)
.map_while(|pos| grid[pos].digit().ok())
.collect::<String>();
// Empty the used cells:
for qa in Qa::iter_range(qa_start, Qa::BOTTOM_RIGHT) {
if grid[qa].digit().is_err() {
for pos in Pos::iter_range(qa_start, Pos::BOTTOM_RIGHT) {
if grid[pos].digit().is_err() {
break;
}
grid[qa] = Cell::Empty;
grid[pos] = Cell::Empty;
}
number_str
.parse::<u32>()
Expand Down
2 changes: 1 addition & 1 deletion day10/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ aoc = { path = "../aoc" }
color-eyre = "0.6.2"
itertools = "0.12.0"
nom = "7.1.3"
sqrid = "0.0.18"
sqrid = "0.0.24"
24 changes: 12 additions & 12 deletions day10/src/bin/day10a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@ use day10::*;
fn process(bufin: impl BufRead) -> Result<usize> {
let input = parser::parse(bufin)?;
let mut grid = Grid::default();
let mut start = Qa::default();
let mut start = Pos::default();
for (y, line) in input.into_iter().enumerate() {
for (x, cell) in line.into_iter().enumerate() {
let qa = Qa::try_from((x as u16, y as u16))?;
grid[qa] = cell;
let pos = Pos::try_from((x as u16, y as u16))?;
grid[pos] = cell;
if cell == Cell::Start {
start = qa;
start = pos;
}
}
}
for qr0 in [Qr::N, Qr::E, Qr::S, Qr::W] {
for qr0 in [Dir::N, Dir::E, Dir::S, Dir::W] {
let mut steps = 0;
let mut qr = qr0;
let mut qa = start;
while let Ok(next_qa) = qa + qr {
qa = next_qa;
let mut dir = qr0;
let mut pos = start;
while let Ok(next_qa) = pos + dir {
pos = next_qa;
steps += 1;
if qa == start {
if pos == start {
return Ok(steps / 2);
}
if let Some(next_qr) = next_qr(&grid, qa, qr) {
qr = next_qr;
if let Some(next_qr) = next_qr(&grid, pos, dir) {
dir = next_qr;
} else {
break;
}
Expand Down
50 changes: 25 additions & 25 deletions day10/src/bin/day10b.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ use itertools::Itertools;
#[derive(Debug, Default, Clone, Copy)]
pub struct P(pub f64, pub f64);

impl From<Qa> for P {
fn from(qa: Qa) -> P {
(&qa).into()
impl From<Pos> for P {
fn from(pos: Pos) -> P {
(&pos).into()
}
}

impl From<&Qa> for P {
fn from(qa: &Qa) -> P {
let t = qa.tuple();
impl From<&Pos> for P {
fn from(pos: &Pos) -> P {
let t = pos.tuple();
P(t.0 as f64, t.1 as f64)
}
}
Expand All @@ -33,19 +33,19 @@ pub fn intersect(u: &LineSeg, v: &LineSeg) -> bool {
ccw(u.0, v.0, v.1) != ccw(u.1, v.0, v.1) && ccw(u.0, u.1, v.0) != ccw(u.0, u.1, v.1)
}

fn calc_pipe(grid: &Grid, start: Qa) -> Vec<Qa> {
for qr0 in [Qr::N, Qr::E, Qr::S, Qr::W] {
fn calc_pipe(grid: &Grid, start: Pos) -> Vec<Pos> {
for qr0 in [Dir::N, Dir::E, Dir::S, Dir::W] {
let mut pipe = vec![start];
let mut qr = qr0;
let mut qa = start;
while let Ok(next_qa) = qa + qr {
qa = next_qa;
pipe.push(qa);
if qa == start {
let mut dir = qr0;
let mut pos = start;
while let Ok(next_qa) = pos + dir {
pos = next_qa;
pipe.push(pos);
if pos == start {
return pipe;
}
if let Some(next_qr) = next_qr(grid, qa, qr) {
qr = next_qr;
if let Some(next_qr) = next_qr(grid, pos, dir) {
dir = next_qr;
} else {
break;
}
Expand All @@ -57,14 +57,14 @@ fn calc_pipe(grid: &Grid, start: Qa) -> Vec<Qa> {
fn process(bufin: impl BufRead) -> Result<usize> {
let input = parser::parse(bufin)?;
let mut grid = Grid::default();
let mut start = Qa::default();
let botright = Qa::try_from((input[0].len() as u16 - 1, input.len() as u16 - 1)).unwrap();
let mut start = Pos::default();
let botright = Pos::try_from((input[0].len() as u16 - 1, input.len() as u16 - 1)).unwrap();
for (y, line) in input.into_iter().enumerate() {
for (x, cell) in line.into_iter().enumerate() {
let qa = Qa::try_from((x as u16, y as u16))?;
grid[qa] = cell;
let pos = Pos::try_from((x as u16, y as u16))?;
grid[pos] = cell;
if cell == Cell::Start {
start = qa;
start = pos;
}
}
}
Expand All @@ -78,10 +78,10 @@ fn process(bufin: impl BufRead) -> Result<usize> {
pipe[0].into(),
)))
.collect::<Vec<_>>();
Ok(Qa::iter_range(Qa::TOP_LEFT, botright)
.filter(|qa| !pipe.contains(qa))
.filter(|qa| {
let qaline = LineSeg(qa.into(), P::default());
Ok(Pos::iter_range(Pos::TOP_LEFT, botright)
.filter(|pos| !pipe.contains(pos))
.filter(|pos| {
let qaline = LineSeg(pos.into(), P::default());
let intersections = linesegs
.iter()
.filter(|polyline| intersect(&qaline, polyline))
Expand Down
32 changes: 16 additions & 16 deletions day10/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,25 @@ impl Cell {
}
}

pub use sqrid::Qr;
pub use sqrid::Dir;
pub type Sqrid = sqrid::sqrid_create!(140, 140, false);
pub type Qa = sqrid::qa_create!(Sqrid);
pub type Pos = sqrid::pos_create!(Sqrid);
pub type Grid = sqrid::grid_create!(Sqrid, Cell);

pub fn next_qr(grid: &Grid, qa: Qa, qr: Qr) -> Option<Qr> {
match (grid[qa], qr) {
(Cell::NS, Qr::N) => Some(Qr::N),
(Cell::NS, Qr::S) => Some(Qr::S),
(Cell::EW, Qr::E) => Some(Qr::E),
(Cell::EW, Qr::W) => Some(Qr::W),
(Cell::NE, Qr::W) => Some(Qr::N),
(Cell::NE, Qr::S) => Some(Qr::E),
(Cell::NW, Qr::E) => Some(Qr::N),
(Cell::NW, Qr::S) => Some(Qr::W),
(Cell::SW, Qr::E) => Some(Qr::S),
(Cell::SW, Qr::N) => Some(Qr::W),
(Cell::SE, Qr::W) => Some(Qr::S),
(Cell::SE, Qr::N) => Some(Qr::E),
pub fn next_qr(grid: &Grid, pos: Pos, dir: Dir) -> Option<Dir> {
match (grid[pos], dir) {
(Cell::NS, Dir::N) => Some(Dir::N),
(Cell::NS, Dir::S) => Some(Dir::S),
(Cell::EW, Dir::E) => Some(Dir::E),
(Cell::EW, Dir::W) => Some(Dir::W),
(Cell::NE, Dir::W) => Some(Dir::N),
(Cell::NE, Dir::S) => Some(Dir::E),
(Cell::NW, Dir::E) => Some(Dir::N),
(Cell::NW, Dir::S) => Some(Dir::W),
(Cell::SW, Dir::E) => Some(Dir::S),
(Cell::SW, Dir::N) => Some(Dir::W),
(Cell::SE, Dir::W) => Some(Dir::S),
(Cell::SE, Dir::N) => Some(Dir::E),
_ => None,
}
}
Expand Down
2 changes: 1 addition & 1 deletion day14/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ edition = "2021"
aoc = { path = "../aoc" }
color-eyre = "0.6.2"
nom = "7.1.3"
sqrid = "0.0.21"
sqrid = "0.0.24"
2 changes: 1 addition & 1 deletion day14/src/bin/day14a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use day14::*;
fn process(size: usize, bufin: impl BufRead) -> Result<usize> {
let input = parser::parse(bufin)?;
let mut grid = Grid::try_from(input)?;
grid = tilt(size, grid, Qr::N);
grid = tilt(size, grid, Dir::N);
Ok(grid_load(size, &grid))
}

Expand Down
Loading

0 comments on commit 214f56b

Please sign in to comment.