Skip to content

Commit 5464f7b

Browse files
committed
2024-04 Rust & Scala
1 parent ce3be00 commit 5464f7b

File tree

14 files changed

+715
-2
lines changed

14 files changed

+715
-2
lines changed

ReadMe.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
| Year-Day | Task | Scala | Rust | Others |
88
|----------|:-------------------------------------------------------------------------------|:-----------------------------------------------------------------------:|:----------------------------------------------:|:----------------------------------------------------------------------:|
9+
| 2024-04 | [Ceres Search](https://adventofcode.com/2024/day/4) | [Scala](scala2/src/main/scala/jurisk/adventofcode/y2024/Advent04.scala) | [Rust](rust/y2024/src/bin/solution_2024_04.rs) | |
910
| 2024-03 | [Mull It Over](https://adventofcode.com/2024/day/3) | [Scala](scala2/src/main/scala/jurisk/adventofcode/y2024/Advent03.scala) | [Rust](rust/y2024/src/bin/solution_2024_03.rs) | |
1011
| 2024-02 | [Red-Nosed Reports](https://adventofcode.com/2024/day/2) | [Scala](scala2/src/main/scala/jurisk/adventofcode/y2024/Advent02.scala) | [Rust](rust/y2024/src/bin/solution_2024_02.rs) | |
1112
| 2024-01 | [Historian Hysteria](https://adventofcode.com/2024/day/1) | [Scala](scala2/src/main/scala/jurisk/adventofcode/y2024/Advent01.scala) | [Rust](rust/y2024/src/bin/solution_2024_01.rs) | |

rust/Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use std::ops::{Add, Neg};
2+
3+
use num_traits::Num;
4+
5+
use crate::coords2d::Coords2D;
6+
7+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Ord, PartialOrd)]
8+
pub enum DirectionWithDiagonals {
9+
N,
10+
NE,
11+
E,
12+
SE,
13+
S,
14+
SW,
15+
W,
16+
NW,
17+
}
18+
19+
impl DirectionWithDiagonals {
20+
#[must_use]
21+
pub const fn all() -> [Self; 8] {
22+
[
23+
Self::N,
24+
Self::NE,
25+
Self::E,
26+
Self::SE,
27+
Self::S,
28+
Self::SW,
29+
Self::W,
30+
Self::NW,
31+
]
32+
}
33+
}
34+
35+
impl<N> From<DirectionWithDiagonals> for Coords2D<N>
36+
where
37+
N: Num + Neg<Output = N> + Copy,
38+
{
39+
fn from(value: DirectionWithDiagonals) -> Self {
40+
match value {
41+
DirectionWithDiagonals::N => Coords2D::new(N::zero(), N::one()),
42+
DirectionWithDiagonals::NE => Coords2D::new(N::one(), N::one()),
43+
DirectionWithDiagonals::E => Coords2D::new(N::one(), N::zero()),
44+
DirectionWithDiagonals::SE => Coords2D::new(N::one(), -N::one()),
45+
DirectionWithDiagonals::S => Coords2D::new(N::zero(), -N::one()),
46+
DirectionWithDiagonals::SW => Coords2D::new(-N::one(), -N::one()),
47+
DirectionWithDiagonals::W => Coords2D::new(-N::one(), N::zero()),
48+
DirectionWithDiagonals::NW => Coords2D::new(-N::one(), N::one()),
49+
}
50+
}
51+
}
52+
53+
impl<N> Add<DirectionWithDiagonals> for Coords2D<N>
54+
where
55+
N: Num + Neg<Output = N> + Copy,
56+
{
57+
type Output = Coords2D<N>;
58+
59+
fn add(self, rhs: DirectionWithDiagonals) -> Self::Output {
60+
self + Coords2D::from(rhs)
61+
}
62+
}

rust/common/src/grid2d.rs

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#![expect(clippy::cast_possible_wrap, clippy::cast_possible_truncation, clippy::cast_sign_loss)]
2+
3+
use std::iter::Sum;
4+
use std::str::FromStr;
5+
6+
use pathfinding::matrix::Matrix;
7+
8+
use crate::coords2d::Coords2D;
9+
use crate::parsing::parse_matrix;
10+
11+
pub type Coords = Coords2D<i32>;
12+
13+
pub trait Grid2D<T> {
14+
fn map_by_coords<F, U>(&self, f: F) -> MatrixGrid2D<U>
15+
where
16+
F: Fn(Coords) -> U;
17+
18+
fn count<F>(&self, f: F) -> usize
19+
where
20+
F: Fn(Coords, &T) -> bool;
21+
22+
fn get(&self, coords: Coords) -> Option<&T>;
23+
24+
fn get_or_else(&self, coords: Coords, default: T) -> T
25+
where
26+
T: Clone,
27+
{
28+
self.get(coords).cloned().unwrap_or(default)
29+
}
30+
31+
fn sum(&self) -> T
32+
where
33+
T: Sum + Clone;
34+
}
35+
36+
pub struct MatrixGrid2D<T> {
37+
data: Matrix<T>,
38+
}
39+
40+
impl From<(usize, usize)> for Coords {
41+
fn from(value: (usize, usize)) -> Self {
42+
let (x, y) = value;
43+
Coords::new(x as i32, y as i32)
44+
}
45+
}
46+
47+
impl From<Coords> for (usize, usize) {
48+
fn from(coords: Coords) -> Self {
49+
(coords.x as usize, coords.y as usize)
50+
}
51+
}
52+
53+
impl<T> Grid2D<T> for MatrixGrid2D<T> {
54+
fn map_by_coords<F, U>(&self, f: F) -> MatrixGrid2D<U>
55+
where
56+
F: Fn(Coords) -> U,
57+
{
58+
let new_data = self
59+
.data
60+
.keys()
61+
.map(|coords| f(Coords::from(coords)))
62+
.collect();
63+
MatrixGrid2D {
64+
data: Matrix::from_vec(self.data.rows, self.data.columns, new_data).unwrap(),
65+
}
66+
}
67+
68+
fn count<F>(&self, f: F) -> usize
69+
where
70+
F: Fn(Coords, &T) -> bool,
71+
{
72+
self.data
73+
.keys()
74+
.filter(|coords| f(Coords::from(*coords), &self.data[*coords]))
75+
.count()
76+
}
77+
78+
fn get(&self, coords: Coords) -> Option<&T> {
79+
self.data.get(coords.into())
80+
}
81+
82+
fn sum(&self) -> T
83+
where
84+
T: Sum + Clone,
85+
{
86+
self.data.values().cloned().sum()
87+
}
88+
}
89+
90+
impl FromStr for MatrixGrid2D<char> {
91+
type Err = String;
92+
93+
fn from_str(s: &str) -> Result<Self, Self::Err> {
94+
parse_matrix(s, Ok).map(|data| MatrixGrid2D { data })
95+
}
96+
}

rust/common/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ pub mod cryptography;
99
pub mod cuboid;
1010
pub mod direction;
1111
pub mod direction_hex;
12+
pub mod direction_with_diagonals;
13+
pub mod grid2d;
1214
pub mod math;
1315
pub mod matrices;
1416
pub mod pairing;

rust/y2024/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ workspace = true
1111

1212
[dependencies]
1313
advent-of-code-common = { path = "../common" }
14+
pathfinding.workspace = true
1415
itertools.workspace = true
1516
regex.workspace = true

rust/y2024/resources/04-test-00.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
MMMSXXMASM
2+
MSAMXMSMSA
3+
AMXSXMAAMM
4+
MSAMASMSMX
5+
XMASAMXAMM
6+
XXAMMXXAMA
7+
SMSMSASXSS
8+
SAXAMASAAA
9+
MAMMMXMMMM
10+
MXMXAXMASX

0 commit comments

Comments
 (0)