Metadata
-
Date
-
Tagged
-
Part of series
-
Older post
-
Newer post
Advent of Code 2015 Day 3
Day 3: Perfectly Spherical Houses in a Vacuum
https://adventofcode.com/2015/day/3
Santa is delivering presents to an infinite two-dimensional grid of houses.
Today’s input is a list of move instructions for Santa.
Moves are always a single step:
^
Northv
South>
East<
West
An example input looks like this:
After every move, Santa delivers a present to the house at that coordinate.
Part 1
By following these instructions, Santa ends up visiting some houses multiple times.
The question asks how many houses receive at least one present.
Helpers
A data structure to keep track of a coordinate in that 2D space.
About that derive
bit on top:
Those are some automatically derived traits
I use to be able to use Coord
in a few different ways.
They enable several behaviours of our Coord
struct.
Like Eq
to be able to tell if two Coord
s are equal.
Option 1: A set of coordinates
Santa starts at a random coordinate. I picked 0,0 but the starting location doesn’t matter.
After every move, the new coordinate is added to a set of visited houses. Because it’s a set, it won’t store duplicates and only count a specific coordinate once.
At the end, the length of that set is the answer to part 1.
Option 2: Turning the list of moves into a list of coordinates
This solution turns the iterator of moves into an iterator of coordinates. Only the unique coordinates in that iterator are kept. The amount of items in that iterator is the answer to part 1.
Main code for part 1
Part 2
The next year, Santa creates a robot that can deliver presents too, “Robo-Santa”.
They both start at the same coordinate.
They take turns moving based on the instructions.
The list of instructions is still the same one.
The question asks how many houses receive at least one present.
Option 1: A set of coordinates
Similar to part 1, but keep track of 2 coordinates. To check which santa moves, check if the index of the move is divisible by 2.
Option 2: Turning the list of moves into a list of coordinates
Identical logic changes to convert from part 1 to part 2 as in Option 1: