advent_of_code/year2015/
day03.rs

1use crate::input::Input;
2use std::collections::HashSet;
3
4pub fn solve(input: &Input) -> Result<u32, String> {
5    let mut visited_houses = HashSet::new();
6
7    let mut santa_position = (0, 0);
8    visited_houses.insert(santa_position);
9
10    let mut robo_santa_position = (0, 0);
11
12    for (idx, c) in input.text.chars().enumerate() {
13        let mover = if input.is_part_one() || idx % 2 == 0 {
14            &mut santa_position
15        } else {
16            &mut robo_santa_position
17        };
18        match c {
19            '>' => {
20                mover.0 += 1;
21            }
22            '<' => {
23                mover.0 -= 1;
24            }
25            '^' => {
26                mover.1 -= 1;
27            }
28            'v' => {
29                mover.1 += 1;
30            }
31            _ => {
32                return Err(format!("Invalid input char '{c}'"));
33            }
34        }
35
36        visited_houses.insert(*mover);
37    }
38
39    Ok(visited_houses.len() as u32)
40}
41
42#[test]
43pub fn tests() {
44    use crate::input::{test_part_one, test_part_two};
45
46    let real_input = include_str!("day03_input.txt");
47    test_part_one!(real_input => 2572);
48    test_part_two!(real_input => 2631);
49}