Skip to content

Commit 8a9fa5f

Browse files
committed
Use grid
1 parent c682cb0 commit 8a9fa5f

2 files changed

Lines changed: 29 additions & 53 deletions

File tree

src/grid.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -105,43 +105,47 @@ impl Compass {
105105
}
106106

107107
impl Pos {
108-
pub fn step(&self, dir: Compass) -> Self {
108+
pub fn walk(&self, dir: Compass, steps: i32) -> Self {
109109
match dir {
110110
Compass::North => Self {
111111
x: self.x,
112-
y: self.y - 1,
112+
y: self.y - steps,
113113
},
114114
Compass::NorthEast => Self {
115-
x: self.x + 1,
116-
y: self.y - 1,
115+
x: self.x + steps,
116+
y: self.y - steps,
117117
},
118118
Compass::East => Self {
119-
x: self.x + 1,
119+
x: self.x + steps,
120120
y: self.y,
121121
},
122122
Compass::SouthEast => Self {
123-
x: self.x + 1,
124-
y: self.y + 1,
123+
x: self.x + steps,
124+
y: self.y + steps,
125125
},
126126
Compass::South => Self {
127127
x: self.x,
128-
y: self.y + 1,
128+
y: self.y + steps,
129129
},
130130
Compass::SouthWest => Self {
131-
x: self.x - 1,
132-
y: self.y + 1,
131+
x: self.x - steps,
132+
y: self.y + steps,
133133
},
134134
Compass::West => Self {
135-
x: self.x - 1,
135+
x: self.x - steps,
136136
y: self.y,
137137
},
138138
Compass::NorthWest => Self {
139-
x: self.x - 1,
140-
y: self.y - 1,
139+
x: self.x - steps,
140+
y: self.y - steps,
141141
},
142142
}
143143
}
144144

145+
pub fn step(&self, dir: Compass) -> Self {
146+
self.walk(dir, 1)
147+
}
148+
145149
// direction as (dx, dy), normalised by dividing by gcd
146150
pub fn direction_dxdy(&self, other: &Self) -> (i32, i32) {
147151
if self == other {

src/y2016/day1.rs

Lines changed: 12 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::grid::{Compass, ORIGIN, Pos};
12
use std::collections::HashSet;
23

34
pub struct Solver {
@@ -31,7 +32,7 @@ fn part1(input: &[Instruction]) -> u32 {
3132

3233
fn part2(input: &[Instruction]) -> u32 {
3334
let mut state = State::new();
34-
let mut visited: HashSet<(i32, i32)> = HashSet::new();
35+
let mut visited: HashSet<Pos> = HashSet::new();
3536
for instruction in input {
3637
state.turn(instruction.turn);
3738
for _ in 0..instruction.walk {
@@ -45,61 +46,32 @@ fn part2(input: &[Instruction]) -> u32 {
4546
panic!("Didn't visit any location twice")
4647
}
4748

48-
#[derive(Clone, Copy)]
49-
enum Direction {
50-
North,
51-
East,
52-
South,
53-
West,
54-
}
55-
56-
impl Direction {
57-
fn turn(&self, turn: Turn) -> Direction {
58-
match turn {
59-
Turn::Left => match self {
60-
Direction::North => Direction::West,
61-
Direction::East => Direction::North,
62-
Direction::South => Direction::East,
63-
Direction::West => Direction::South,
64-
},
65-
Turn::Right => match self {
66-
Direction::North => Direction::East,
67-
Direction::East => Direction::South,
68-
Direction::South => Direction::West,
69-
Direction::West => Direction::North,
70-
},
71-
}
72-
}
73-
}
74-
7549
struct State {
76-
direction: Direction,
77-
position: (i32, i32),
50+
direction: Compass,
51+
position: Pos,
7852
}
7953

8054
impl State {
8155
fn new() -> Self {
8256
Self {
83-
direction: Direction::North,
84-
position: (0, 0),
57+
direction: Compass::North,
58+
position: ORIGIN,
8559
}
8660
}
8761

8862
fn turn(&mut self, turn: Turn) {
89-
self.direction = self.direction.turn(turn);
63+
self.direction = match turn {
64+
Turn::Left => self.direction.left90(),
65+
Turn::Right => self.direction.right90(),
66+
}
9067
}
9168

9269
fn walk(&mut self, walk: u32) {
93-
match self.direction {
94-
Direction::North => self.position.1 += walk as i32,
95-
Direction::East => self.position.0 += walk as i32,
96-
Direction::South => self.position.1 -= walk as i32,
97-
Direction::West => self.position.0 -= walk as i32,
98-
};
70+
self.position = self.position.walk(self.direction, walk as i32);
9971
}
10072

10173
fn distance_from_origin(&self) -> u32 {
102-
self.position.0.unsigned_abs() + self.position.1.unsigned_abs()
74+
self.position.manhattan_distance(&ORIGIN)
10375
}
10476
}
10577

0 commit comments

Comments
 (0)