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
//! # Hex Ed
//!
//! Hex grid parsing and navigation uses
//! [Axial Coordinates](https://www.redblobgames.com/grids/hexagons/#coordinates-cube)
//! exactly as described in the excellent [Red Blob Games](https://www.redblobgames.com/) blog.
//!
//! As mentioned in the blog, the Manhattan distance to the center has the formula
//! `(q.abs() + r.abs() + s.abs()) / 2`
type Input = (i32, i32);
pub fn parse(input: &str) -> Input {
let mut iter = input.bytes();
let mut q: i32 = 0;
let mut r: i32 = 0;
let mut part_one = 0;
let mut part_two = 0;
while let Some(first) = iter.next() {
match first {
b'n' => match iter.next().unwrap_or(0) {
b'e' => {
q += 1;
r -= 1;
}
b'w' => q -= 1,
_ => r -= 1,
},
b's' => match iter.next().unwrap_or(0) {
b'e' => q += 1,
b'w' => {
q -= 1;
r += 1;
}
_ => r += 1,
},
_ => (),
}
// q + r + s = 0, so we can always determine s given the other two.
let s = q + r;
// Manhattan distance to the center.
part_one = (q.abs() + r.abs() + s.abs()) / 2;
// Keep track of furthest distance.
part_two = part_two.max(part_one);
}
(part_one, part_two)
}
pub fn part1(input: &Input) -> i32 {
input.0
}
pub fn part2(input: &Input) -> i32 {
input.1
}