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.is_multiple_of(2))
22}
23
24fn deliver(input: &[Point], predicate: fn(usize) -> bool) -> usize {
25    let mut santa = ORIGIN;
26    let mut robot = ORIGIN;
27
28    let mut set = FastSet::with_capacity(input.len());
29    set.insert(ORIGIN);
30
31    for (index, &point) in input.iter().enumerate() {
32        if predicate(index) {
33            santa += point;
34            set.insert(santa);
35        } else {
36            robot += point;
37            set.insert(robot);
38        }
39    }
40
41    set.len()
42}