diff --git a/2024/Day01.cs b/2024/Day01.cs index 3783746..2c6bf26 100644 --- a/2024/Day01.cs +++ b/2024/Day01.cs @@ -1,10 +1,34 @@ namespace _2024; -public class Day01 : ISolution -{ - public void Run() - { - var _ = File.ReadAllText("input/01"); - Console.WriteLine("The solution for day 01 is not implmented yet"); +public class Day01 : ISolution { + public void Run() { + // Read all lines from the input file + var lines = File.ReadAllLines("input/01"); + + // Two lists to hold the first and second integers per line + var left = new List(); + var right = new List(); + + foreach (var line in lines) { + if (string.IsNullOrWhiteSpace(line)) continue; + + var parts = line.Split(' ', StringSplitOptions.RemoveEmptyEntries); + if (parts.Length < 2) { + Console.WriteLine("Bad parse"); + return; + } + + if (int.TryParse(parts[0], out var a) && int.TryParse(parts[1], out var b)) { + left.Add(a); + right.Add(b); + } + } + + var leftSorted = left.OrderBy(x => x).ToList(); + var rightSorted = right.OrderBy(x => x).ToList(); + Console.WriteLine(leftSorted.Select((t, i) => Math.Abs(t - rightSorted[i])).Sum()); + + Console.WriteLine(left.Sum(i => right.Count(x => x == i) * i)); } } + diff --git a/2024/Day02.cs b/2024/Day02.cs index 2d795b2..aad907a 100644 --- a/2024/Day02.cs +++ b/2024/Day02.cs @@ -4,7 +4,21 @@ public class Day02 : ISolution { public void Run() { - var _ = File.ReadAllText("input/02"); - Console.WriteLine("The solution for day 02 is not implmented yet"); + var input = File.ReadAllText("input/02") + .Split('\n') + .Select(line => line.Split(' ') + .Select(int.Parse) + .ToList()) + .ToList(); + + Console.WriteLine(PartOne(input)); + } + + private static int PartOne(List> list) { + return list.Select(line => Enumerable.Range(1, Math.Max(0, line.Count - 1)) + .Select(i => line[i] - line[i - 1]) + .ToList()) + .Where(diffs => diffs.All(d => d > 0) || diffs.All(d => d < 0)) + .Count(diffs => !diffs.Any(d => Math.Abs(d) is < 1 or > 3)); } } diff --git a/README.md b/README.md index 737b906..e606b6a 100644 --- a/README.md +++ b/README.md @@ -3,13 +3,23 @@ Advent Of Code solutions using a different language each year 2015: C99 + 2016: Go + 2017: TBD + 2018: TBD + 2019: TBD + 2020: TBD + 2021: TBD + 2022: TBD + 2023: TBD + 2024: C# + 2025: TBD