Day 1
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
40
AdventOfCode/Problems/AOC2024/Day1/HistorianHysteria.cs
Normal file
40
AdventOfCode/Problems/AOC2024/Day1/HistorianHysteria.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using AdventOfCode.Runner.Attributes;
|
||||
|
||||
using System;
|
||||
using System.Collections.Frozen;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AdventOfCode.Problems.AOC2024.Day1;
|
||||
[ProblemInfo(2024, 1, "Historian Hysteria")]
|
||||
internal class HistorianHysteria : Problem<int, int>
|
||||
{
|
||||
private int[] _left = [];
|
||||
private int[] _right = [];
|
||||
|
||||
public override void CalculatePart1()
|
||||
{
|
||||
Part1 = _left.Order()
|
||||
.Zip(_right.Order())
|
||||
.Select(a => Math.Abs(a.First - a.Second))
|
||||
.Sum();
|
||||
}
|
||||
|
||||
public override void CalculatePart2()
|
||||
{
|
||||
var rightFeq = _right.GroupBy(x => x)
|
||||
.ToFrozenDictionary(g => g.Key, g => g.Count());
|
||||
Part2 = _left.Select(x => rightFeq.TryGetValue(x, out var f) ? f * x : 0)
|
||||
.Sum();
|
||||
}
|
||||
|
||||
public override void LoadInput()
|
||||
{
|
||||
var lines = ReadInputLines("input.txt");
|
||||
var data = lines.Select(l => l.Split(' ').Select(int.Parse)).ToList();
|
||||
_left = data.Select(l => l.First()).ToArray();
|
||||
_right = data.Select(l => l.Last()).ToArray();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user