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