aoc/year2017/
day19.rs

1//! # A Series of Tubes
2//!
3//! Uses the utility [`Grid`] to parse the input, then the [`Point`] class to follow the path.
4//!
5//! [`Grid`]: crate::util::grid
6//! [`Point`]: crate::util::point
7use crate::util::grid::*;
8use crate::util::point::*;
9
10type Input = (String, u32);
11
12pub fn parse(input: &str) -> Input {
13    let grid = Grid::parse(input);
14
15    let mut position = grid.find(b'|').unwrap();
16    let mut direction = DOWN;
17
18    let mut part_one = String::new();
19    let mut part_two = 0;
20
21    loop {
22        let next = grid[position];
23
24        match next {
25            b'+' => {
26                let left = direction.counter_clockwise();
27                let right = direction.clockwise();
28                direction = if grid[position + right] == b' ' { left } else { right };
29            }
30            b' ' => break,
31            _ if next.is_ascii_alphabetic() => part_one.push(next as char),
32            _ => (),
33        }
34
35        position += direction;
36        part_two += 1;
37    }
38
39    (part_one, part_two)
40}
41
42pub fn part1(input: &Input) -> &str {
43    input.0.as_str()
44}
45
46pub fn part2(input: &Input) -> u32 {
47    input.1
48}