Skip to content

Commit 392ed16

Browse files
committed
[LIB] Cleanup temp lib structs
1 parent 7fac085 commit 392ed16

File tree

4 files changed

+20
-61
lines changed

4 files changed

+20
-61
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

aoc_2021/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ edition = "2021"
66
[dependencies]
77
common = { path = "../common" }
88
hashbrown = "0.13.1"
9+
nd_vec = "0.4.0"

aoc_2021/src/day_13.rs

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
11
use hashbrown::HashSet;
22

33
use common::{Answer, Solution};
4+
use nd_vec::vector;
45

5-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
6-
struct Point {
7-
x: usize,
8-
y: usize,
9-
}
10-
11-
impl Point {
12-
fn new(x: usize, y: usize) -> Self {
13-
Self { x, y }
14-
}
15-
}
6+
type Point = nd_vec::Vec2<usize>;
167

178
pub struct Day13;
189

@@ -69,23 +60,23 @@ impl Paper {
6960

7061
match ins.direction {
7162
Direction::Horizontal => {
72-
for i in self.data.clone().iter().filter(|x| x.x > ins.position) {
63+
for i in self.data.clone().iter().filter(|x| x.x() > ins.position) {
7364
self.data.remove(i);
74-
self.data.insert(Point::new(ins.position * 2 - i.x, i.y));
65+
self.data.insert(vector!(ins.position * 2 - i.x(), i.y()));
7566
}
7667
}
7768
Direction::Vertical => {
78-
for i in self.data.clone().iter().filter(|x| x.y > ins.position) {
69+
for i in self.data.clone().iter().filter(|x| x.y() > ins.position) {
7970
self.data.remove(i);
80-
self.data.insert(Point::new(i.x, ins.position * 2 - i.y));
71+
self.data.insert(vector!(i.x(), ins.position * 2 - i.y()));
8172
}
8273
}
8374
}
8475
}
8576

8677
fn bounds(&self) -> (usize, usize) {
87-
let x = self.data.iter().map(|x| x.x).max().unwrap();
88-
let y = self.data.iter().map(|x| x.y).max().unwrap();
78+
let x = self.data.iter().map(|x| x.x()).max().unwrap();
79+
let y = self.data.iter().map(|x| x.y()).max().unwrap();
8980
(x, y)
9081
}
9182

@@ -95,7 +86,7 @@ impl Paper {
9586

9687
for y in 0..=my {
9788
for x in 0..=mx {
98-
let point = Point::new(x, y);
89+
let point = vector!(x, y);
9990
if self.data.contains(&point) {
10091
out.push('#');
10192
} else {
@@ -113,7 +104,7 @@ fn parse_point(raw: &str) -> Point {
113104
let parts = raw.split_once(',').unwrap();
114105
let x = parts.0.parse().unwrap();
115106
let y = parts.1.parse().unwrap();
116-
Point::new(x, y)
107+
vector!(x, y)
117108
}
118109

119110
fn parse_fold(raw: &str) -> Fold {

aoc_2022/src/day_14.rs

Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use nd_vec::vector;
44
type Point = nd_vec::Vec2<usize>;
55

66
const NEW_POINT: Point = vector!(500, 0);
7+
78
pub struct Day14;
89

910
impl Solution for Day14 {
@@ -47,7 +48,7 @@ impl Solution for Day14 {
4748

4849
#[derive(Debug)]
4950
struct World {
50-
data: Matrix<Element>,
51+
data: Vec<Vec<Element>>,
5152
working: Point,
5253

5354
x_adjust: usize,
@@ -63,12 +64,6 @@ enum Element {
6364
Air,
6465
}
6566

66-
#[derive(Debug, Default, Clone, PartialEq, Eq)]
67-
pub struct Matrix<T> {
68-
data: Vec<T>,
69-
width: usize,
70-
}
71-
7267
#[derive(Debug)]
7368
struct Line(Point, Point);
7469

@@ -89,11 +84,12 @@ impl World {
8984
}
9085

9186
let x_adjust = min_x - max_y;
92-
let mut out = Matrix::new_filled(max_x - min_x + max_x + 1, max_y + 3, Element::Air);
87+
let mut out = vec![vec![Element::Air; max_x - min_x + max_x + 1]; max_y + 3];
88+
9389
lines
9490
.iter()
9591
.flat_map(|x| x.points())
96-
.for_each(|x| out.set(x.x() - x_adjust, x.y(), Element::Wall));
92+
.for_each(|x| out[x.y()][x.x() - x_adjust] = Element::Wall);
9793

9894
Self {
9995
data: out,
@@ -125,48 +121,18 @@ impl World {
125121

126122
fn count_sand(&self) -> usize {
127123
self.data
128-
.raw()
129124
.iter()
125+
.flat_map(|x| x.iter())
130126
.filter(|x| **x == Element::Sand)
131127
.count()
132128
}
133129

134130
fn get(&self, x: usize, y: usize) -> &Element {
135-
self.data.get(x - self.x_adjust, y)
131+
&self.data[y][x - self.x_adjust]
136132
}
137133

138134
fn get_mut(&mut self, x: usize, y: usize) -> &mut Element {
139-
self.data.get_mut(x - self.x_adjust, y)
140-
}
141-
}
142-
143-
impl<T> Matrix<T> {
144-
fn new_filled(width: usize, height: usize, val: T) -> Self
145-
where
146-
T: Clone,
147-
{
148-
Self {
149-
data: vec![val; width * height],
150-
width,
151-
}
152-
}
153-
154-
fn raw(&self) -> &Vec<T> {
155-
&self.data
156-
}
157-
158-
fn set(&mut self, x: usize, y: usize, data: T) {
159-
self.data[y * self.width + x] = data;
160-
}
161-
162-
fn get(&self, x: usize, y: usize) -> &T {
163-
debug_assert!(x < self.width, "x out of bounds");
164-
&self.data[y * self.width + x]
165-
}
166-
167-
fn get_mut(&mut self, x: usize, y: usize) -> &mut T {
168-
debug_assert!(x < self.width, "x out of bounds");
169-
&mut self.data[y * self.width + x]
135+
&mut self.data[y][x - self.x_adjust]
170136
}
171137
}
172138

0 commit comments

Comments
 (0)