Skip to content

Commit

Permalink
Day 11 (Part 1)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Dec 16, 2023
1 parent 39e3c93 commit 7a2e661
Show file tree
Hide file tree
Showing 6 changed files with 265 additions and 0 deletions.
12 changes: 12 additions & 0 deletions AdventOfCode.sln
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Day10_1", "Day10_1\Day10_1.
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Day10_2", "Day10_2\Day10_2.csproj", "{4432C20F-98FA-455C-8795-2D604FCDBB2D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Day11_1", "Day11_1\Day11_1.csproj", "{F1B4F862-45E3-4F78-95A3-12616E35C0A9}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Day11_2", "Day11_2\Day11_2.csproj", "{7FFF2C27-BF66-4494-ABE3-65A03B65E299}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -135,6 +139,14 @@ Global
{4432C20F-98FA-455C-8795-2D604FCDBB2D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4432C20F-98FA-455C-8795-2D604FCDBB2D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4432C20F-98FA-455C-8795-2D604FCDBB2D}.Release|Any CPU.Build.0 = Release|Any CPU
{F1B4F862-45E3-4F78-95A3-12616E35C0A9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F1B4F862-45E3-4F78-95A3-12616E35C0A9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F1B4F862-45E3-4F78-95A3-12616E35C0A9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F1B4F862-45E3-4F78-95A3-12616E35C0A9}.Release|Any CPU.Build.0 = Release|Any CPU
{7FFF2C27-BF66-4494-ABE3-65A03B65E299}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7FFF2C27-BF66-4494-ABE3-65A03B65E299}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7FFF2C27-BF66-4494-ABE3-65A03B65E299}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7FFF2C27-BF66-4494-ABE3-65A03B65E299}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
24 changes: 24 additions & 0 deletions Day11_1/Day11_1.csproj
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>
82 changes: 82 additions & 0 deletions Day11_1/Program.cs
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);
7 changes: 7 additions & 0 deletions Day11_1/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"profiles": {
"Run": {
"commandName": "Project"
}
}
}
Loading

0 comments on commit 7a2e661

Please sign in to comment.