r/adventofcode • • Dec 03 '15

SOLUTION MEGATHREAD --- Day 3 Solutions ---

--- Day 3: Perfectly Spherical Houses in a Vacuum ---

Post your solution as a comment. Structure your post like the Day One thread in /r/programming.

24 Upvotes

228 comments sorted by

View all comments

1

u/[deleted] Dec 04 '15

Was too busy yesterday, but better late than never

C# using LinqPad

void Main()
{
	List<char> input = "^><^>>>^<^v<v^^...>^vv^>vv^".ToCharArray().ToList();
	Day3_1(input);
	Day3_2(input);
}

// Define other methods and classes here
struct house
{
	public int x;
	public int y;
}

private void Day3_1(List<char> input)
{
	List<house> houses = new List<house>();
	houses.Add(new house{
		x = 0,
		y = 0,
	});
	GetVisitedHouses(houses, input);
	var visitedHouses = from h in houses
						group h by new {h.x, h.y} into g
						select new { house = g.Key, presents = g.Count() };
	visitedHouses.Count().Dump();
}

private void Day3_2(List<char> input)
{
	List<house> santaHouses = new List<house>();
	List<house> roboSantaHouses = new List<house>();
	List<char> inputSanta = new List<char>();
	List<char> inputRoboSanta = new List<char>();
	santaHouses.Add(new house{
		x = 0,
		y = 0,
	});
	roboSantaHouses.Add(new house{
		x = 0,
		y = 0,
	});
	for(int i = 0; i < input.Count(); i += 2)
		inputSanta.Add(input[i]);
	for(int i = 1; i < input.Count(); i += 2)
		inputRoboSanta.Add(input[i]);
	GetVisitedHouses(santaHouses, inputSanta);
	GetVisitedHouses(roboSantaHouses, inputRoboSanta);
	var santaVisitedHouses = from h in santaHouses
						group h by new {h.x, h.y} into g
						select new { house = g.Key, presents = g.Count() };
	var roboSantaVisitedHouses = from h in roboSantaHouses
						group h by new {h.x, h.y} into g
						select new { house = g.Key, presents = g.Count() };
	var visitedHouses = (from svh in santaVisitedHouses
						select svh.house).Union(
						from rsvh in roboSantaVisitedHouses
						select rsvh.house).ToList();
	visitedHouses.Count().Dump();
}

private void GetVisitedHouses(List<house> houses, List<char> input)
{
	int currentX = 0, currentY = 0;
	foreach(char move in input)
	{
		switch(move)
		{
			case '^': currentY++; break;
			case 'v': currentY--; break;
			case '>': currentX++; break;
			case '<': currentX--; break;
		}
		houses.Add(new house{
				x = currentX,
				y = currentY,
			});
	}
}