File tree Expand file tree Collapse file tree 2 files changed +58
-0
lines changed
main/scala/eu/sim642/adventofcode2020
test/scala/eu/sim642/adventofcode2020 Expand file tree Collapse file tree 2 files changed +58
-0
lines changed Original file line number Diff line number Diff line change
1
+ package eu .sim642 .adventofcode2020
2
+
3
+ import eu .sim642 .adventofcodelib .IteratorImplicits ._
4
+ import eu .sim642 .adventofcodelib .SeqImplicits ._
5
+ import eu .sim642 .adventofcodelib .IntegralImplicits ._
6
+
7
+ object Day23 {
8
+
9
+ type Cups = Vector [Int ]
10
+
11
+ def simulateMove (cups : Cups ): Cups = {
12
+ val (current +: picked, rest) = cups.splitAt(4 )
13
+ val destination = Iterator .from(current - 1 , - 1 ).map(_ %+ 10 ).find(rest.contains).get
14
+ val (beforeDestination, afterDestination) = rest.splitAt(rest.indexOf(destination) + 1 )
15
+ val newRest = beforeDestination ++ picked ++ afterDestination
16
+ (current +: newRest).rotateLeft(1 )
17
+ }
18
+
19
+ def simulateMoves (cups : Cups , moves : Int = 100 ): Cups = {
20
+ Iterator .iterate(cups)(simulateMove)(moves)
21
+ }
22
+
23
+ def simulateMovesLabels (cups : Cups , moves : Int = 100 ): String = {
24
+ val finalCups = simulateMoves(cups, moves)
25
+ val 1 +: restCups = finalCups.rotateLeft(finalCups.indexOf(1 ))
26
+ restCups.mkString(" " )
27
+ }
28
+
29
+
30
+ def parseCups (input : String ): Cups = input.map(_.asDigit).toVector
31
+
32
+ // lazy val input: String = io.Source.fromInputStream(getClass.getResourceAsStream("day23.txt")).mkString.trim
33
+ val input : String = " 318946572"
34
+
35
+ def main (args : Array [String ]): Unit = {
36
+ println(simulateMovesLabels(parseCups(input)))
37
+ }
38
+ }
Original file line number Diff line number Diff line change
1
+ package eu .sim642 .adventofcode2020
2
+
3
+ import Day23 ._
4
+ import org .scalatest .funsuite .AnyFunSuite
5
+
6
+ class Day23Test extends AnyFunSuite {
7
+
8
+ val exampleInput = " 389125467"
9
+
10
+ test(" Part 1 examples" ) {
11
+ assert(simulateMove(parseCups(exampleInput)) == parseCups(" 289154673" ))
12
+
13
+ assert(simulateMovesLabels(parseCups(exampleInput), 10 ) == " 92658374" )
14
+ assert(simulateMovesLabels(parseCups(exampleInput)) == " 67384529" )
15
+ }
16
+
17
+ test(" Part 1 input answer" ) {
18
+ assert(simulateMovesLabels(parseCups(input)) == " 52864379" )
19
+ }
20
+ }
You can’t perform that action at this time.
0 commit comments