-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday19.sc
29 lines (23 loc) · 811 Bytes
/
day19.sc
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
26
27
28
29
import common.{Memo, loadPackets}
import scala.collection.mutable
val input = loadPackets(List("day19.txt"))
val towels = input.head.split(",").map(_.trim)
val patterns = input.drop(2)
case object Memos {
def isPossibleInternal(pattern: String): Boolean = pattern.isEmpty ||
towels.filter(pattern.startsWith)
.map(_.length)
.map(pattern.substring)
.exists(isPossible)
def countPossibleInternal(pattern: String): Long =
if pattern.isEmpty then 1L
else towels.filter(pattern.startsWith)
.map(_.length)
.map(pattern.substring)
.map(countPossible)
.sum
val isPossible = Memo(isPossibleInternal)
val countPossible = Memo(countPossibleInternal)
}
val part1 = patterns.count(Memos.isPossible)
val part2 = patterns.map(Memos.countPossible).sum