Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e9894ae

Browse files
Simplify
1 parent 8054846 commit e9894ae

File tree

2 files changed

+13
-32
lines changed

2 files changed

+13
-32
lines changed

‎src/main/scala/AdventOfCode2022/Day20.scala

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,21 @@
11
package AdventOfCode2022
22

33
object Day20:
4-
case class Node(value: Long, var prev: Node, var next: Node)
5-
6-
def parse(input: Seq[Int], key: Long): Seq[Node] =
7-
val nodes = input.map(n => Node(n * key, null, null))
8-
nodes.zipWithIndex.foreach { (node, i) =>
9-
node.prev = nodes((i - 1 + nodes.size) % nodes.size)
10-
node.next = nodes((i + 1) % nodes.size)
11-
}
12-
nodes
13-
14-
def mix(nodes: Seq[Node]): Unit =
15-
for node <- nodes do
16-
val remainder = (node.value % (nodes.size - 1)).toInt
17-
val move = if remainder >= 0 then remainder else remainder + nodes.size - 1
18-
for _ <- 1 to move do
19-
val (a, b, c, d) = (node.prev, node, node.next, node.next.next)
20-
a.next = c
21-
b.prev = c
22-
b.next = d
23-
c.prev = a
24-
c.next = b
25-
d.prev = b
4+
def decrypt(input: Seq[Int], key: Long, rounds: Int): Long =
5+
val mixed = collection.mutable.ArrayBuffer.from(input.map(_ * key).zipWithIndex)
6+
for _ <- 1 to rounds do
7+
for index <- input.indices do
8+
val from = mixed.indexWhere(_._2 == index)
9+
val pair @ (number, _) = mixed.remove(from)
10+
val remainder = (number % mixed.size).toInt
11+
val to = (from + remainder + mixed.size) % mixed.size
12+
mixed.insert(to, pair)
2613
end for
2714
end for
15+
val start = mixed.indexWhere(_._1 == 0)
16+
(1 to 3).map(offset => mixed((start + 1000 * offset) % mixed.size)._1).sum
2817

29-
def skip(start: Node): Node = Iterator.iterate(start)(_.next).drop(1000).next()
30-
31-
def decrypt(input: Seq[Int], key: Long, rounds: Int): Long =
32-
val nodes = parse(input, key)
33-
for _ <- 1 to rounds do mix(nodes)
34-
val start = nodes.find(_.value == 0).get
35-
Iterator.iterate(start)(skip).drop(1).take(3).map(_.value).sum
36-
37-
def part1(input: Seq[Int]): Long = decrypt(input, 1, 1)
18+
def part1(input: Seq[Int]): Long = decrypt(input, 1L, 1)
3819

3920
def part2(input: Seq[Int]): Long = decrypt(input, 811589153L, 10)
4021

‎src/test/scala/AdventOfCode2022/Day20Suite.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package AdventOfCode2022
33
import org.scalatest.funsuite.AnyFunSuite
44

55
class Day20Suite extends AnyFunSuite:
6-
val sample = Seq(1, 2, -3, 3, -2, 0, 4)
6+
val sample = Seq(1, 2, -3, 3, -2, 0, 4)
77

88
test("Part 1 should handle sample input correctly") {
99
assert(Day20.part1(sample) == 3)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /