File tree Expand file tree Collapse file tree 2 files changed +19
-2
lines changed
main/scala/eu/sim642/adventofcode2020
test/scala/eu/sim642/adventofcode2020 Expand file tree Collapse file tree 2 files changed +19
-2
lines changed Original file line number Diff line number Diff line change @@ -69,8 +69,19 @@ object Day22 {
69
69
case (card1 +: newDeck1, card2 +: newDeck2) =>
70
70
val winner : Either [Any , Any ] = {
71
71
if (newDeck1.lengthIs >= card1 && newDeck2.lengthIs >= card2) {
72
- val recDecks = (newDeck1.take(card1), newDeck2.take(card2))
73
- playWinner(recDecks)
72
+ val recDec1 = newDeck1.take(card1)
73
+ val recDec2 = newDeck2.take(card2)
74
+ // "Supercard" optimization:
75
+ // https://github.com/glguy/advent2020/blob/1776cb3388581a1fbab79f1802650d43e24ffa66/execs/Day22.hs#L62-L65
76
+ // No difference on my input, 20× difference on mstksg input
77
+ val recDec1Max = recDec1.max
78
+ val recDec2Max = recDec2.max
79
+ if (recDec1Max > recDec2Max && recDec1Max >= (card1 + card2))
80
+ Left (())
81
+ else {
82
+ val recDecks = (recDec1, recDec2)
83
+ playWinner(recDecks)
84
+ }
74
85
} else {
75
86
if (card1 > card2)
76
87
Left (())
Original file line number Diff line number Diff line change @@ -20,6 +20,8 @@ class Day22Test extends AnyFunSuite {
20
20
|7
21
21
|10""" .stripMargin
22
22
23
+ lazy val mstksgInput : String = io.Source .fromInputStream(getClass.getResourceAsStream(" day22mstksg.txt" )).mkString.trim
24
+
23
25
test(" Part 1 examples" ) {
24
26
assert(Part1 .winningScore(parseDecks(exampleInput)) == 306 )
25
27
}
@@ -35,4 +37,8 @@ class Day22Test extends AnyFunSuite {
35
37
test(" Part 2 input answer" ) {
36
38
assert(Part2 .winningScore(parseDecks(input)) == 32317 )
37
39
}
40
+
41
+ test(" Part 2 mstksg" ) {
42
+ assert(Part2 .winningScore(parseDecks(mstksgInput)) == 32760 ) // unconfirmed answer
43
+ }
38
44
}
You can’t perform that action at this time.
0 commit comments