feat(2015-03): Add day 3 solution

This commit is contained in:
Josh Creek
2022-10-13 12:41:15 +01:00
parent c2752a38cc
commit 53330a4f5d
3 changed files with 223 additions and 0 deletions
File diff suppressed because one or more lines are too long
+197
View File
@@ -0,0 +1,197 @@
using System.Linq.Expressions;
using System.Reflection;
using System.Reflection.Metadata.Ecma335;
namespace AOC.Tests.Y2015
{
[TestFixture, Parallelizable(ParallelScope.All)]
public class Day03
{
protected string GetThisClassName() { return this.GetType().Name; }
private string realData;
private class House
{
public int VisitedCount { get; set; }
public int X { get; set; }
public int Y { get; set; }
};
[SetUp]
public void Setup()
{
realData = File.ReadAllLines(Path.Combine(TestContext.CurrentContext.TestDirectory, "Y2015", "Data", $"{GetThisClassName()}.dat"))[0];
}
private void DeliverPresentToHouse(int x, int y, ref List<House> houses)
{
if (houses.FindIndex(h => h.X == x && h.Y ==y) > -1)
{
foreach (House house in houses.Where(h => h.X == x && h.Y == y))
{
house.VisitedCount += 1;
}
}
else
{
House house = new House()
{
VisitedCount = 1,
X = x,
Y = y,
};
houses.Add(house);
}
}
[TestCase(">", 2)]
[TestCase("^>v<", 4)]
[TestCase("^v^v^v^v^v", 2)]
[TestCase(null, 2565)] // The actual answer
public void Part1(string input, int? expected)
{
string lines = input != null ? input : realData;
int currentX = 0;
int currentY = 0;
List<House> houses = new List<House>();
House firstHouse = new House()
{
VisitedCount = 1,
X = currentX,
Y = currentY,
};
houses.Add(firstHouse);
foreach (char movement in lines)
{
switch (movement)
{
case '^':
// North
currentY += 1;
break;
case 'v':
// South
currentY -= 1;
break;
case '<':
// East
currentX += 1;
break;
case '>':
// West
currentX -= 1;
break;
}
DeliverPresentToHouse(currentX, currentY, ref houses);
}
int numberOfHousesVisited = houses.Count;
if (expected != null)
{
Assert.That(numberOfHousesVisited, Is.EqualTo(expected.Value));
}
Console.WriteLine($"Part 1: {numberOfHousesVisited}");
}
[TestCase("^v", 3)]
[TestCase("^>v<", 3)]
[TestCase("^v^v^v^v^v", 11)]
[TestCase(null, 2639)] // The actual answer
public void Part2(string input, int? expected)
{
string lines = input != null ? input : realData;
int currentX = 0;
int currentY = 0;
int roboSantacurrentX = 0;
int roboSantacurrentY = 0;
List<House> houses = new List<House>();
House firstHouse = new House()
{
VisitedCount = 2, // Visited by both Santa and Robo-Santa
X = currentX,
Y = currentY,
};
houses.Add(firstHouse);
bool isSanta = true;
foreach (char movement in lines)
{
switch (movement)
{
case '^':
// North
if (isSanta)
{
currentY += 1;
}
else
{
roboSantacurrentY += 1;
}
break;
case 'v':
// South
if (isSanta)
{
currentY -= 1;
}
else
{
roboSantacurrentY -= 1;
}
break;
case '<':
// East
if (isSanta)
{
currentX += 1;
}
else
{
roboSantacurrentX += 1;
}
break;
case '>':
// West
if (isSanta)
{
currentX -= 1;
}
else
{
roboSantacurrentX -= 1;
}
break;
}
if (isSanta)
{
DeliverPresentToHouse(currentX, currentY, ref houses);
isSanta = false;
}
else
{
DeliverPresentToHouse(roboSantacurrentX, roboSantacurrentY, ref houses);
isSanta = true;
}
}
int numberOfHousesVisited = houses.Count;
if (expected != null)
{
Assert.That(numberOfHousesVisited, Is.EqualTo(expected.Value));
}
Console.WriteLine($"Part 2: {numberOfHousesVisited}");
}
}
}
@@ -0,0 +1,25 @@
--- Day 3: Perfectly Spherical Houses in a Vacuum ---
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.
--- 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.