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
use std::ops;

#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
pub enum Direction { Up, Right, Down, Left }

use self::Direction::*;

impl Direction {
    pub fn cw(&self) -> Direction {
        match *self {
            Up => Right,
            Right => Down,
            Down => Left,
            Left => Up,
        }
    }

    pub fn reverse(&self) -> Direction {
        match *self {
            Up => Down,
            Right => Left,
            Down => Up,
            Left => Right,
        }
    }

    pub fn ccw(&self) -> Direction {
        match *self {
            Up => Left,
            Right => Up,
            Down => Right,
            Left => Down,
        }
    }
}

impl Into<(isize, isize)> for Direction {
    fn into(self) -> (isize, isize) {
        match self {
            Up => (0, -1),
            Right => (1, 0),
            Down => (0, 1),
            Left => (-1, 0),
        }
    }
}

impl<'a> Into<(isize, isize)> for &'a Direction {
    fn into(self) -> (isize, isize) {
        match *self {
            Up => (0, -1),
            Right => (1, 0),
            Down => (0, 1),
            Left => (-1, 0),
        }
    }
}

impl ops::Add<(isize, isize)> for Direction {
    type Output = (isize, isize);

    fn add(self, (x, y): (isize, isize)) -> Self::Output {
        let (dx, dy) = self.into();
        (x + dx, y + dy)
    }
}

impl<'a> ops::Add<(isize, isize)> for &'a Direction {
    type Output = (isize, isize);

    fn add(self, o: (isize, isize)) -> Self::Output {
        *self + o
    }
}

impl ops::Add<(usize, usize)> for Direction {
    type Output = (usize, usize);

    fn add(self, (x, y): (usize, usize)) -> Self::Output {
        let (dx, dy) = self.into();
        (
            (x as isize + dx) as usize,
            (y as isize + dy) as usize,
        )
    }
}

impl<'a> ops::Add<(usize, usize)> for &'a Direction {
    type Output = (usize, usize);

    fn add(self, o: (usize, usize)) -> Self::Output {
        *self + o
    }
}

impl ops::Add<Direction> for (isize, isize) {
    type Output = (isize, isize);

    fn add(self, facing: Direction) -> Self::Output {
        facing + self
    }
}

impl<'a> ops::Add<&'a Direction> for (isize, isize) {
    type Output = (isize, isize);

    fn add(self, facing: &'a Direction) -> Self::Output {
        facing + self
    }
}

impl ops::Add<Direction> for (usize, usize) {
    type Output = (usize, usize);

    fn add(self, facing: Direction) -> Self::Output {
        facing + self
    }
}

impl<'a> ops::Add<&'a Direction> for (usize, usize) {
    type Output = (usize, usize);

    fn add(self, facing: &'a Direction) -> Self::Output {
        facing + self
    }
}

impl ops::AddAssign<Direction> for (isize, isize) {
    fn add_assign(&mut self, other: Direction) {
        *self = *self + other
    }
}

impl<'a> ops::AddAssign<&'a Direction> for (isize, isize) {
    fn add_assign(&mut self, other: &'a Direction) {
        *self = *self + other
    }
}

impl ops::AddAssign<Direction> for (usize, usize) {
    fn add_assign(&mut self, other: Direction) {
        *self = *self + other
    }
}

impl<'a> ops::AddAssign<&'a Direction> for (usize, usize) {
    fn add_assign(&mut self, other: &'a Direction) {
        *self = *self + other
    }
}