aoc/year2016/
day02.rs

1//! # Bathroom Security
2//!
3//! Relies heavily on the [`point`] and [`grid`] modules.
4//!
5//! [`grid`]: crate::util::grid
6//! [`point`]: crate::util::point
7use crate::util::grid::*;
8use crate::util::point::*;
9
10pub fn parse(input: &str) -> Vec<&str> {
11    input.lines().collect()
12}
13
14pub fn part1(input: &[&str]) -> String {
15    let digits = Grid::parse("123\n456\n789");
16    let mut position = ORIGIN;
17    let mut result = String::new();
18
19    for line in input {
20        for b in line.bytes() {
21            let next = position + Point::from(b);
22            if next.x.abs() <= 1 && next.y.abs() <= 1 {
23                position = next;
24            }
25        }
26        result.push(digits[position + Point::new(1, 1)] as char);
27    }
28
29    result
30}
31
32pub fn part2(input: &[&str]) -> String {
33    let digits = Grid::parse("##1##\n#234#\n56789\n#ABC#\n##D##");
34    let mut position = Point::new(-2, 0);
35    let mut result = String::new();
36
37    for line in input {
38        for b in line.bytes() {
39            let next = position + Point::from(b);
40            if next.manhattan(ORIGIN) <= 2 {
41                position = next;
42            }
43        }
44        result.push(digits[position + Point::new(2, 2)] as char);
45    }
46
47    result
48}