File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
year2024/src/main/kotlin/net/olegg/aoc/year2024/day22 Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,34 @@ object Day22 : DayOf2024(22) {
22
22
.first()
23
23
}
24
24
}
25
+
26
+ override fun second (): Any? {
27
+ val prices = lines
28
+ .map { it.toLong() }
29
+ .map { number ->
30
+ val nums = generateSequence(number) { curr ->
31
+ val first = (curr xor (curr shl 6 )) and modulo
32
+ val second = (first xor (first shr 5 )) and modulo
33
+ val third = (second xor (second shl 11 )) and modulo
34
+ third
35
+ }
36
+ .map { curr -> curr % 10 }
37
+ .take(2001 )
38
+ .toList()
39
+
40
+ val diffs = nums.zipWithNext { a, b -> b - a }
41
+
42
+ val pairs = diffs.windowed(4 ).asReversed().zip(nums.asReversed()).reversed()
43
+
44
+ pairs.groupBy { it.first }.mapValues { it.value.first().second }
45
+ }
46
+
47
+ val allDiffs = prices.flatMap { currPrices -> currPrices.keys }.toSet()
48
+
49
+ return allDiffs.maxOf { diff ->
50
+ prices.sumOf { currPrices -> (currPrices[diff] ? : 0 ) }
51
+ }
52
+ }
25
53
}
26
54
27
55
fun main () = SomeDay .mainify(Day22 )
You can’t perform that action at this time.
0 commit comments