Skip to content

Instantly share code, notes, and snippets.

@johnboker
Created December 7, 2015 00:59
Show Gist options
  • Save johnboker/19bdce472112f3afd603 to your computer and use it in GitHub Desktop.
Save johnboker/19bdce472112f3afd603 to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Drawing.Imaging;
namespace AdventOfCodeDay6
{
class MainClass
{
public static void Main (string[] args)
{
StreamReader ss = new StreamReader ("../../input.txt");
string line;
var input = new List<string> ();
while ((line = ss.ReadLine ()) != null) {
input.Add (line);
}
var re = new Regex (@".*?(?<X1>[0-9]+),(?<Y1>[0-9]+) through (?<X2>[0-9]+),(?<Y2>[0-9]+)", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
var lights1 = new int[1000, 1000];
var lights2 = new int[1000, 1000];
foreach (var c in input) {
Command command = Command.Toggle;
if (c.StartsWith ("toggle"))
command = Command.Toggle;
if (c.StartsWith ("turn off"))
command = Command.TurnOff;
if (c.StartsWith ("turn on"))
command = Command.TurnOn;
var m = re.Matches (c) [0];
var x1 = int.Parse (m.Groups ["X1"].Value);
var y1 = int.Parse (m.Groups ["Y1"].Value);
var x2 = int.Parse (m.Groups ["X2"].Value);
var y2 = int.Parse (m.Groups ["Y2"].Value);
//Console.WriteLine ($"{command} ({x1}, {y1}) to ({x2}, {y2})");
ProcessCommand1 (lights1, command, x1, y1, x2, y2);
ProcessCommand2 (lights2, command, x1, y1, x2, y2);
}
int onCount = 0;
int brightness = 0;
for (int x = 0; x < 1000; x++) {
for (int y = 0; y < 1000; y++) {
onCount += lights1 [x, y];
brightness += lights2 [x, y];
}
}
SaveImage (lights1, "part1.png");
SaveImage (lights2, "part2.png");
Console.WriteLine ($"Lights On: {onCount}");
Console.WriteLine ($"Lights On: {brightness}");
}
public static void ProcessCommand1 (int[,] lights, Command cmd, int x1, int y1, int x2, int y2)
{
for (int x = x1; x <= x2; x++) {
for (int y = y1; y <= y2; y++) {
if (cmd == Command.Toggle) {
lights [x, y] = 1-lights [x, y];
} else if (cmd == Command.TurnOff) {
lights [x, y] = 0;
} else if (cmd == Command.TurnOn) {
lights [x, y] = 1;
}
}
}
}
public static void ProcessCommand2 (int[,] lights, Command cmd, int x1, int y1, int x2, int y2)
{
for (int x = x1; x <= x2; x++) {
for (int y = y1; y <= y2; y++) {
if (cmd == Command.Toggle) {
lights [x, y] += 2;
} else if (cmd == Command.TurnOff) {
if (lights [x, y] > 0)
lights [x, y]--;
} else if (cmd == Command.TurnOn) {
lights [x, y]++;
}
}
}
}
public static void SaveImage (int[,] lights, string fileName)
{
using (Bitmap b = new Bitmap (1000, 1000)) {
var max = 0;
for (int x = 0; x < 1000; x++) {
for (int y = 0; y < 1000; y++) {
max = Math.Max (max, lights [x, y]);
}
}
for (int x = 0; x < 1000; x++) {
for (int y = 0; y < 1000; y++) {
int v = lights [x, y];
int cv = (int)(((double)v / max) * 255.0);
b.SetPixel (x, y, Color.FromArgb (0xff, cv, cv, cv));
}
}
b.Save ($"../../{fileName}", ImageFormat.Png);
}
}
public enum Command
{
Toggle,
TurnOn,
TurnOff
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment