generated from mazharenko/aoc-agent-template-multipleyears
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay22.cs
67 lines (59 loc) · 1.41 KB
/
Day22.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using MoreLinq;
namespace aoc.Year2024;
internal partial class Day22
{
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);
}
}
}