1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
//@ A `Direction` is a simple compass direction. There are only
//@ eight of them because we only handle the eight directions
//@ that are immediately expressible via a grid:
//@ ```
//@ \|/
//@ - -
//@ /|\
//@ ```
//@
//@ /// A `Direction` is a simple compass direction on the grid.
#[derive(PartialEq, Eq, PartialOrd, Ord, Copy, Clone, Debug)]
pub enum Direction { N, NE, E, SE, S, SW, W, NW }

pub const DIRECTIONS: &'static [Direction] =
    &[Direction::N, Direction::NE, Direction::E, Direction::SE,
      Direction::S, Direction::SW, Direction::W, Direction::NW];


/// A turn chooses between clockwise (`CW`) and counter-clockwise (`CCW`).
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub enum Turn {
    /// Clockwise
    CW,
    /// Counter-clockwise
    CCW,
}

impl Turn {
    pub fn reverse(&self) -> Self {
        use self::Turn::*;
        match *self {
            CW => CCW,
            CCW => CW,
        }
    }
}

impl Direction {
    pub fn ver_north(&self) -> i32 {
        use directions::Direction::*;
        match *self {
            N | NE | NW => 1,
            S | SE | SW => -1,
            E | W => 0,
        }
    }

    pub fn ver_south(&self) -> i32 { -self.ver_north() }

    pub fn hor_east(&self) -> i32 {
        use directions::Direction::*;
        match *self {
            E | NE | SE => 1,
            W | NW | SW => -1,
            N | S => 0,
        }
    }

    pub fn hor_west(&self) -> i32 { -self.hor_east() }

    pub fn reverse(&self) -> Self {
        use self::Direction::*;
        match *self {
            N => S, NE => SW, E => W, SE => NW,
            S => N, SW => NE, W => E, NW => SE,
        }
    }

    pub fn veer(&self, t: Turn) -> Self {
        use self::Direction::*;
        use self::Turn::*;
        match (*self, t) {
            (N, CW) => NE, (NE, CW) => E, (E, CW) => SE, (SE, CW) => S,
            (S, CW) => SW, (SW, CW) => W, (W, CW) => NW, (NW, CW) => N,
            (N, CCW) => NW, (NW, CCW) => W, (W, CCW) => SW, (SW, CCW) => S,
            (S, CCW) => SE, (SE, CCW) => E, (E, CCW) => NE, (NE, CCW) => N,
        }
    }

    pub fn sharp_turn(&self, t: Turn) -> Self {
        // a sharp turn ends up being the same as reversing and then
        // veering to the reverse direction (because reversing is like
        // taking the sharpest turn one notch too far).
        self.reverse().veer(t.reverse())
    }
}

mod dir_tests {
    #[test]
    fn dir_basics() {
        use directions::Direction::*;
        assert_eq!(NE.ver_north(), 1);
        assert_eq!(NW.ver_south(), -1);
        assert_eq!(S.hor_east(), 0);
        assert_eq!(NE.hor_east(), 1);
        assert_eq!(SW.hor_west(), 1);
    }
}

pub trait ToDirections { fn to_directions(&self) -> Vec<Direction>; }

pub struct N;
pub struct NE;
pub struct E;
pub struct SE;
pub struct S;
pub struct SW;
pub struct W;
pub struct NW;

pub struct N_;
pub struct _E;
pub struct S_;
pub struct _W;

pub struct Horizontal;
pub struct Vertical;
pub struct NonHorizontal;
pub struct NonVertical;
pub struct NonNorth;
pub struct NonSouth;
pub struct NonEast;
pub struct NonWest;
pub struct Any;

impl ToDirections for Direction {
    fn to_directions(&self) -> Vec<Direction> { vec![*self] }
}

impl ToDirections for Horizontal {
    fn to_directions(&self) -> Vec<Direction> { vec![Direction::W, Direction::E] }
}

impl ToDirections for Vertical {
    fn to_directions(&self) -> Vec<Direction> { vec![Direction::N, Direction::S] }
}

