Skip to content

Commit 03e82f2

Browse files
committed
solve 22nd day 2nd task
1 parent 0d6eb85 commit 03e82f2

File tree

1 file changed

+28
-0
lines changed
  • year2024/src/main/kotlin/net/olegg/aoc/year2024/day22

1 file changed

+28
-0
lines changed

year2024/src/main/kotlin/net/olegg/aoc/year2024/day22/Day22.kt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,34 @@ object Day22 : DayOf2024(22) {
2222
.first()
2323
}
2424
}
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+
}
2553
}
2654

2755
fun main() = SomeDay.mainify(Day22)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy