-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
75 lines (60 loc) · 1.62 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
using AdventOfCode.Common;
using Coor = AdventOfCode.Common.Coor<int>;
char[,] map = Resources.GetInputFileLines().ParseAsArray();
const char O = 'O';
var height = map.Height();
var width = map.Width();
Tilt(Coor.Up);
Console.WriteLine($"Part 1: {GetWeight()}");
map = Resources.GetInputFileLines().ParseAsArray();
var iterations = 1_000_000_000;
var knownStates = new Dictionary<string, int>();
var memoize = true;
for (int iteration = 1; iteration <= iterations; iteration++)
{
SpinCycle();
if (!memoize) continue;
var hash = map.ToFlatString();
if (knownStates.TryGetValue(hash, out var i))
{
// A cycle of the length of (iteration - i) will repeat now, we will just run the tail
iteration = iterations - ((iterations - iteration) % (iteration - i));
memoize = false;
}
else
{
knownStates.Add(hash, iteration);
}
}
Console.WriteLine($"Part 2: {GetWeight()}");
void SpinCycle()
{
Tilt(Coor.Up);
Tilt(Coor.Left);
Tilt(Coor.Down);
Tilt(Coor.Right);
}
void Tilt(Coor direction)
{
bool movement;
do
{
movement = false;
map.ForEach((row, col, value) =>
{
if (value != O) return;
var c = new Coor(row, col) + direction;
while (c.InBoundsOf(map) && map.Get(c) == '.')
{
map.Set(c - direction, '.');
map.Set(c, O);
movement = true;
c += direction;
}
});
} while (movement);
}
int GetWeight() => map
.AllCoordinates()
.Select(c => map.Get(c) == O ? height - c.Row : 0)
.Sum();