generated from MartinZikmund/template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
39e3c93
commit 7a2e661
Showing
6 changed files
with
265 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Remove="input.txt" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Content Include="input.txt"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</Content> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Tools\Tools.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using System.Drawing; | ||
|
||
var input = File.ReadAllLines("input.txt"); | ||
|
||
var width = input[0].Length; | ||
var height = input.Length; | ||
var map = new char[width, height]; | ||
var gallaxies = new List<Point>(); | ||
|
||
for (int x = 0; x < input[0].Length; x++) | ||
{ | ||
for (int y = 0; y < input.Length; y++) | ||
{ | ||
map[x, y] = input[y][x]; | ||
if (map[x,y] == '#') | ||
{ | ||
gallaxies.Add(new Point(x, y)); | ||
} | ||
} | ||
} | ||
|
||
var emptyRows = new List<int>(); | ||
var emptyColumns = new List<int>(); | ||
|
||
for (int column = 0; column < width; column++) | ||
{ | ||
var isEmpty = true; | ||
for (int row = 0; row < height; row++) | ||
{ | ||
if (map[column, row] != '.') | ||
{ | ||
isEmpty = false; | ||
break; | ||
} | ||
} | ||
|
||
if (isEmpty) | ||
{ | ||
emptyColumns.Add(column); | ||
} | ||
} | ||
|
||
for (int row = 0; row < height; row++) | ||
{ | ||
var isEmpty = true; | ||
for (int column = 0; column < width; column++) | ||
{ | ||
if (map[column, row] != '.') | ||
{ | ||
isEmpty = false; | ||
break; | ||
} | ||
} | ||
|
||
if (isEmpty) | ||
{ | ||
emptyRows.Add(row); | ||
} | ||
} | ||
|
||
long totalDistance = 0; | ||
foreach (var galaxy in gallaxies) | ||
{ | ||
foreach (var otherGalaxy in gallaxies) | ||
{ | ||
if (galaxy == otherGalaxy) | ||
{ | ||
continue; | ||
} | ||
|
||
var distance = Math.Abs(galaxy.X - otherGalaxy.X) + Math.Abs(galaxy.Y - otherGalaxy.Y); | ||
|
||
// Number of empty rows/columns between the two galaxies | ||
var emptyRowsBetween = emptyRows.Count(row => Math.Min(galaxy.Y, otherGalaxy.Y) < row && row < Math.Max(galaxy.Y, otherGalaxy.Y)); | ||
var emptyColumnsBetween = emptyColumns.Count(column => Math.Min(galaxy.X, otherGalaxy.X) < column && column < Math.Max(galaxy.X, otherGalaxy.X)); | ||
|
||
distance += emptyRowsBetween + emptyColumnsBetween; | ||
totalDistance += distance; | ||
} | ||
} | ||
|
||
Console.WriteLine(totalDistance / 2); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"profiles": { | ||
"Run": { | ||
"commandName": "Project" | ||
} | ||
} | ||
} |
Oops, something went wrong.