Skip to content

Commit

Permalink
chore: add helpers, clean up, prepare
Browse files Browse the repository at this point in the history
  • Loading branch information
clechasseur committed Nov 23, 2024
1 parent 5ae3da9 commit e1f6a8c
Show file tree
Hide file tree
Showing 16 changed files with 799 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:
strategy:
fail-fast: false
matrix:
toolchain: [ 1.56.1, stable ]
toolchain: [ 1.70.0, stable ]
os: [ ubuntu ]
ignore-lock: [ false ]
all-features: [ true ]
Expand Down
213 changes: 213 additions & 0 deletions Cargo.lock

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

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
name = "adventofcode2024-clp"
version = "0.1.0"
edition = "2021"
rust-version = "1.56.1"
rust-version = "1.70.0"

[features]
slow = []
utils = []

[dependencies]
itertools = "0.13.0"
num = "0.4.3"
regex = "1.11.1"
strum = { version = "0.26.3", features = ["derive"] }
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ My solutions to the Advent of Code 2024 puzzles in Rust 🦀

## Requirements

* [Rust](https://www.rust-lang.org/) 1.56.1 or later
* [Rust](https://www.rust-lang.org/) 1.70.0 or later

## Running the tests

Expand Down
3 changes: 3 additions & 0 deletions src/day_01.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn part_1() -> usize {
0
}
6 changes: 5 additions & 1 deletion src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@

pub mod direction;
pub mod looping;
pub mod pt;
pub mod pt_3d;
pub mod regex;
55 changes: 55 additions & 0 deletions src/helpers/direction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use std::ops::Neg;

use num::{one, zero, One, Zero};
use strum::{Display, EnumCount, FromRepr};

use crate::helpers::pt::Pt;

/// ↓ ↑ ← →
#[repr(u8)]
#[derive(
Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, FromRepr, EnumCount, Display,
)]
pub enum Direction {
Right,
Down,
Left,
Up,
}

impl Direction {
/// Turns 90 degrees to the left.
pub fn turn_left(&self) -> Self {
Self::from_repr(((*self as u8) + 3) % (Self::COUNT as u8)).unwrap()
}

/// Turns 90 degrees to the right.
pub fn turn_right(&self) -> Self {
Self::from_repr(((*self as u8) + 1) % (Self::COUNT as u8)).unwrap()
}

/// Turns around (e.g. performs a 180 degrees turn).
pub fn turn_around(&self) -> Self {
Self::from_repr(((*self as u8) + 2) % (Self::COUNT as u8)).unwrap()
}

/// Returns the displacement to apply to move one step in this direction.
/// The displacement is returned as a [`Pt`].
///
/// # Notes
///
/// Because this enum is meant to be used to move around a map represented as a series of rows
/// like on a computer screen, `Up`'s displacement will _subtract_ one from the Y axis, while
/// `Down`'s will _add_ one to the Y axis.
pub fn displacement<T>(&self) -> Pt<T>
where
T: Zero + One + Neg<Output = T>,
{
match self {
Direction::Right => Pt::new(one(), zero()),
Direction::Down => Pt::new(zero(), one()),
Direction::Left => Pt::new(-one::<T>(), zero()),
Direction::Up => Pt::new(zero(), -one::<T>()),
}
}
}
Loading

0 comments on commit e1f6a8c

Please sign in to comment.