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