aoc/year2015/
day03.rs

1//! # Perfectly Spherical Houses in a Vacuum
2//!
3//! We store Santa's path in a [`FastSet`] of [`Point`] objects that deduplicates visited points.
4//! For part two we alternate between Santa and the robot, tracking two points simultaneously and
5//! reusing the same deduplicating logic as part one.
6//!
7//! [`FastSet`]: crate::util::hash
8//! [`Point`]: crate::util::point
9use crate::util::hash::*;
10use crate::util::point::*;
11
12pub fn parse(input: &str) -> Vec<Point> {
13    input.trim().bytes().map(Point::from).collect()
14}
15
16pub fn part1(input: &[Point]) -> usize {
17    deliver(input, |_| true)
18}
19
20pub fn part2(input: &[Point]) -> usize {
21    deliver(input, |i| i % 2 == 0)
22}
23
24fn deliver(input: &[Point], predicate: fn(usize) -> bool) -> usize {
25    let mut santa = ORIGIN;
26    let mut robot = ORIGIN;
27    let mut set = FastSet::with_capacity(10_000);
28    set.insert(ORIGIN);
29
30    for (index, point) in input.iter().enumerate() {
31        if predicate(index) {
32            santa += *point;
33            set.insert(santa);
34        } else {
35            robot += *point;
36            set.insert(robot);
37        }
38    }
39
40    set.len()
41}