Day 11, Part 2 done
This commit is contained in:
parent
88ce597c46
commit
323bd730ce
5 changed files with 96 additions and 16 deletions
6
.editorconfig
Normal file
6
.editorconfig
Normal file
|
@ -0,0 +1,6 @@
|
|||
|
||||
[*.cs]
|
||||
|
||||
# ReSharper properties
|
||||
resharper_local_function_body = expression_body
|
||||
resharper_nested_ternary_style = compact
|
|
@ -8,32 +8,31 @@ while (Console.ReadLine().AsMemory() is { IsEmpty: false } line)
|
|||
lines.Add(line);
|
||||
}
|
||||
|
||||
|
||||
var horizontalLength = lines.Count;
|
||||
var verticalLength = lines[0].Length;
|
||||
var rowCount = lines.Count;
|
||||
var colCount = lines[0].Length;
|
||||
List<List<char>> expandedGrid = [];
|
||||
for (var y = 0; y < verticalLength; y++)
|
||||
for (var x = 0; x < colCount; x++)
|
||||
{
|
||||
var onlyDots = true;
|
||||
for (var x = 0; x < horizontalLength; x++)
|
||||
var empty = true;
|
||||
for (var y = 0; y < rowCount; y++)
|
||||
{
|
||||
var cell = lines[x].Span[y];
|
||||
if (y != 0) expandedGrid[x].Add(cell);
|
||||
var cell = lines[y].Span[x];
|
||||
if (x != 0) expandedGrid[y].Add(cell);
|
||||
else expandedGrid.Add([cell]);
|
||||
|
||||
if (cell == '#') onlyDots = false;
|
||||
if (cell == '#') empty = false;
|
||||
}
|
||||
|
||||
if (onlyDots)
|
||||
for (var x = 0; x < lines.Count; x++)
|
||||
expandedGrid[x].Add('.');
|
||||
if (empty)
|
||||
for (var y = 0; y < lines.Count; y++)
|
||||
expandedGrid[y].Add('.');
|
||||
}
|
||||
|
||||
List<(int X, int Y)> galaxyCoords = [];
|
||||
horizontalLength = expandedGrid.Count;
|
||||
verticalLength = expandedGrid[0].Count;
|
||||
for (var x = 0; x < horizontalLength; x++)
|
||||
for (var y = 0; y < verticalLength; y++)
|
||||
rowCount = expandedGrid.Count;
|
||||
colCount = expandedGrid[0].Count;
|
||||
for (var x = 0; x < rowCount; x++)
|
||||
for (var y = 0; y < colCount; y++)
|
||||
if (expandedGrid[x][y] == '#')
|
||||
galaxyCoords.Add((x, y));
|
||||
|
||||
|
|
10
Day11/Day11.Part2/Day11.Part2.csproj
Normal file
10
Day11/Day11.Part2/Day11.Part2.csproj
Normal file
|
@ -0,0 +1,10 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
59
Day11/Day11.Part2/Program.cs
Normal file
59
Day11/Day11.Part2/Program.cs
Normal file
|
@ -0,0 +1,59 @@
|
|||
using System.Runtime.CompilerServices;
|
||||
|
||||
var grid = new List<ReadOnlyMemory<char>>();
|
||||
while (Console.ReadLine().AsMemory() is { IsEmpty: false } line)
|
||||
grid.Add(line);
|
||||
|
||||
var emptyRows = new List<int>(new int[grid.Count]);
|
||||
var emptyColumns = new List<int>(new int[grid[0].Length]);
|
||||
|
||||
for (var i = 0; i < grid.Count; i++)
|
||||
if (grid[i].Span.IndexOf('#') == -1)
|
||||
emptyRows[i] = 1;
|
||||
|
||||
for (var i = 0; i < grid[0].Length; i++)
|
||||
if (grid.All(t => t.Span[i] == '.'))
|
||||
emptyColumns[i] = 1;
|
||||
|
||||
var distanceRows = new List<int>(new int[grid.Count]);
|
||||
var distanceColumns = new List<int>(new int[grid[0].Length]);
|
||||
InitializeDistanceMap(emptyRows, distanceRows);
|
||||
InitializeDistanceMap(emptyColumns, distanceColumns);
|
||||
|
||||
var galaxies = new List<(int, int)>();
|
||||
|
||||
for (var y = 0; y < grid.Count; ++y)
|
||||
for (var x = 0; x < grid[y].Length; ++x)
|
||||
if (grid[y].Span[x] == '#')
|
||||
galaxies.Add((y, x));
|
||||
|
||||
long sum = 0;
|
||||
|
||||
for (var y = 0; y < galaxies.Count; ++y)
|
||||
{
|
||||
var (y1, x1) = galaxies[y];
|
||||
|
||||
for (var x = y + 1; x < galaxies.Count; ++x)
|
||||
{
|
||||
var (y2, x2) = galaxies[x];
|
||||
sum += Math.Abs(y1 - y2) + Math.Abs(x1 - x2);
|
||||
sum += GetDistanceBetweenPoints(distanceRows, y1, y2) * 999999L;
|
||||
sum += GetDistanceBetweenPoints(distanceColumns, x1, x2) * 999999L;
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine(sum);
|
||||
|
||||
return;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
static int GetDistanceBetweenPoints(IReadOnlyList<int> prefixSum, int y, int x) =>
|
||||
y > x ? x == 0 ? prefixSum[y] : prefixSum[y] - prefixSum[x - 1] :
|
||||
y == 0 ? prefixSum[x] : prefixSum[x] - prefixSum[y - 1];
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
static void InitializeDistanceMap(IReadOnlyList<int> arr, IList<int> prefixSum)
|
||||
{
|
||||
prefixSum[0] = arr[0];
|
||||
for (var i = 1; i < arr.Count; ++i) prefixSum[i] = prefixSum[i - 1] + arr[i];
|
||||
}
|
|
@ -42,6 +42,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day10.Part2", "Day10\Day10.
|
|||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day11.Part1", "Day11\Day11.Part1\Day11.Part1.csproj", "{9B8BBF95-E459-418F-A534-3484BA1CD8FF}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day11.Part2", "Day11\Day11.Part2\Day11.Part2.csproj", "{F7799ADA-363C-4E5C-AE20-03ED8B54950D}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
@ -132,5 +134,9 @@ Global
|
|||
{9B8BBF95-E459-418F-A534-3484BA1CD8FF}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{9B8BBF95-E459-418F-A534-3484BA1CD8FF}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{9B8BBF95-E459-418F-A534-3484BA1CD8FF}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{F7799ADA-363C-4E5C-AE20-03ED8B54950D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{F7799ADA-363C-4E5C-AE20-03ED8B54950D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{F7799ADA-363C-4E5C-AE20-03ED8B54950D}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{F7799ADA-363C-4E5C-AE20-03ED8B54950D}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
|
Loading…
Reference in a new issue