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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/// A representation of a 2D grid of u8s.
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct Grid {
    /// Store the numbers in a 1D list...
    pub numbers: Vec<u8>,
    /// ...and use the width to determine the 1D offset as a 2D co-ordinate
    pub width: usize,
}

impl From<String> for Grid {
    /// Turn the characters into digits and concatenate, caching the width
    fn from(string: String) -> Self {
        Grid::from_string_with_mapping(
            &string,
            |c| {
                c.to_digit(10)
                 .expect(format!("{} is not a digit", c).as_str()) as u8
            },
        )
    }
}

/// Temporary struct representing an iterator over a grid
pub struct GridCoords<'a> {
    /// Reference to the grid being iterated
    grid: &'a Grid,
    /// The current position of the iterator
    pos: usize,
}

impl<'a> Iterator for GridCoords<'a> {
    type Item = ((usize, usize), u8);

    fn next(&mut self) -> Option<Self::Item> {
        let curr = self.grid.get_with_coords(self.pos);
        self.pos = self.pos + 1;

        curr
    }
}

impl Grid {
    /// Build a new grid with a mapping function to generate the date for each cell
    pub fn new<F>(width: usize, height: usize, init: F) -> Self
        where F: Fn(usize, usize) -> u8
    {
        let mut numbers = Vec::new();
        for y in 0..height {
            for x in 0..width {
                numbers.push(init(x, y))
            }
        }

        Self { width, numbers }
    }

    pub fn from_string_with_mapping(input: &String, mapping: fn(char) -> u8) -> Self {
        let width: usize = input.lines().next().unwrap_or("").len();

        let numbers = input
            .lines()
            .flat_map(|line| {
                return line.chars().map(mapping);
            })
            .collect();

        Self { numbers, width }
    }

    /// Helper to abstract iterating over the whole grid
    pub fn iter(&self) -> GridCoords {
        GridCoords { grid: self, pos: 0 }
    }

    /// Return the value at the given co-ordinates
    pub fn get(&self, y: usize, x: usize) -> Option<u8> {
        self.pos_of((y, x))
            .and_then(|p| self.numbers.get(p))
            .map(|&v| v)
    }

    /// Update the value in a given cell
    pub fn set(&mut self, y: usize, x: usize, val: u8) -> bool {
        match self.pos_of((y, x)) {
            Some(pos) => {
                self.numbers[pos] = val;
                true
            }
            None => false,
        }
    }

    /// Turn (y, x) coordinates into a position in the underlying array
    pub fn pos_of(&self, (y, x): (usize, usize)) -> Option<usize> {
        if x >= self.width {
            return None;
        }

        let pos = x + y * self.width;

        if pos >= self.numbers.len() {
            return None;
        }

        return Some(pos);
    }

    /// Calculate the height from the
    pub fn height(&self) -> usize {
        (self.numbers.len() + self.width - 1) / self.width
    }

    /// Sum the cells in the grid
    pub fn sum(&self) -> usize {
        self.numbers.iter().map(|&v| usize::from(v)).sum()
    }

    /// Used by [`GridCoords::next`]
    pub fn get_with_coords(&self, pos: usize) -> Option<((usize, usize), u8)> {
        let x = pos % self.width;
        let y = pos / self.width;

        self.numbers.get(pos).map(|&val| ((y, x), val))
    }

    pub fn get_orthogonal_surrounds(&self, (y, x): (usize, usize)) -> Vec<((usize, usize), u8)> {
        [(-1, 0), (0, 1), (1, 0), (0, -1)] // N E S W
            .iter()
            .flat_map(|&(dy, dx)| self.get_relative(y, x, dy, dx))
            .collect()
    }

    pub fn get_relative(
        &self,
        y: usize,
        x: usize,
        dy: isize,
        dx: isize,
    ) -> Option<((usize, usize), u8)> {
        let y1 = (y as isize) + dy;
        let x1 = (x as isize) + dx;

        if y1 >= 0 && x1 >= 0 {
            self.get(y1 as usize, x1 as usize)
                .map(|val| ((y1 as usize, x1 as usize), val))
        } else {
            None
        }
    }

    /// Dump the grid to stdout - useful for visualising the grid when debugging
    #[allow(dead_code)]
    pub fn print(&self) -> String {
        self.print_with(
            |v| if v <= 9 {
                v.to_string()
            } else {
                "#".to_string()
            }
        )
    }

    #[allow(dead_code)]
    pub fn print_with<F>(&self, cell_renderer: F) -> String
        where F: Fn(u8) -> String
    {
        let (_, out) = self
            .iter()
            .fold((0usize, "".to_string()), |(prev_y, out), ((y, _), v)| {
                (
                    y,
                    format!(
                        "{}{}{}",
                        out,
                        if y != prev_y { "\n" } else { "" },
                        cell_renderer(v),
                    ),
                )
            });

        out.to_string()
    }
}

#[cfg(test)]
mod tests {
    use crate::util::grid::Grid;

    fn sample_input() -> String {
        "12345\n\
        23456\n\
        34567\n\
        45678\n\
        56789"
            .to_string()
    }


    #[test]
    fn can_set_and_get() {
        let mut grid = Grid::from(sample_input());

        assert_eq!(grid.get(0, 0), Some(1));
        assert_eq!(grid.get(0, 4), Some(5));
        assert_eq!(grid.get(4, 0), Some(5));
        assert_eq!(grid.get(4, 4), Some(9));
        assert_eq!(grid.get(5, 4), None);
        assert_eq!(grid.get(3, 5), None);
        assert_eq!(grid.get(17, 29), None);

        grid.set(4, 4, 17);

        assert_eq!(grid.get(4, 4), Some(17));
    }

    #[test]
    fn can_print() {
        let input = sample_input();

        let mut grid = Grid::from(input.clone());

        assert_eq!(grid.print(), input);

        grid.set(4, 4, 10);

        assert_eq!(grid.print(), input.replace("9", "#"));
    }

    //noinspection SpellCheckingInspection
    #[test]
    fn can_print_with_custom_output() {
        let input = sample_input();
        let grid = Grid::from(input);

        let expected = "abcde\n\
        bcdef\n\
        cdefg\n\
        defgh\n\
        efghi"
            .to_string();

        assert_eq!(grid.print_with(|v| char::from(v + 96).to_string()), expected);
    }

    #[test]
    fn set_ignores_out_of_bounds() {
        let mut grid = Grid::from(sample_input());

        assert_eq!(grid.set(5, 0, 9), false);
        assert_eq!(grid.set(0, 5, 9), false);
        assert_eq!(grid.set(5, 5, 9), false);
        // unchanged
        assert_eq!(grid.print(), sample_input());
    }

    #[test]
    fn can_calc_height() {
        assert_eq!(Grid::from("1\n2\n3\n4".to_string()).height(), 4);
        assert_eq!(Grid::from("12\n34\n56".to_string()).height(), 3);
        assert_eq!(Grid::from("123\n34".to_string()).height(), 2);
    }

    #[test]
    fn can_build_grid() {
        let grid = Grid::new(3, 3, |x, y| u8::try_from(x).unwrap() + u8::try_from(y).unwrap());
        assert_eq!(grid.width, 3);
        assert_eq!(grid.height(), 3);
        assert_eq!(grid.print(), "012\n123\n234");
    }

    #[test]
    fn can_sum_grid() {
        let grid = Grid::new(3, 3, |x, y| u8::try_from(x).unwrap() + u8::try_from(y).unwrap());
        assert_eq!(grid.sum(), 18)
    }
}