Skip to content

Commit bf2c790

Browse files
committed
Refactor to use common pos module
1 parent 361887e commit bf2c790

5 files changed

Lines changed: 126 additions & 172 deletions

File tree

src/pos.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,17 +162,30 @@ impl fmt::Debug for Pos {
162162

163163
impl fmt::Display for Pos {
164164
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
165-
write!(f, "({},{})", self.x, self.y)
165+
write!(f, "{},{}", self.x, self.y)
166166
}
167167
}
168168

169169
// 3d position with x, y and z coordinates
170+
#[derive(Clone, Eq, Hash, PartialEq)]
170171
pub struct Pos3d {
171172
pub x: i32,
172173
pub y: i32,
173174
pub z: i32,
174175
}
175176

177+
pub const ORIGIN3D: Pos3d = Pos3d { x: 0, y: 0, z: 0 };
178+
179+
impl From<(i32, i32, i32)> for Pos3d {
180+
fn from(xyz: (i32, i32, i32)) -> Self {
181+
Pos3d {
182+
x: xyz.0,
183+
y: xyz.1,
184+
z: xyz.2,
185+
}
186+
}
187+
}
188+
176189
impl FromStr for Pos3d {
177190
type Err = ParseError;
178191

@@ -196,6 +209,28 @@ impl FromStr for Pos3d {
196209
}
197210
}
198211

