Skip to content

Commit

Permalink
day 1
Browse files Browse the repository at this point in the history
  • Loading branch information
mazharenko committed Dec 1, 2024
1 parent 84462eb commit 5e7ae3a
Show file tree
Hide file tree
Showing 4 changed files with 1,072 additions and 16 deletions.
80 changes: 65 additions & 15 deletions src/aoc/Year2024/Day01/Day01.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,68 @@ namespace aoc.Year2024.Day01;

internal partial class Day01
{
internal partial class Part1
{
public string Solve(string input)
{
throw new NotImplementedException();
}
}
internal partial class Part2
{
public string Solve(string input)
{
throw new NotImplementedException();
}
}
}
private static (int[] first, int[] second) ParseBase(string input)
{
var parser =
Numerics.IntegerInt32.ThenIgnore(SpanX.Space)
.Then(Numerics.IntegerInt32)
.Lines()
.Select(lines => (
lines.Select(x => x.Item1).ToArray(),
lines.Select(x => x.Item2).ToArray())
);

return parser.Parse(input);
}

internal partial class Part1
{
private readonly Example example = new(
"""
3 4
4 3
2 5
1 3
3 9
3 3
""", 11);

public (int[] first, int[] second) Parse(string input) => ParseBase(input);

public int Solve((int[] first, int[] second) input)
{
var totalDifference =
input.first.Order()
.Zip(input.second.Order(), Distance).Sum();

return totalDifference;

int Distance(int x, int y) => Math.Abs(x - y);
}
}

internal partial class Part2
{
private readonly Example example = new(
"""
3 4
4 3
2 5
1 3
3 9
3 3
""", 31);


public (int[] first, int[] second) Parse(string input) => ParseBase(input);

public int Solve((int[] first, int[] second) input)
{
var secondGroups = input.second.ToLookup(x => x);

var similarityScore = input.first.Select(x => x * secondGroups[x].Count()).Sum();

return similarityScore;
}
}
}
5 changes: 5 additions & 0 deletions tests/AoC.Tests/AoC.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
<ItemGroup>
<ProjectReference Include="..\..\src\aoc\aoc.csproj"/>
</ItemGroup>
<ItemGroup>
<None Update="inputs\*.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<Using Include="NUnit.Framework"/>
</ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion tests/AoC.Tests/InputTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ internal partial class InputTests
{
private static IEnumerable<PartInputCaseData> GetCases()
{
yield break;
yield return new PartInputCaseData(1, 1, "1889772");
yield return new PartInputCaseData(1, 2, "23228917");
}
}
Loading

0 comments on commit 5e7ae3a

Please sign in to comment.