2015 day 18
This commit is contained in:
parent
adfbbf1b0a
commit
52949ca01b
|
@ -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);
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue