advent-of-code-2024/day 1/Program.cs
2024-12-01 18:08:02 +00:00

64 lines
1.5 KiB
C#

namespace day_1
{
internal class Program
{
static void Main(string[] args)
{
string[] flines = File.ReadAllLines(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data.txt"));
List<int> la = new List<int>(),
lb = new List<int>();
Dictionary<int, int> similarity = new Dictionary<int, int>();
int diff = 0;
int similar = 0;
for (int i = 0; i < flines.Length; i++)
{
string a, b;
a = flines[i].Split(" ")[0];
b = flines[i].Split(" ")[1];
la.Add(Convert.ToInt32(a));
lb.Add(Convert.ToInt32(b));
}
la.Sort();
lb.Sort();
for (int i = 0; i < la.Count; i++)
{
int d = la[i] - lb[i];
if (d < 0)
d = -d;
diff += d;
}
for (int x = 0; x < la.Count; x++)
{
int v = la[x];
int r = 0;
for (int y = 0; y < lb.Count && lb[y] <= v; y++)
{
if (v == lb[y])
r++;
}
similarity[v] = r;
}
foreach (KeyValuePair<int, int> sim in similarity)
{
similar += sim.Key * sim.Value;
}
Console.WriteLine($"distance: {diff}\nsimilarity: {similar}");
}
}
}