-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
90 lines (70 loc) · 1.86 KB
/
Program.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
using AdventOfCode.Common;
using Coor = AdventOfCode.Common.Coor<int>;
var map = Resources.GetInputFileLines().ParseAsJaggedArray(c => c - '0');
static int Step(int[][] map)
{
var flashingOctos = new Queue<Coor>();
// First increase all by 1
for (int row = 0; row < map.Length; row++)
{
for (int column = 0; column < map[row].Length; column++)
{
if (++map[row][column] == 10)
{
flashingOctos.Enqueue(new Coor(row, column));
}
}
}
// Flash
var flashedOctos = new Queue<Coor>();
while (flashingOctos.TryDequeue(out var flashing))
{
flashedOctos.Enqueue(flashing);
foreach (var direction in Coor.NineWayNeighbours)
{
var around = flashing + direction;
// Out of bounds
if (around.Y < 0 || around.X < 0 || around.Y >= map.Length || around.X >= map[0].Length)
{
continue;
}
if (++map[around.Y][around.X] == 10)
{
flashingOctos.Enqueue(new Coor(around.Y, around.X));
}
}
}
var flashes = flashedOctos.Count;
// Reset flashed to 0
while (flashedOctos.TryDequeue(out var flashed))
{
map[flashed.Y][flashed.X] = 0;
}
return flashes;
}
static long Part1(int[][] map)
{
long flashes = 0;
for (int i = 0; i < 100; i++)
{
flashes += Step(map);
}
return flashes;
}
static int Part2(int[][] map)
{
int step = 0;
while (true)
{
step++;
if (Step(map) == map.Length * map[0].Length)
{
return step;
}
}
}
Console.WriteLine($"Part 1: {Part1(map)}");
map = Resources.GetInputFileLines()
.Select(line => line.Select(n => n - '0').ToArray())
.ToArray();
Console.WriteLine($"Part 2: {Part2(map)}");