Skip to content

Instantly share code, notes, and snippets.

@luxbock
Created March 5, 2018 22:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luxbock/39489ba5122241316d5ce5740ba1a2e6 to your computer and use it in GitHub Desktop.
Save luxbock/39489ba5122241316d5ce5740ba1a2e6 to your computer and use it in GitHub Desktop.
/// Santa is delivering presents to an infinite two-dimensional grid of houses.
///
/// He begins by delivering a present to the house at his starting location, and then an elf at the North Pole calls him via radio and tells him where to move next. Moves are always exactly one house to the north (^), south (v), east (>), or west (<). After each move, he delivers another present to the house at his new location.
///
/// However, the elf back at the north pole has had a little too much eggnog, and so his directions are a little off, and Santa ends up visiting some houses more than once. How many houses receive at least one present?
///
/// For example:
///
/// > delivers presents to 2 houses: one at the starting location, and one to the east.
/// ^>v< delivers presents to 4 houses in a square, including twice to the house at his starting/ending location.
/// ^v^v^v^v^v delivers a bunch of presents to some very lucky children at only 2 houses.
/// Your puzzle answer was 2081.
///
/// The first half of this puzzle is complete! It provides one gold star: *
///
/// --- Part Two ---
/// The next year, to speed up the process, Santa creates a robot version of himself, Robo-Santa, to deliver presents with him.
///
/// Santa and Robo-Santa start at the same location (delivering two presents to the same starting house), then take turns moving based on instructions from the elf, who is eggnoggedly reading from the same script as the previous year.
///
/// This year, how many houses receive at least one present?
///
/// For example:
///
/// ^v delivers presents to 3 houses, because Santa goes north, and then Robo-Santa goes south.
/// ^>v< now delivers presents to 3 houses, and Santa and Robo-Santa end up back where they started.
/// ^v^v^v^v^v now delivers presents to 11 houses, with Santa going one direction and Robo-Santa going the other.
fn main() {
let input = read_input("resources/2015-day-3.txt");
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
struct House(i32, i32);
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
struct Direction(i32, i32);
#[derive(Debug)]
struct ParseError;
impl Default for House {
fn default() -> Self {
House(0, 0)
}
}
impl AddAssign<Direction> for House {
fn add_assign(&mut self, dir: Direction) {
let Direction(x, y) = dir;
self.0 += x;
self.1 += y;
}
}
fn direction(ch: char) -> Result<Direction, ParseError> {
match ch {
'<' => Ok(Direction(-1, 0)),
'>' => Ok(Direction( 1, 0)),
'^' => Ok(Direction( 0, 1)),
'v' => Ok(Direction( 0, -1)),
_ => Err(ParseError)
}
}
fn trace_path(dirs: Vec<Direction>) -> () {
// ???
}
let dirs: Vec<Direction> = input
.trim_right()
.chars()
.map(|c| direction(c).expect("Direction not recognized!"))
.collect();
let houses: HashSet<House> = dirs.iter()
.scan(House(0, 0), |acc, dir| {
*acc += dir.clone();
Some(*acc)})
.collect();
println!("[Day 3] Houses visited: {}", 1 + houses.len());
let mut santa_dirs: Vec<Direction> = vec![];
let mut robot_dirs: Vec<Direction> = vec![];
for (i, dir) in dirs.into_iter().enumerate() {
if i % 2 == 0 {
santa_dirs.push(dir);
} else {
robot_dirs.push(dir);
}
}
let mut houses_2: HashSet<House> = santa_dirs.into_iter()
.scan(House(0, 0), |acc, dir| {
*acc += dir.clone();
Some(*acc)
})
.collect();
houses_2.extend(robot_dirs
.into_iter()
.scan(House(0, 0), |acc, dir| {
*acc += dir.clone();
Some(*acc)
}));
println!("[Day 3] Houses visited by Santa and Robot-Santa {}", houses_2.len());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment