ben
/
aoc
1
0
Fork 0

2015 day 18

This commit is contained in:
Ben Harris 2023-09-20 14:38:39 -04:00
parent adfbbf1b0a
commit 52949ca01b
2 changed files with 53 additions and 9 deletions

View File

@ -9,7 +9,7 @@ public class Test2015
[DataRow(typeof(Day01), "232", "1783")]
[DataRow(typeof(Day02),"1586300", "3737498")]
[DataRow(typeof(Day03), "2081", "2341")]
// [DataRow(typeof(Day04), "346386", "9958218")]
[DataRow(typeof(Day04), "346386", "9958218")]
[DataRow(typeof(Day05), "258", "53")]
// [DataRow(typeof(Day06), "543903", "14687245")]
[DataRow(typeof(Day07), "3176", "14710")]
@ -23,6 +23,7 @@ public class Test2015
[DataRow(typeof(Day15), "222870", "117936")]
[DataRow(typeof(Day16), "103", "405")]
[DataRow(typeof(Day17), "1304", "18")]
[DataRow(typeof(Day18), "1061", "1006")]
public void CheckAllDays(Type dayType, string part1, string part2)
{
Common.CheckDay(dayType, part1, part2);

View File

@ -3,17 +3,60 @@ namespace AOC2015;
/// <summary>
/// Day 18: <a href="https://adventofcode.com/2015/day/18"/>
/// </summary>
public sealed class Day18 : Day
public sealed class Day18() : Day(2015, 18, "Like a GIF For Your Yard")
{
public Day18() : base(2015, 18, "Like a GIF For Your Yard")
{
}
private bool[][] _lights;
public override void ProcessInput()
{
var input = Input.ToArray();
_lights = Enumerable.Range(0, 102).Select(_ => new bool[102]).ToArray();
for (var i = 0; i < 100; i++)
for (var j = 0; j < 100; j++)
_lights[i + 1][j + 1] = input[i][j] == '#';
}
public override object Part1() => "";
private static bool[][] DoStep(bool[][] lights)
{
var nextGrid = Enumerable.Range(0, 102).Select(_ => new bool[102]).ToArray();
for (var i = 1; i < 101; i++)
{
for (var j = 1; j < 101; j++)
{
var activeNeighbors = new[]
{
lights[i - 1][j - 1],
lights[i - 1][j],
lights[i - 1][j + 1],
lights[i][j - 1],
lights[i][j + 1],
lights[i + 1][j - 1],
lights[i + 1][j],
lights[i + 1][j + 1]
}.Count(n => n);
public override object Part2() => "";
}
nextGrid[i][j] = lights[i][j] ? activeNeighbors is 2 or 3 : activeNeighbors == 3;
}
}
return nextGrid;
}
public override object Part1()
{
for (var i = 0; i < 100; i++) _lights = DoStep(_lights);
return _lights.Sum(l => l.Count(x => x));
}
public override object Part2()
{
ProcessInput();
for (var i = 0; i < 100; i++)
{
_lights = DoStep(_lights);
_lights[1][1] = _lights[1][100] = _lights[100][1] = _lights[100][100] = true;
}
return _lights.Sum(l => l.Count(x => x));
}
}