r/adventofcode • • Dec 06 '15

SOLUTION MEGATHREAD --- Day 6 Solutions ---

--- Day 6: Probably a Fire Hazard ---

Post your solution as a comment. Structure your post like the Day Five thread.

22 Upvotes

171 comments sorted by

View all comments

2

u/JeffJankowski Dec 06 '15

Quick and dirty af C# before I do it in F#:

static int[][] lights;
public static void flip(Point p1, Point p2, bool? on)
{
	for (int i = Math.Min(p1.X, p2.X); i <= Math.Max(p1.X, p2.X); i++)
	{
		for (int j = Math.Min(p1.Y, p2.Y); j <= Math.Max(p1.Y, p2.Y); j++)
			lights[i][j] = Math.Max(0, lights[i][j] + (on.HasValue ? (on.Value ? 1 : -1) : 2));
	}
}

public static Point Convert(string coords)
{
	string[] both = coords.Split(',');
	return new Point(Int32.Parse(both[0]), Int32.Parse(both[1]));
}

public static void Main(string[] args)
{
	lights = new int[1000][];
	for (int i = 0; i < 1000; i++)
		lights[i] = new int[1000];

	using (StreamReader sr = new StreamReader(@"..\..\input.txt"))
	{
		string line;
		while ((line = sr.ReadLine()) != null)
		{
			string op = new String(line.TakeWhile(c => !Char.IsDigit(c)).ToArray()).Trim();
			string[] coords = line.Split(' ').Where(s => Char.IsDigit(s[0])).ToArray();
			Point p1 = Convert(coords[0]);
			Point p2 = Convert(coords[1]);

			switch (op)
			{
				case "toggle":
					flip(p1, p2, null);
					break;
				case "turn off":
					flip(p1, p2, false);
					break;
				case "turn on":
					flip(p1, p2, true);
					break;
			}
		}

		int count = 0;
		for (int i = 0; i < 1000; i++)
		{
			for (int j = 0; j < 1000; j++)
				count += lights[i][j];
		}

		Console.WriteLine(count);
	}

	Console.ReadLine();
}