-
Notifications
You must be signed in to change notification settings - Fork 3
/
Day20.scala
25 lines (21 loc) · 967 Bytes
/
Day20.scala
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
package AdventOfCode2022
object Day20:
def decrypt(input: Seq[Int], key: Long, rounds: Int): Long =
val mixed = collection.mutable.ArrayBuffer.from(input.map(_ * key).zipWithIndex)
for _ <- 1 to rounds do
for index <- input.indices do
val from = mixed.indexWhere(_._2 == index)
val pair @ (number, _) = mixed.remove(from)
val remainder = (number % mixed.size).toInt
val to = (from + remainder + mixed.size) % mixed.size
mixed.insert(to, pair)
end for
end for
val start = mixed.indexWhere(_._1 == 0)
(1 to 3).map(offset => mixed((start + 1000 * offset) % mixed.size)._1).sum
def part1(input: Seq[Int]): Long = decrypt(input, 1L, 1)
def part2(input: Seq[Int]): Long = decrypt(input, 811589153L, 10)
def main(args: Array[String]): Unit =
val data = io.Source.fromResource("AdventOfCode2022/Day20.txt").getLines().map(_.toInt).toSeq
println(part1(data))
println(part2(data))