Skip to content

Commit

Permalink
day 22
Browse files Browse the repository at this point in the history
  • Loading branch information
mazharenko committed Dec 22, 2024
1 parent 1776c69 commit 2100777
Show file tree
Hide file tree
Showing 3 changed files with 2,484 additions and 15 deletions.
77 changes: 62 additions & 15 deletions src/aoc/Year2024/Day22.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,67 @@
using MoreLinq;

namespace aoc.Year2024;

internal partial class Day22
{
internal partial class Part1
{
public string Solve(string input)
{
throw new NotImplementedException();
}
}

internal partial class Part2
{
public string Solve(string input)
{
throw new NotImplementedException();
}
}
public long[] Parse(string input)
{
return Numerics.IntegerInt64.Lines().Parse(input);
}

private static IEnumerable<long> SecretSeq(long init)
{
return MoreEnumerable.Generate(init, current =>
{
current = ((current * 64) ^ current) % 16777216;
current = ((current / 32) ^ current) % 16777216;
current = ((current * 2048) ^ current) % 16777216;
return current;
});
}

internal partial class Part1
{
private readonly Example example = new(
"""
1
10
100
2024
""", 37327623);

public long Solve(long[] input)
{
return input.Select(n => SecretSeq(n).ElementAt(2000)).Sum();
}
}

internal partial class Part2
{
private readonly Example example = new(
"""
1
2
3
2024
""", 23L);

public long Solve(long[] input)
{
return input.Select(buyer => SecretSeq(buyer).Take(2001))
.SelectMany(CollectPricesAndChanges)
.GroupBy(x => x.changeSeq, x => x.price, (_, prices) => prices.Sum())
.Max();
}

private static IEnumerable<((long, long, long, long) changeSeq, long price)> CollectPricesAndChanges(IEnumerable<long> secret)
{
return secret.Select(x => x % 10)
.Window(5)
.Select(window => (
changeSeq: (window[1] - window[0], window[2] - window[1], window[3] - window[2], window[4] - window[3]),
price: window[4]
)).DistinctBy(x => x.changeSeq);
}
}
}
2 changes: 2 additions & 0 deletions tests/AoC.Tests/InputTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,7 @@ private static IEnumerable<PartInputCaseData> GetCases()
yield return new PartInputCaseData(20, 2, "1011325");
yield return new PartInputCaseData(21, 1, "169390");
yield return new PartInputCaseData(21, 2, "210686850124870");
yield return new PartInputCaseData(22, 1, "20401393616");
yield return new PartInputCaseData(22, 2, "2272");
}
}
Loading

0 comments on commit 2100777

Please sign in to comment.