-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.cs
125 lines (99 loc) · 3.84 KB
/
Solution.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System;
using System.Collections.Generic;
using System.Linq;
namespace AdventOfCode2020.Day17
{
internal class Solution
{
private const int NumberOfIterations = 6;
private readonly IDictionary<(int X, int Y), bool> _initialState;
private readonly ConwaySimulator _simulator;
public Solution(IReadOnlyList<string> input)
{
_simulator = new ConwaySimulator(NumberOfIterations, NextCellState);
var height = input.Count;
var width = input[0].Length;
_initialState = new Dictionary<(int, int), bool>();
for (var x = 0; x < width; x++)
for (var y = 0; y < height; y++)
{
_initialState[(x, y)] = input[y][x] == '#';
}
}
public int PartOne()
{
var initial = _initialState.ToDictionary(x => (IConwayCell) new Point3D(x.Key.X, x.Key.Y, 0), x => x.Value);
var state = _simulator.Simulate(initial);
return state.Count(x => x.Value);
}
public int PartTwo()
{
var initial = _initialState.ToDictionary(x => (IConwayCell) new Point4D(x.Key.X, x.Key.Y, 0, 0), x => x.Value);
var state = _simulator.Simulate(initial);
return state.Count(x => x.Value);
}
private static bool NextCellState(bool isActive, int activeNeighbours) =>
isActive ? activeNeighbours == 2 || activeNeighbours == 3 : activeNeighbours == 3;
}
internal class Point3D : IConwayCell
{
private readonly Lazy<IEnumerable<IConwayCell>> _neighbours;
public Point3D(int x, int y, int z)
{
X = x;
Y = y;
Z = z;
_neighbours = new Lazy<IEnumerable<IConwayCell>>(GetNeighbours);
}
public int X { get; }
public int Y { get; }
public int Z { get; }
public IEnumerable<IConwayCell> Neighbours => _neighbours.Value;
public bool Equals(Point3D other) => X == other.X && Y == other.Y && Z == other.Z;
public override bool Equals(object? obj) => obj is Point3D other && Equals(other);
public override int GetHashCode() => HashCode.Combine(X, Y, Z);
private IEnumerable<IConwayCell> GetNeighbours()
{
for (var dx = -1; dx <= 1; dx++)
for (var dy = -1; dy <= 1; dy++)
for (var dz = -1; dz <= 1; dz++)
{
if (dx == 0 && dy == 0 && dz == 0)
continue;
yield return new Point3D(X + dx, Y + dy, Z + dz);
}
}
}
internal class Point4D : IConwayCell
{
private readonly Lazy<IEnumerable<IConwayCell>> _neighbours;
public Point4D(int a, int b, int c, int d)
{
A = a;
B = b;
C = c;
D = d;
_neighbours = new Lazy<IEnumerable<IConwayCell>>(GetNeighbours);
}
public int A { get; }
public int B { get; }
public int C { get; }
public int D { get; }
public IEnumerable<IConwayCell> Neighbours => _neighbours.Value;
public bool Equals(Point4D other) => A == other.A && B == other.B && C == other.C && D == other.D;
public override bool Equals(object? obj) => obj is Point4D other && Equals(other);
public override int GetHashCode() => HashCode.Combine(A, B, C);
private IEnumerable<IConwayCell> GetNeighbours()
{
for (var da = -1; da <= 1; da++)
for (var db = -1; db <= 1; db++)
for (var dc = -1; dc <= 1; dc++)
for (var dd = -1; dd <= 1; dd++)
{
if (da == 0 && db == 0 && dc == 0 && dd == 0)
continue;
yield return new Point4D(A + da, B + db, C + dc, D + dd);
}
}
}
}