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
65fea59
commit e0b125e
Showing
11 changed files
with
1,387 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,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)); |
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.