Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using System.IO;
using System.Linq;
namespace AdventOfCodeDay18
{
class Program
{
static void Main(string[] args)
{
bool doPartTwo = false;
var ss = new StreamReader("../../input1.txt");
var input = new bool[100, 100];
var cnt = 0;
string line;
while ((line = ss.ReadLine()) != null)
{
for (int i = 0; i < 100; i++)
{
input[i, cnt] = line[i] == '#';
}
cnt++;
}
var currentState = input;
if (doPartTwo)
{
currentState[0, 0] = true;
currentState[0, 99] = true;
currentState[99, 0] = true;
currentState[99, 99] = true;
}
var nextState = new bool[100, 100];
for (var step = 0; step < 100; step++)
{
for (var col = 0; col < 100; col++)
{
for (var row = 0; row < 100; row++)
{
var neighbors = (new[] {
new Coordinate(row-1, col-1),
new Coordinate(row-1, col),
new Coordinate(row-1, col+1),
new Coordinate(row, col-1),
new Coordinate(row, col+1),
new Coordinate(row+1, col-1),
new Coordinate(row+1, col),
new Coordinate(row+1, col+1),
}).Where(a => a.IsValid());
var sum = neighbors.Sum(a => currentState[a.Row, a.Col] ? 1 : 0);
if (currentState[row, col])
{
nextState[row, col] = sum == 2 || sum == 3;
}
else
{
nextState[row, col] = sum == 3;
}
if (doPartTwo)
{
nextState[0, 0] = true;
nextState[0, 99] = true;
nextState[99, 0] = true;
nextState[99, 99] = true;
}
}
}
currentState = nextState;
nextState = new bool[100, 100];
}
var onCount = 0;
for (var col = 0; col < 100; col++)
{
for (var row = 0; row < 100; row++)
{
onCount += currentState[row, col] ? 1 : 0;
}
}
Console.WriteLine($"Lights On: {onCount}");
Console.ReadLine();
}
}
public class Coordinate
{
public Coordinate(int r, int c)
{
Row = r;
Col = c;
}
public int Row { get; set; }
public int Col { get; set; }
public bool IsValid()
{
return Row >= 0 && Col >= 0 && Row < 100 && Col < 100;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.