212+
impl Add for &Pos3d {
213+
type Output = Pos3d;
214+
fn add(self, other: &Pos3d) -> Pos3d {
215+
Pos3d {
216+
x: self.x + other.x,
217+
y: self.y + other.y,
218+
z: self.z + other.z,
219+
}
220+
}
221+
}
222+
223+
impl Sub for &Pos3d {
224+
type Output = Pos3d;
225+
fn sub(self, other: &Pos3d) -> Pos3d {
226+
Pos3d {
227+
x: self.x - other.x,
228+
y: self.y - other.y,
229+
z: self.z - other.z,
230+
}
231+
}
232+
}
233+
199234
impl Pos3d {
200235
#[allow(dead_code)]
201236
// manhattan distance between points

src/y2018/day13.rs

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
use crate::pos::Pos;
12
use std::fmt;
23
use std::fmt::Write;
34

45
pub fn part1(mine: &Mine) -> String {
5-
format!("{:?}", mine.clone().first_crash())
6+
mine.clone().first_crash().to_string()
67
}
78

89
pub fn part2(mine: &Mine) -> String {
9-
format!("{:?}", mine.clone().last_cart())
10+
mine.clone().last_cart().to_string()
1011
}
1112

1213
#[derive(Clone)]
@@ -69,22 +70,9 @@ enum Turn {
6970
Right,
7071
}
7172

72-
#[derive(PartialEq)]
73-
struct Position {
74-
x: usize,
75-
y: usize,
76-
}
77-
78-
impl fmt::Debug for Position {
79-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
80-
write!(f, "{},{}", self.x, self.y)
81-
}
82-
}
83-
8473
#[derive(Debug, Clone)]
8574
struct Cart {
86-
x: usize,
87-
y: usize,
75+
pos: Pos,
8876
facing: Direction,
8977
next_turn: Turn,
9078
destroyed: bool,
@@ -112,13 +100,13 @@ impl Cart {
112100

113101
fn move_turn(&mut self, map: &[Vec<Cell>]) {
114102
match self.facing {
115-
Direction::North => self.y -= 1,
116-
Direction::East => self.x += 1,
117-
Direction::South => self.y += 1,
118-
Direction::West => self.x -= 1,
103+
Direction::North => self.pos.y -= 1,
104+
Direction::East => self.pos.x += 1,
105+
Direction::South => self.pos.y += 1,
106+
Direction::West => self.pos.x -= 1,
119107
}
120108

121-
match map[self.y][self.x] {
109+
match map[self.pos.y as usize][self.pos.x as usize] {
122110
Cell::Corner1 => match self.facing {
123111
Direction::North => self.facing = Direction::East,
124112
Direction::East => self.facing = Direction::North,
@@ -148,7 +136,10 @@ impl fmt::Debug for Mine {
148136
let mut i = 0;
149137
for (y, row) in self.map.iter().enumerate() {
150138
for (x, cell) in row.iter().enumerate() {
151-
if i < self.carts.len() && self.carts[i].y == y && self.carts[i].x == x {
139+
if i < self.carts.len()
140+
&& self.carts[i].pos.y == y as i32
141+
&& self.carts[i].pos.x == x as i32
142+
{
152143
f.write_char(self.carts[i].facing.to_char()).unwrap();
153144
i += 1;
154145
} else {
@@ -162,39 +153,38 @@ impl fmt::Debug for Mine {
162153
}
163154

164155
impl Mine {
165-
fn first_crash(&mut self) -> Position {
156+
fn first_crash(&mut self) -> Pos {
166157
loop {
167158
for cart_id in 0..self.carts.len() {
168159
self.carts[cart_id].move_turn(&self.map);
169160

170161
// has it crashed?
171162
if self.detect_crash(cart_id) {
172-
return Position {
173-
x: self.carts[cart_id].x,
174-
y: self.carts[cart_id].y,
163+
return Pos {
164+
x: self.carts[cart_id].pos.x,
165+
y: self.carts[cart_id].pos.y,
175166
};
176167
}
177168
}
178-
self.carts.sort_by_key(|c| (c.y, c.x));
169+
self.carts.sort_by_key(|c| (c.pos.y, c.pos.x));
179170
}
180171
}
181172

182173
fn detect_crash(&mut self, this_id: usize) -> bool {
183-
let this_x = self.carts[this_id].x;
184-
let this_y = self.carts[this_id].y;
174+
let this_pos = self.carts[this_id].pos;
185175
for (other_id, other) in self.carts.iter_mut().enumerate() {
186176
if other_id == this_id {
187177
continue;
188178
}
189-
if this_x == other.x && this_y == other.y {
179+
if this_pos == other.pos {
190180
other.destroyed = true;
191181
return true;
192182
}
193183
}
194184
false
195185
}
196186

197-
fn last_cart(&mut self) -> Position {
187+
fn last_cart(&mut self) -> Pos {
198188
loop {
199189
for cart_id in 0..self.carts.len() {
200190
self.carts[cart_id].move_turn(&self.map);
@@ -205,12 +195,9 @@ impl Mine {
205195
}
206196
self.carts.retain(|cart| !cart.destroyed);
207197
if self.carts.len() == 1 {
208-
return Position {
209-
x: self.carts[0].x,
210-
y: self.carts[0].y,
211-
};
198+
return self.carts[0].pos;
212199
}
213-
self.carts.sort_by_key(|c| (c.y, c.x));
200+
self.carts.sort_by_key(|c| (c.pos.y, c.pos.x));
214201
}
215202
}
216203
}
@@ -236,8 +223,10 @@ pub fn parse_input(input: &str) -> Mine {
236223
mine.map[y][x] = Cell::EW;
237224
}
238225
mine.carts.push(Cart {
239-
x,
240-
y,
226+
pos: Pos {
227+
x: x as i32,
228+
y: y as i32,
229+
},
241230
facing: Direction::from_char(c).unwrap(),
242231
next_turn: Turn::Left,
243232
destroyed: false,
@@ -275,12 +264,12 @@ mod tests {
275264
#[test]
276265
fn test_first_crash() {
277266
let mut mine = parse_input(test_input());
278-
assert_eq!(Position { x: 7, y: 3 }, mine.first_crash());
267+
assert_eq!(Pos { x: 7, y: 3 }, mine.first_crash());
279268
}
280269

281270
#[test]
282271
fn test_last_cart() {
283272
let mut mine = parse_input(test_input2());
284-
assert_eq!(Position { x: 6, y: 4 }, mine.last_cart());
273+
assert_eq!(Pos { x: 6, y: 4 }, mine.last_cart());
285274
}
286275
}

src/y2018/day6.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,7 @@ fn box_size(coords: &[Pos]) -> (Pos, Pos) {
9999
}
100100

101101
pub fn parse_input(input: &str) -> Vec<Pos> {
102-
let mut list = vec![];
103-
for line in input.lines() {
104-
let coords: Vec<_> = line.split(", ").filter_map(|s| s.parse().ok()).collect();
105-
list.push(Pos {
106-
x: coords[0],
107-
y: coords[1],
108-
});
109-
}
110-
list
102+
input.lines().map(|s| s.parse().unwrap()).collect()
111103
}
112104

113105
#[test]

0 commit comments

Comments
 (0)