Initial Commit
This commit is contained in:
14
2024/Day01CSharp/Day01CSharp.csproj
Normal file
14
2024/Day01CSharp/Day01CSharp.csproj
Normal file
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\AdventCommon\AdventCommon.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
97
2024/Day01CSharp/Program.cs
Normal file
97
2024/Day01CSharp/Program.cs
Normal file
@@ -0,0 +1,97 @@
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
using AdventCommon;
|
||||
using System.Security.AccessControl;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
|
||||
/** P1 **/
|
||||
var p1exampleSln = Part01(new Day01Parsed("example-input.txt"));
|
||||
var p1puzzleSln = Part01(new Day01Parsed("puzzle-input.txt"));
|
||||
|
||||
Console.WriteLine("Part01 Total Distance: " + p1exampleSln);
|
||||
Console.WriteLine("Part01 Total Distance: " + p1puzzleSln);
|
||||
|
||||
/** P2 **/
|
||||
var p2exampleSln = Part02(new Day01Parsed("example-input.txt"));
|
||||
var p2puzzleSln = Part02(new Day01Parsed("puzzle-input.txt"));
|
||||
|
||||
Console.WriteLine("Part02 Example Sum: " + p2exampleSln);
|
||||
Console.WriteLine("Part02 Puzzle Sum: " + p2puzzleSln);
|
||||
|
||||
int GetDistance(int l, int r)
|
||||
{
|
||||
return Math.Abs(l - r);
|
||||
}
|
||||
|
||||
int Part01(Day01Parsed input)
|
||||
{
|
||||
int distance = 0;
|
||||
input.left.Sort();
|
||||
input.right.Sort();
|
||||
|
||||
for (int i = 0; i < input.left.Count; i++)
|
||||
{
|
||||
distance += GetDistance(input.left[i], input.right[i]);
|
||||
}
|
||||
|
||||
return distance;
|
||||
}
|
||||
|
||||
int Part02(Day01Parsed input)
|
||||
{
|
||||
int total = 0;
|
||||
|
||||
for (int i = 0; i < input.left.Count; i++)
|
||||
{
|
||||
var l = input.left[i];
|
||||
total += l * input.GetSimilarity(l);
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
class Day01Parsed : AdventCommon.ParsedInput
|
||||
{
|
||||
public List<int> left = new List<int>();
|
||||
public List<int> right = new List<int>();
|
||||
|
||||
public int GetSimilarity(int value)
|
||||
{
|
||||
int instances = 0;
|
||||
|
||||
foreach (var item in right)
|
||||
{
|
||||
if (item == value)
|
||||
{
|
||||
instances++;
|
||||
}
|
||||
}
|
||||
|
||||
return instances;
|
||||
}
|
||||
|
||||
public Day01Parsed(string fileName) : base(fileName)
|
||||
{
|
||||
if (left.Count != right.Count)
|
||||
{
|
||||
throw new Exception("Improperly formatted input file");
|
||||
}
|
||||
}
|
||||
|
||||
public override bool ParseLine(string line, object? context = null)
|
||||
{
|
||||
var split = line.Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
if (split.Length != 2)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var l = Int32.Parse(split[0]);
|
||||
var r = Int32.Parse(split[1]);
|
||||
|
||||
left.Add(l);
|
||||
right.Add(r);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
8
2024/Day01CSharp/Properties/launchSettings.json
Normal file
8
2024/Day01CSharp/Properties/launchSettings.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"profiles": {
|
||||
"Day01CSharp": {
|
||||
"commandName": "Project",
|
||||
"workingDirectory": "C:\\dev\\dev-drive\\DevSandbox\\AdventOfCode\\2024\\Day01CSharp"
|
||||
}
|
||||
}
|
||||
}
|
||||
6
2024/Day01CSharp/example-input.txt
Normal file
6
2024/Day01CSharp/example-input.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3
|
||||
1000
2024/Day01CSharp/puzzle-input.txt
Normal file
1000
2024/Day01CSharp/puzzle-input.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user