From 214f56b294ba7e14f7f2d21fe1e2142266f67b1d Mon Sep 17 00:00:00 2001 From: Leandro Lisboa Penz Date: Sat, 23 Dec 2023 18:47:17 +0000 Subject: [PATCH] Migrate all days to sqrid 0.0.24 --- Cargo.lock | 40 +++++++-------------------------- day03/Cargo.toml | 2 +- day03/src/bin/day03a.rs | 10 ++++----- day03/src/bin/day03b.rs | 10 ++++----- day03/src/lib.rs | 22 +++++++++--------- day10/Cargo.toml | 2 +- day10/src/bin/day10a.rs | 24 ++++++++++---------- day10/src/bin/day10b.rs | 50 ++++++++++++++++++++--------------------- day10/src/lib.rs | 32 +++++++++++++------------- day14/Cargo.toml | 2 +- day14/src/bin/day14a.rs | 2 +- day14/src/bin/day14b.rs | 19 ++++------------ day14/src/lib.rs | 30 ++++++++++++------------- day16/Cargo.toml | 2 +- day17/Cargo.toml | 2 +- day18/Cargo.toml | 2 +- 16 files changed, 108 insertions(+), 143 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index de96f0e..92c18a5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -179,7 +179,7 @@ dependencies = [ "aoc", "color-eyre", "nom", - "sqrid 0.0.18", + "sqrid", ] [[package]] @@ -249,7 +249,7 @@ dependencies = [ "color-eyre", "itertools", "nom", - "sqrid 0.0.18", + "sqrid", ] [[package]] @@ -286,7 +286,7 @@ dependencies = [ "aoc", "color-eyre", "nom", - "sqrid 0.0.21", + "sqrid", ] [[package]] @@ -306,7 +306,7 @@ dependencies = [ "autofolder", "color-eyre", "nom", - "sqrid 0.0.22", + "sqrid", ] [[package]] @@ -316,7 +316,7 @@ dependencies = [ "aoc", "color-eyre", "nom", - "sqrid 0.0.23", + "sqrid", ] [[package]] @@ -327,7 +327,7 @@ dependencies = [ "autofolder", "color-eyre", "nom", - "sqrid 0.0.23", + "sqrid", ] [[package]] @@ -358,7 +358,7 @@ dependencies = [ "color-eyre", "nom", "rayon", - "sqrid 0.0.24", + "sqrid", ] [[package]] @@ -378,7 +378,7 @@ dependencies = [ "aoc", "color-eyre", "nom", - "sqrid 0.0.24", + "sqrid", ] [[package]] @@ -649,30 +649,6 @@ dependencies = [ "lazy_static", ] -[[package]] -name = "sqrid" -version = "0.0.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "42bcef427c0f62df62547ef60e3b463fd5f2fb62f814bb07c1685b1c1398220a" - -[[package]] -name = "sqrid" -version = "0.0.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f978bd43a6f98f1d3d8b8cd066a650aa8230a48182a703869e8c9fa1dac5f08b" - -[[package]] -name = "sqrid" -version = "0.0.22" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "92bfab44888e2fc47178beb71e1ad84bd2a97361a6943019a8602f214ae4c126" - -[[package]] -name = "sqrid" -version = "0.0.23" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cfa174f70d3ecc8171ef9784a74bac0689af9ce41b01d00732a132465f985f3a" - [[package]] name = "sqrid" version = "0.0.24" diff --git a/day03/Cargo.toml b/day03/Cargo.toml index f26345f..285c584 100644 --- a/day03/Cargo.toml +++ b/day03/Cargo.toml @@ -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" diff --git a/day03/src/bin/day03a.rs b/day03/src/bin/day03a.rs index 65ac43b..9b2051a 100644 --- a/day03/src/bin/day03a.rs +++ b/day03/src/bin/day03a.rs @@ -9,19 +9,19 @@ fn process(bufin: impl BufRead) -> Result { 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::() { - let Ok(qa_adj) = qa_symbol + qr else { continue }; + for dir in Dir::iter::() { + let Ok(qa_adj) = qa_symbol + dir else { continue }; let Ok(number) = grid_get_number(&mut grid, qa_adj) else { continue; }; diff --git a/day03/src/bin/day03b.rs b/day03/src/bin/day03b.rs index becb0fd..21c9f5d 100644 --- a/day03/src/bin/day03b.rs +++ b/day03/src/bin/day03b.rs @@ -9,20 +9,20 @@ fn process(bufin: impl BufRead) -> Result { 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::() { - let Ok(qa_adj) = qa_symbol + qr else { continue }; + for dir in Dir::iter::() { + let Ok(qa_adj) = qa_symbol + dir else { continue }; let Ok(number) = grid_get_number(&mut grid, qa_adj) else { continue; }; diff --git a/day03/src/lib.rs b/day03/src/lib.rs index 257fb9c..b2657b8 100644 --- a/day03/src/lib.rs +++ b/day03/src/lib.rs @@ -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 { +pub fn grid_get_number(grid: &mut Grid, qa_digit: Pos) -> Result { if !matches!(grid[qa_digit], Cell::Digit(_)) { return Err(eyre!( "cell {:?} with {} doesn't have a digit", @@ -60,23 +60,23 @@ pub fn grid_get_number(grid: &mut Grid, qa_digit: Qa) -> Result { )); } // 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::(); // 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::() diff --git a/day10/Cargo.toml b/day10/Cargo.toml index 7f39445..757b757 100644 --- a/day10/Cargo.toml +++ b/day10/Cargo.toml @@ -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" diff --git a/day10/src/bin/day10a.rs b/day10/src/bin/day10a.rs index 9c00e4c..8f2c2c0 100644 --- a/day10/src/bin/day10a.rs +++ b/day10/src/bin/day10a.rs @@ -7,28 +7,28 @@ use day10::*; fn process(bufin: impl BufRead) -> Result { 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; } diff --git a/day10/src/bin/day10b.rs b/day10/src/bin/day10b.rs index accea1d..fd434ac 100644 --- a/day10/src/bin/day10b.rs +++ b/day10/src/bin/day10b.rs @@ -9,15 +9,15 @@ use itertools::Itertools; #[derive(Debug, Default, Clone, Copy)] pub struct P(pub f64, pub f64); -impl From for P { - fn from(qa: Qa) -> P { - (&qa).into() +impl From 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) } } @@ -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 { - for qr0 in [Qr::N, Qr::E, Qr::S, Qr::W] { +fn calc_pipe(grid: &Grid, start: Pos) -> Vec { + 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; } @@ -57,14 +57,14 @@ fn calc_pipe(grid: &Grid, start: Qa) -> Vec { fn process(bufin: impl BufRead) -> Result { 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; } } } @@ -78,10 +78,10 @@ fn process(bufin: impl BufRead) -> Result { pipe[0].into(), ))) .collect::>(); - 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)) diff --git a/day10/src/lib.rs b/day10/src/lib.rs index 739c3cf..a35404e 100644 --- a/day10/src/lib.rs +++ b/day10/src/lib.rs @@ -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 { - 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 { + 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, } } diff --git a/day14/Cargo.toml b/day14/Cargo.toml index 03e0f75..883d129 100644 --- a/day14/Cargo.toml +++ b/day14/Cargo.toml @@ -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" diff --git a/day14/src/bin/day14a.rs b/day14/src/bin/day14a.rs index c3029b4..ea4fd5a 100644 --- a/day14/src/bin/day14a.rs +++ b/day14/src/bin/day14a.rs @@ -7,7 +7,7 @@ use day14::*; fn process(size: usize, bufin: impl BufRead) -> Result { 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)) } diff --git a/day14/src/bin/day14b.rs b/day14/src/bin/day14b.rs index 4ef4a17..6c13042 100644 --- a/day14/src/bin/day14b.rs +++ b/day14/src/bin/day14b.rs @@ -6,32 +6,21 @@ use day14::*; use std::collections::HashMap; -pub use sqrid::Qr; +pub use sqrid::Dir; pub type Sqrid = sqrid::sqrid_create!(100, 100, false); -pub type Qa = sqrid::qa_create!(Sqrid); +pub type Pos = sqrid::pos_create!(Sqrid); pub type Grid = sqrid::grid_create!(Sqrid, Cell); -pub type SqridD = sqrid::sqrid_create!(10, 10, false); -pub type QaD = sqrid::qa_create!(SqridD); -pub type GridD = sqrid::grid_create!(SqridD, Cell); - const CYCLES: u64 = 1000000000; -fn _grid_display(grid: &Grid) { - let grid_d = QaD::iter() - .map(|qa| grid[Qa::try_from(qa.tuple()).unwrap()]) - .collect::(); - eprintln!("{}", grid_d); -} - fn process(size: usize, bufin: impl BufRead) -> Result { let input = parser::parse(bufin)?; let mut grid = Grid::try_from(input)?; let mut cache = HashMap::::default(); let mut icycle = 0; while icycle < CYCLES { - for qr in [Qr::N, Qr::W, Qr::S, Qr::E] { - grid = tilt(size, grid, qr); + for dir in [Dir::N, Dir::W, Dir::S, Dir::E] { + grid = tilt(size, grid, dir); } if icycle < CYCLES / 2 { if let Some(first) = cache.get(&grid) { diff --git a/day14/src/lib.rs b/day14/src/lib.rs index 6f4a358..a66af1d 100644 --- a/day14/src/lib.rs +++ b/day14/src/lib.rs @@ -38,9 +38,9 @@ impl TryFrom for Cell { } } -pub use sqrid::Qr; +pub use sqrid::Dir; pub type Sqrid = sqrid::sqrid_create!(100, 100, false); -pub type Qa = sqrid::qa_create!(Sqrid); +pub type Pos = sqrid::pos_create!(Sqrid); pub type Grid = sqrid::grid_create!(Sqrid, Cell); impl fmt::Display for Cell { @@ -82,38 +82,38 @@ fn test() -> Result<()> { Ok(()) } -pub fn tilt(size: usize, mut grid: Grid, qr: Qr) -> Grid { - let qas = if qr == Qr::N || qr == Qr::W { - Qa::iter().collect::>() +pub fn tilt(size: usize, mut grid: Grid, dir: Dir) -> Grid { + let qas = if dir == Dir::N || dir == Dir::W { + Pos::iter().collect::>() } else { - Qa::iter().rev().collect::>() + Pos::iter().rev().collect::>() }; for qa_rock in qas { if grid[qa_rock] != Cell::Rock { continue; } - let mut qa = qa_rock; - while let Ok(qa_new) = qa + qr { + let mut pos = qa_rock; + while let Ok(qa_new) = pos + dir { let t = qa_new.tuple(); let t = (t.0 as usize, t.1 as usize); if t.0 >= size || t.1 >= size || grid[qa_new] != Cell::Empty { break; } - qa = qa_new; + pos = qa_new; } - if qa != qa_rock { + if pos != qa_rock { grid[qa_rock] = Cell::Empty; - grid[qa] = Cell::Rock; + grid[pos] = Cell::Rock; } } grid } pub fn grid_load(size: usize, grid: &Grid) -> usize { - Qa::iter() - .map(|qa| { - if grid[qa] == Cell::Rock { - let t = qa.tuple(); + Pos::iter() + .map(|pos| { + if grid[pos] == Cell::Rock { + let t = pos.tuple(); size - t.1 as usize } else { 0 diff --git a/day16/Cargo.toml b/day16/Cargo.toml index 3f2b112..ce8857f 100644 --- a/day16/Cargo.toml +++ b/day16/Cargo.toml @@ -8,4 +8,4 @@ aoc = { path = "../aoc" } autofolder = "0.5.0" color-eyre = "0.6.2" nom = "7.1.3" -sqrid = "0.0.22" +sqrid = "0.0.24" diff --git a/day17/Cargo.toml b/day17/Cargo.toml index 0f2ffc2..0cb6643 100644 --- a/day17/Cargo.toml +++ b/day17/Cargo.toml @@ -7,4 +7,4 @@ edition = "2021" aoc = { path = "../aoc" } color-eyre = "0.6.2" nom = "7.1.3" -sqrid = "0.0.23" +sqrid = "0.0.24" diff --git a/day18/Cargo.toml b/day18/Cargo.toml index 2341422..7dc828c 100644 --- a/day18/Cargo.toml +++ b/day18/Cargo.toml @@ -8,4 +8,4 @@ aoc = { path = "../aoc" } autofolder = "0.5.0" color-eyre = "0.6.2" nom = "7.1.3" -sqrid = "0.0.23" +sqrid = "0.0.24"