generated from mazharenko/aoc-agent-template-multipleyears
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1776c69
commit 2100777
Showing
3 changed files
with
2,484 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.