2024-01,02
This commit is contained in:
@@ -1,10 +1,34 @@
|
|||||||
namespace _2024;
|
namespace _2024;
|
||||||
|
|
||||||
public class Day01 : ISolution
|
public class Day01 : ISolution {
|
||||||
{
|
public void Run() {
|
||||||
public void Run()
|
// Read all lines from the input file
|
||||||
{
|
var lines = File.ReadAllLines("input/01");
|
||||||
var _ = File.ReadAllText("input/01");
|
|
||||||
Console.WriteLine("The solution for day 01 is not implmented yet");
|
// Two lists to hold the first and second integers per line
|
||||||
|
var left = new List<int>();
|
||||||
|
var right = new List<int>();
|
||||||
|
|
||||||
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,21 @@ public class Day02 : ISolution
|
|||||||
{
|
{
|
||||||
public void Run()
|
public void Run()
|
||||||
{
|
{
|
||||||
var _ = File.ReadAllText("input/02");
|
var input = File.ReadAllText("input/02")
|
||||||
Console.WriteLine("The solution for day 02 is not implmented yet");
|
.Split('\n')
|
||||||
|
.Select(line => line.Split(' ')
|
||||||
|
.Select(int.Parse)
|
||||||
|
.ToList())
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
Console.WriteLine(PartOne(input));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int PartOne(List<List<int>> 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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
10
README.md
10
README.md
@@ -3,13 +3,23 @@
|
|||||||
Advent Of Code solutions using a different language each year
|
Advent Of Code solutions using a different language each year
|
||||||
|
|
||||||
2015: C99
|
2015: C99
|
||||||
|
|
||||||
2016: Go
|
2016: Go
|
||||||
|
|
||||||
2017: TBD
|
2017: TBD
|
||||||
|
|
||||||
2018: TBD
|
2018: TBD
|
||||||
|
|
||||||
2019: TBD
|
2019: TBD
|
||||||
|
|
||||||
2020: TBD
|
2020: TBD
|
||||||
|
|
||||||
2021: TBD
|
2021: TBD
|
||||||
|
|
||||||
2022: TBD
|
2022: TBD
|
||||||
|
|
||||||
2023: TBD
|
2023: TBD
|
||||||
|
|
||||||
2024: C#
|
2024: C#
|
||||||
|
|
||||||
2025: TBD
|
2025: TBD
|
||||||
|
|||||||
Reference in New Issue
Block a user