-
Notifications
You must be signed in to change notification settings - Fork 10
/
Day11.cs
84 lines (68 loc) · 2.46 KB
/
Day11.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
using System;
using AdventOfCode.CSharp.Common;
namespace AdventOfCode.CSharp.Y2023.Solvers;
public class Day11 : ISolver
{
public static void Solve(ReadOnlySpan<byte> input, Solution solution)
{
int rowLen = input.IndexOf((byte)'\n') + 1;
int height = input.Length / rowLen;
int[] galaxiesPerCol = new int[rowLen - 1];
long part1 = 0;
long part2 = 0;
int seenGalaxies = 0;
long part1TotalDistanceFromSeenGalaxies = 0;
long part2TotalDistanceFromSeenGalaxies = 0;
for (int y = 0; y < height; y++)
{
ReadOnlySpan<byte> line = input.Slice(0, rowLen - 1);
int i = 0;
bool rowIsGap = true;
while (true)
{
int nextGalaxyLocation = line.IndexOf((byte)'#');
if (nextGalaxyLocation == -1)
break;
part1 += part1TotalDistanceFromSeenGalaxies;
part2 += part2TotalDistanceFromSeenGalaxies;
rowIsGap = false;
seenGalaxies++;
galaxiesPerCol[i + nextGalaxyLocation]++;
line = line.Slice(nextGalaxyLocation + 1);
i += nextGalaxyLocation + 1;
}
if (rowIsGap)
{
part1TotalDistanceFromSeenGalaxies += seenGalaxies * 2;
part2TotalDistanceFromSeenGalaxies += seenGalaxies * 1000000;
}
else
{
part1TotalDistanceFromSeenGalaxies += seenGalaxies;
part2TotalDistanceFromSeenGalaxies += seenGalaxies;
}
input = input.Slice(rowLen);
}
seenGalaxies = 0;
part1TotalDistanceFromSeenGalaxies = 0;
part2TotalDistanceFromSeenGalaxies = 0;
foreach (int n in galaxiesPerCol)
{
if (n == 0)
{
part1TotalDistanceFromSeenGalaxies += seenGalaxies * 2;
part2TotalDistanceFromSeenGalaxies += seenGalaxies * 1000000;
}
else
{
part1 += n * part1TotalDistanceFromSeenGalaxies;
part2 += n * part2TotalDistanceFromSeenGalaxies;
seenGalaxies += n;
part1TotalDistanceFromSeenGalaxies += seenGalaxies;
part2TotalDistanceFromSeenGalaxies += seenGalaxies;
}
}
solution.SubmitPart1(part1);
solution.SubmitPart2(part2);
}
}