97 lines
2.2 KiB
C#
97 lines
2.2 KiB
C#
// 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;
|
|
}
|
|
} |