aoc/year2017/day11.rs
1//! # Hex Ed
2//!
3//! Hex grid parsing and navigation uses
4//! [Axial Coordinates](https://www.redblobgames.com/grids/hexagons/#coordinates-cube)
5//! exactly as described in the excellent [Red Blob Games](https://www.redblobgames.com/) blog.
6//!
7//! As mentioned in the blog, the Manhattan distance to the center has the formula
8//! `(q.abs() + r.abs() + s.abs()) / 2`
9type Input = (i32, i32);
10
11pub fn parse(input: &str) -> Input {
12 let mut iter = input.bytes();
13 let mut q: i32 = 0;
14 let mut r: i32 = 0;
15 let mut part_one = 0;
16 let mut part_two = 0;
17
18 while let Some(first) = iter.next() {
19 let second = iter.next().unwrap_or(0);
20 let (dq, dr) = match (first, second) {
21 (b'n', b'e') => (1, -1),
22 (b'n', b'w') => (-1, 0),
23 (b'n', _) => (0, -1),
24 (b's', b'e') => (1, 0),
25 (b's', b'w') => (-1, 1),
26 (b's', _) => (0, 1),
27 _ => unreachable!(),
28 };
29
30 // Update axial coordinates.
31 q += dq;
32 r += dr;
33
34 // Two-letter directions (ne, nw, se, sw) need the trailing delimiter consumed.
35 if second == b'e' || second == b'w' {
36 iter.next();
37 }
38
39 // Manhattan distance to the center.
40 // q + r + s = 0, so we can always determine s given the other two.
41 part_one = (q.abs() + r.abs() + (q + r).abs()) / 2;
42 // Keep track of furthest distance.
43 part_two = part_two.max(part_one);
44 }
45
46 (part_one, part_two)
47}
48
49pub fn part1(input: &Input) -> i32 {
50 input.0
51}
52
53pub fn part2(input: &Input) -> i32 {
54 input.1
55}