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.
Read the full puzzle.
using System.Collections.Generic;
using System.Linq;
namespace AdventOfCode.Y2015.Day03;
[ProblemName("Perfectly Spherical Houses in a Vacuum")]
class Solution : Solver {
public object PartOne(string input) => Run(input, 1);
public object PartTwo(string input) => Run(input, 2);
int Run(string input, int actors) {
var seen = new HashSet<(int, int)>();
var pos = new(int irow, int icol)[actors];
for (var i = 0; i < actors; i++) {
pos[i] = (0, 0);
}
seen.Add((0,0));
var actor = 0;
foreach (var ch in input) {
switch (ch) {
case 'v': pos[actor].irow++; break;
case '<': pos[actor].icol--; break;
case '>': pos[actor].icol++; break;
case '^': pos[actor].irow--; break;
}
seen.Add(pos[actor]);
actor = (actor + 1) % actors;
}
return seen.Count();
}
}
Please ☆ my repo if you like it!