Skip to content

Commit

Permalink
Fix code to work with new version of dependencies and compiler/clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
lpenz committed Sep 10, 2024
1 parent a3f5edd commit 6068383
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 88 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ regex = "1.8.4"
anyhow = "1"

[dependencies.chrono]
version = "0.4.38"
version = "0.4.26"
default-features = false
features = ["clock"]

79 changes: 37 additions & 42 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ pub const MAX_WIDTH: u16 = 20;
pub const MAX_HEIGHT: u16 = 20;

pub type Sqrid = crate::sqrid_create!(MAX_WIDTH, MAX_HEIGHT, false);
pub type Qa = crate::qa_create!(Sqrid);
pub type Qr = crate::Qr;
pub type Pos = crate::pos_create!(Sqrid);
pub type Dir = crate::Dir;
pub type Gridbool = crate::gridbool_create!(Sqrid);

/* Cell *************************************************************/

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum Cell {
#[default]
Type0,
Type1,
Type2,
Expand All @@ -35,12 +36,6 @@ pub enum Cell {
Type13,
}

impl Default for Cell {
fn default() -> Cell {
Cell::Type0
}
}

impl From<Cell> for char {
fn from(cell: Cell) -> char {
match cell {
Expand Down Expand Up @@ -88,67 +83,67 @@ impl fmt::Display for Cell {
}

impl Cell {
pub fn enter(&self, dir: &Qr) -> Option<Qr> {
pub fn enter(&self, dir: &Dir) -> Option<Dir> {
let dir = *dir;
if dir == Qr::N {
if dir == Dir::N {
return None;
}
match self {
Cell::Type0 => None,
Cell::Type1 => Some(Qr::S),
Cell::Type1 => Some(Dir::S),
Cell::Type2 => match dir {
Qr::E => Some(Qr::E),
Qr::W => Some(Qr::W),
Dir::E => Some(Dir::E),
Dir::W => Some(Dir::W),
_ => None,
},
Cell::Type3 => match dir {
Qr::S => Some(Qr::S),
Dir::S => Some(Dir::S),
_ => None,
},
Cell::Type4 => match dir {
Qr::S => Some(Qr::W),
Qr::W => Some(Qr::S),
Dir::S => Some(Dir::W),
Dir::W => Some(Dir::S),
_ => None,
},
Cell::Type5 => match dir {
Qr::S => Some(Qr::E),
Qr::E => Some(Qr::S),
Dir::S => Some(Dir::E),
Dir::E => Some(Dir::S),
_ => None,
},
Cell::Type6 => match dir {
Qr::W => Some(Qr::W),
Qr::E => Some(Qr::E),
Dir::W => Some(Dir::W),
Dir::E => Some(Dir::E),
_ => None,
},
Cell::Type7 => match dir {
Qr::S => Some(Qr::S),
Qr::W => Some(Qr::S),
Dir::S => Some(Dir::S),
Dir::W => Some(Dir::S),
_ => None,
},
Cell::Type8 => match dir {
Qr::W => Some(Qr::S),
Qr::E => Some(Qr::S),
Dir::W => Some(Dir::S),
Dir::E => Some(Dir::S),
_ => None,
},
Cell::Type9 => match dir {
Qr::S => Some(Qr::S),
Qr::E => Some(Qr::S),
Dir::S => Some(Dir::S),
Dir::E => Some(Dir::S),
_ => None,
},
Cell::Type10 => match dir {
Qr::S => Some(Qr::W),
Dir::S => Some(Dir::W),
_ => None,
},
Cell::Type11 => match dir {
Qr::S => Some(Qr::E),
Dir::S => Some(Dir::E),
_ => None,
},
Cell::Type12 => match dir {
Qr::W => Some(Qr::S),
Dir::W => Some(Dir::S),
_ => None,
},
Cell::Type13 => match dir {
Qr::E => Some(Qr::S),
Dir::E => Some(Dir::S),
_ => None,
},
}
Expand Down Expand Up @@ -240,15 +235,15 @@ impl fmt::Display for Rotation {

#[derive(Debug, Default, Clone, Copy)]
pub struct Entity {
pub qa: Qa,
pub qr: Qr,
pub qa: Pos,
pub qr: Dir,
}

impl Entity {
pub fn step(&self, node: &Node) -> Option<Entity> {
let cell = node.grid[self.qa];
if let Some(qr) = cell.enter(&self.qr) {
if let Some(qa) = self.qa + qr {
if let Ok(qa) = self.qa + qr {
return Some(Entity { qa, qr });
}
}
Expand All @@ -269,8 +264,8 @@ impl fmt::Display for Entity {
}
}

impl AsRef<Qa> for Entity {
fn as_ref(&self) -> &Qa {
impl AsRef<Pos> for Entity {
fn as_ref(&self) -> &Pos {
&self.qa
}
}
Expand All @@ -295,15 +290,15 @@ impl<'a> Iterator for EntityPathIter<'a> {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Action {
Wait,
Rotate { qa: Qa, rot: Rotation },
Rotate { qa: Pos, rot: Rotation },
}

impl Action {
pub fn new(qa: Qa, rot: Rotation) -> Action {
pub fn new(qa: Pos, rot: Rotation) -> Action {
Action::Rotate { qa, rot }
}

pub fn qa(&self) -> Option<Qa> {
pub fn qa(&self) -> Option<Pos> {
match self {
Action::Rotate { qa, rot: _ } => Some(*qa),
_ => None,
Expand Down Expand Up @@ -360,7 +355,7 @@ pub type Rocks = AndexableArray<IRock, Option<Entity>, { IRock::SIZE }>;
pub struct Params {
pub width: u16,
pub height: u16,
pub exit: Qa,
pub exit: Pos,
pub frozen: Gridbool,
pub grid0: crate::grid_create!(Sqrid, Cell),
}
Expand Down Expand Up @@ -428,8 +423,8 @@ pub fn check_indy_path(params: &Params, node: &Node, indy: &Entity) -> bool {
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub enum Destiny {
InvalidAction,
Wall(Qa),
Rock(IRock, Qa),
Wall(Pos),
Rock(IRock, Pos),
Victory,
}

Expand Down
2 changes: 1 addition & 1 deletion src/entrypoint1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::io::BufRead;
use super::core::*;
use super::input::*;

pub fn eval(_params: &Params, node: &Node) -> Qa {
pub fn eval(_params: &Params, node: &Node) -> Pos {
let dir = node.grid[node.indy.qa]
.enter(&node.indy.qr)
.expect("invalid indy direction");
Expand Down
7 changes: 3 additions & 4 deletions src/entrypoint2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ pub fn solve_helper(
params: &Params,
node0: &Node,
focus0: Entity,
iturn: usize,
steps: &mut VecDeque<Action>,
) -> bool {
if check_indy_path(params, node0, &focus0) {
Expand All @@ -28,7 +27,7 @@ pub fn solve_helper(
node.apply(a);
}
if let Some(focus) = focus0.step(&node) {
if solve_helper(params, &node, focus, iturn + 1, steps) {
if solve_helper(params, &node, focus, steps) {
for a in &actions {
steps.push_front(*a);
}
Expand All @@ -44,7 +43,7 @@ pub fn rock_solve(
node: &Node,
mut steps: VecDeque<Action>,
irock: IRock,
qa_collision: Qa,
qa_collision: Pos,
) -> Option<Action> {
for focus in node.rock[irock].unwrap().iter(node) {
if focus.qa == qa_collision {
Expand All @@ -65,7 +64,7 @@ pub fn rock_solve(
pub fn solve(params: &Params, node: &Node) -> Option<Action> {
let mut stepsbase = VecDeque::new();
let focus = node.indy.step(node)?;
if !solve_helper(params, node, focus, 0, &mut stepsbase) {
if !solve_helper(params, node, focus, &mut stepsbase) {
return None;
}
stepsbase.push_front(Action::Wait);
Expand Down
14 changes: 6 additions & 8 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// This file is subject to the terms and conditions defined in
// file 'LICENSE', which is part of this source code package.

use std::convert::TryFrom;
use std::convert::TryInto;
use std::io;
use std::str::FromStr;

Expand Down Expand Up @@ -44,9 +42,9 @@ impl FromStr for Entity {
Ok(Entity {
qa: (v[0].parse::<u16>()?, v[1].parse::<u16>()?).try_into()?,
qr: match v[2] {
"TOP" => Qr::S,
"LEFT" => Qr::E,
"RIGHT" => Qr::W,
"TOP" => Dir::S,
"LEFT" => Dir::E,
"RIGHT" => Dir::W,
_ => {
return Err(Error::InvalidInput);
}
Expand Down Expand Up @@ -81,15 +79,15 @@ pub fn input_first(
for (x, cellnum) in line.split(' ').enumerate() {
let firstchar = cellnum.chars().next().unwrap();
if firstchar == '-' {
let qa = Qa::try_from((x as u16, y))?;
params.frozen.set_t(&qa);
let qa = Pos::try_from((x as u16, y))?;
params.frozen.set_t(qa);
}
gridline[x] = cellnum.parse()?;
}
}
node.grid = params.grid0;
let line = lineread(lineit)?;
params.exit = Qa::try_from((line.parse()?, params.height - 1))?;
params.exit = Pos::try_from((line.parse()?, params.height - 1))?;
Ok(())
}

Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
// file 'LICENSE', which is part of this source code package.

pub mod sqrid;
pub use self::sqrid::*;
pub use self::sqrid::{Dir, Grid, Gridbool, Pos, Sqrid};

pub mod andex;
pub use self::andex::*;

pub mod core;
pub mod entrypoint1;
Expand Down
59 changes: 29 additions & 30 deletions tests/episode1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// This file is subject to the terms and conditions defined in
// file 'LICENSE', which is part of this source code package.

use std::convert::TryFrom;
use std::iter;

use anyhow::Result;
Expand Down Expand Up @@ -32,35 +31,35 @@ fn test_input() -> Result<()> {
let mut node = Node::default();
input_first(&mut it_lines, &mut params, &mut node)?;
let inputs = vec![
("0 0 TOP", Qa::try_from((0, 1)).unwrap()),
("0 1 TOP", Qa::try_from((1, 1)).unwrap()),
("1 1 LEFT", Qa::try_from((1, 2)).unwrap()),
("1 2 TOP", Qa::try_from((2, 2)).unwrap()),
("2 2 LEFT", Qa::try_from((3, 2)).unwrap()),
("3 2 LEFT", Qa::try_from((4, 2)).unwrap()),
("4 2 LEFT", Qa::try_from((5, 2)).unwrap()),
("5 2 LEFT", Qa::try_from((6, 2)).unwrap()),
("6 2 LEFT", Qa::try_from((7, 2)).unwrap()),
("7 2 LEFT", Qa::try_from((8, 2)).unwrap()),
("8 2 LEFT", Qa::try_from((8, 3)).unwrap()),
("8 3 TOP", Qa::try_from((8, 4)).unwrap()),
("8 4 TOP", Qa::try_from((7, 4)).unwrap()),
("7 4 RIGHT", Qa::try_from((6, 4)).unwrap()),
("6 4 RIGHT", Qa::try_from((6, 5)).unwrap()),
("6 5 TOP", Qa::try_from((6, 6)).unwrap()),
("6 6 TOP", Qa::try_from((7, 6)).unwrap()),
("7 6 LEFT", Qa::try_from((8, 6)).unwrap()),
("8 6 LEFT", Qa::try_from((9, 6)).unwrap()),
("9 6 LEFT", Qa::try_from((10, 6)).unwrap()),
("9 7 RIGHT", Qa::try_from((8, 7)).unwrap()),
("8 7 RIGHT", Qa::try_from((7, 7)).unwrap()),
("7 7 RIGHT", Qa::try_from((7, 8)).unwrap()),
("7 8 TOP", Qa::try_from((6, 8)).unwrap()),
("6 8 RIGHT", Qa::try_from((5, 8)).unwrap()),
("5 8 RIGHT", Qa::try_from((4, 8)).unwrap()),
("4 8 RIGHT", Qa::try_from((3, 8)).unwrap()),
("3 8 RIGHT", Qa::try_from((2, 8)).unwrap()),
("2 8 RIGHT", Qa::try_from((2, 9)).unwrap()),
("0 0 TOP", Pos::try_from((0, 1)).unwrap()),
("0 1 TOP", Pos::try_from((1, 1)).unwrap()),
("1 1 LEFT", Pos::try_from((1, 2)).unwrap()),
("1 2 TOP", Pos::try_from((2, 2)).unwrap()),
("2 2 LEFT", Pos::try_from((3, 2)).unwrap()),
("3 2 LEFT", Pos::try_from((4, 2)).unwrap()),
("4 2 LEFT", Pos::try_from((5, 2)).unwrap()),
("5 2 LEFT", Pos::try_from((6, 2)).unwrap()),
("6 2 LEFT", Pos::try_from((7, 2)).unwrap()),
("7 2 LEFT", Pos::try_from((8, 2)).unwrap()),
("8 2 LEFT", Pos::try_from((8, 3)).unwrap()),
("8 3 TOP", Pos::try_from((8, 4)).unwrap()),
("8 4 TOP", Pos::try_from((7, 4)).unwrap()),
("7 4 RIGHT", Pos::try_from((6, 4)).unwrap()),
("6 4 RIGHT", Pos::try_from((6, 5)).unwrap()),
("6 5 TOP", Pos::try_from((6, 6)).unwrap()),
("6 6 TOP", Pos::try_from((7, 6)).unwrap()),
("7 6 LEFT", Pos::try_from((8, 6)).unwrap()),
("8 6 LEFT", Pos::try_from((9, 6)).unwrap()),
("9 6 LEFT", Pos::try_from((10, 6)).unwrap()),
("9 7 RIGHT", Pos::try_from((8, 7)).unwrap()),
("8 7 RIGHT", Pos::try_from((7, 7)).unwrap()),
("7 7 RIGHT", Pos::try_from((7, 8)).unwrap()),
("7 8 TOP", Pos::try_from((6, 8)).unwrap()),
("6 8 RIGHT", Pos::try_from((5, 8)).unwrap()),
("5 8 RIGHT", Pos::try_from((4, 8)).unwrap()),
("4 8 RIGHT", Pos::try_from((3, 8)).unwrap()),
("3 8 RIGHT", Pos::try_from((2, 8)).unwrap()),
("2 8 RIGHT", Pos::try_from((2, 9)).unwrap()),
];
for i in &inputs {
input_ep1(&mut iter::once(Ok(i.0.to_string())), &mut params, &mut node)?;
Expand Down

0 comments on commit 6068383

Please sign in to comment.