Skip to content

Commit

Permalink
Day 18
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Jan 17, 2024
1 parent 65fea59 commit e0b125e
Show file tree
Hide file tree
Showing 11 changed files with 1,387 additions and 0 deletions.
12 changes: 12 additions & 0 deletions AdventOfCode.sln
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Day17_1", "Day17_1\Day17_1.
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Day17_2", "Day17_2\Day17_2.csproj", "{BD5D483C-4BA9-4A28-9551-A4ACBE6F541C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Day18_1", "Day18_1\Day18_1.csproj", "{CC908664-AA4D-4AB7-AE26-32A839475B9C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Day18_2", "Day18_2\Day18_2.csproj", "{654BDF14-2082-4DE7-94ED-069945B0D292}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -219,6 +223,14 @@ Global
{BD5D483C-4BA9-4A28-9551-A4ACBE6F541C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BD5D483C-4BA9-4A28-9551-A4ACBE6F541C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BD5D483C-4BA9-4A28-9551-A4ACBE6F541C}.Release|Any CPU.Build.0 = Release|Any CPU
{CC908664-AA4D-4AB7-AE26-32A839475B9C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CC908664-AA4D-4AB7-AE26-32A839475B9C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CC908664-AA4D-4AB7-AE26-32A839475B9C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CC908664-AA4D-4AB7-AE26-32A839475B9C}.Release|Any CPU.Build.0 = Release|Any CPU
{654BDF14-2082-4DE7-94ED-069945B0D292}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{654BDF14-2082-4DE7-94ED-069945B0D292}.Debug|Any CPU.Build.0 = Debug|Any CPU
{654BDF14-2082-4DE7-94ED-069945B0D292}.Release|Any CPU.ActiveCfg = Release|Any CPU
{654BDF14-2082-4DE7-94ED-069945B0D292}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
24 changes: 24 additions & 0 deletions Day18_1/Day18_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>
50 changes: 50 additions & 0 deletions Day18_1/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Tools;

var input = File.ReadAllLines("input.txt");
var origin = new Point();
var currentPosition = origin;
var points = new List<Point>();
var totalBoundaryPoints = 0L;
foreach (var line in input)
{
var parts = line.Split(' ');
var direction = parts[0];
var distance = int.Parse(parts[1]);
Point newPosition = currentPosition;
switch (direction)
{
case "R":
newPosition = new Point(currentPosition.X + distance, currentPosition.Y);
break;
case "L":
newPosition = new Point(currentPosition.X - distance, currentPosition.Y);
break;
case "U":
newPosition = new Point(currentPosition.X, currentPosition.Y - distance);
break;
case "D":
newPosition = new Point(currentPosition.X, currentPosition.Y + distance);
break;
default:
throw new InvalidOperationException();
}
points.Add(newPosition);
totalBoundaryPoints += Math.Abs(newPosition.X - currentPosition.X) + Math.Abs(newPosition.Y - currentPosition.Y);
currentPosition = newPosition;
}

// Calculate area using Shoelace formula
long area = 0;
for (var currentPoint = 0; currentPoint < points.Count - 1; currentPoint++)
{
var nextPoint = currentPoint + 1 == points.Count ? 0 : currentPoint + 1;
area += points[currentPoint].X * points[nextPoint].Y - points[nextPoint].X * points[currentPoint].Y;
}
area = Math.Abs(area) / 2;

// Use Pick's theorem to calculate the number of points inside the polygon
var pointsInside = area - totalBoundaryPoints / 2 + 1;
Console.WriteLine($"Points inside: {pointsInside}");
Console.WriteLine($"Area: {area}");

Console.WriteLine("Total points: " + (totalBoundaryPoints + pointsInside));
7 changes: 7 additions & 0 deletions Day18_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 e0b125e

Please sign in to comment.