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 match first {
20 b'n' => match iter.next().unwrap_or(0) {
21 b'e' => {
22 q += 1;
23 r -= 1;
24 }
25 b'w' => q -= 1,
26 _ => r -= 1,
27 },
28 b's' => match iter.next().unwrap_or(0) {
29 b'e' => q += 1,
30 b'w' => {
31 q -= 1;
32 r += 1;
33 }
34 _ => r += 1,
35 },
36 _ => (),
37 }
38
39 // q + r + s = 0, so we can always determine s given the other two.
40 let s = q + r;
41 // Manhattan distance to the center.
42 part_one = (q.abs() + r.abs() + s.abs()) / 2;
43 // Keep track of furthest distance.
44 part_two = part_two.max(part_one);
45 }
46
47 (part_one, part_two)
48}
49
50pub fn part1(input: &Input) -> i32 {
51 input.0
52}
53
54pub fn part2(input: &Input) -> i32 {
55 input.1
56}