impl ToDirections for NonHorizontal {
    fn to_directions(&self) -> Vec<Direction> {
        use directions::Direction::*;
        vec![N, NE, NW, S, SE, SW]
    }
}

impl ToDirections for NonVertical {
    fn to_directions(&self) -> Vec<Direction> {
        use directions::Direction::*;
        vec![E, NE, SE, W, NW, SW]
    }
}

impl ToDirections for NonNorth {
    fn to_directions(&self) -> Vec<Direction> {
        use directions::Direction::*;
        vec![E, SE, S, SW, W]
    }
}

impl ToDirections for NonSouth {
    fn to_directions(&self) -> Vec<Direction> {
        use directions::Direction::*;
        vec![E, NE, N, NW, W]
    }
}

impl ToDirections for NonEast {
    fn to_directions(&self) -> Vec<Direction> {
        use directions::Direction::*;
        vec![N, NW, W, SW, S]
    }
}

impl ToDirections for NonWest {
    fn to_directions(&self) -> Vec<Direction> {
        use directions::Direction::*;
        vec![N, NE, E, SE, S]
    }
}

fn union<D1, D2>(d1: &D1, d2: &D2) -> Vec<Direction> where D1: ToDirections, D2: ToDirections {
    let mut v = d1.to_directions();
    v.extend(d2.to_directions());
    v.sort();
    v.dedup();
    v
}

impl ToDirections for Any {
    fn to_directions(&self) -> Vec<Direction> { DIRECTIONS.iter().cloned().collect() }
}

impl ToDirections for Vec<Direction> {
    fn to_directions(&self) -> Vec<Direction> { self.clone() }
}

impl<D1,D2> ToDirections for (D1, D2)
    where D1: ToDirections, D2: ToDirections
{
    fn to_directions(&self) -> Vec<Direction> {
        union(&self.0, &self.1)
    }
}
impl<D1,D2,D3> ToDirections for (D1, D2, D3)
    where D1: ToDirections, D2: ToDirections, D3: ToDirections
{
    fn to_directions(&self) -> Vec<Direction> {
        union(&union(&self.0, &self.1), &self.2)
    }
}
impl<D1,D2,D3,D4> ToDirections for (D1, D2, D3, D4)
    where D1: ToDirections, D2: ToDirections, D3: ToDirections, D4: ToDirections
{
    fn to_directions(&self) -> Vec<Direction> {
        union(&union(&self.0, &self.1), &union(&self.2, &self.3))
    }
}
impl<D1,D2,D3,D4,D5> ToDirections for (D1, D2, D3, D4, D5)
    where D1: ToDirections, D2: ToDirections, D3: ToDirections, D4: ToDirections,
    D5: ToDirections
{
    fn to_directions(&self) -> Vec<Direction> {
        union(&union(&union(&self.0, &self.1), &union(&self.2, &self.3)),
              &self.4)
    }
}

macro_rules! to_dirs {
    ($I:ident) => {
        impl ToDirections for $I {
            fn to_directions(&self) -> Vec<Direction> {
                vec![Direction::$I]
            }
        }
    }
}

to_dirs!(N); to_dirs!(NE); to_dirs!(E); to_dirs!(SE);
to_dirs!(S); to_dirs!(SW); to_dirs!(W); to_dirs!(NW);

impl ToDirections for N_ {
    fn to_directions(&self) -> Vec<Direction> {
        vec![Direction::N, Direction::NE, Direction::NW]
    }
}

impl ToDirections for S_ {
    fn to_directions(&self) -> Vec<Direction> {
        vec![Direction::S, Direction::SE, Direction::SW]
    }
}

impl ToDirections for _E {
    fn to_directions(&self) -> Vec<Direction> {
        vec![Direction::E, Direction::NE, Direction::SE]
    }
}

impl ToDirections for _W {
    fn to_directions(&self) -> Vec<Direction> {
        vec![Direction::W, Direction::NW, Direction::SW]
    }
}