Skip to content

Commit 13a641b

Browse files
committed
Solve the day 25 puzzle
1 parent 00bffea commit 13a641b

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

day25/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Execute this code via `scala day25.scala -- <filename>`.
2+
3+
### Observations
4+

day25/day25.scala

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import io.Source
2+
import scala.util.Using
3+
import scala.collection.mutable.ListBuffer
4+
5+
type Lock = List[Int]
6+
type Key = List[Int]
7+
8+
def readLocksAndKeys(filename: String) =
9+
val tmp = Using(Source.fromFile(filename)) {
10+
_.getLines.foldLeft((List[List[String]](), List[String]())) { (acc, l) =>
11+
val (schemas, currentSchema) = acc
12+
if l.isEmpty then (schemas :+ currentSchema, List[String]())
13+
else (schemas, currentSchema :+ l)
14+
}
15+
}.get
16+
val schemas = tmp._1 :+ tmp._2
17+
18+
schemas.foldLeft((List[Lock](), List[Lock]())) {
19+
(lockAndKeyAcc, schemaLines) =>
20+
val (locks, keys) = lockAndKeyAcc
21+
val schema = (0 until schemaLines(0).length)
22+
.map(i => schemaLines.filter(l => l(i) == '#').length - 1)
23+
.toList
24+
25+
if schemaLines(0).forall(_ == '#') then (locks :+ schema, keys)
26+
else (locks, keys :+ schema)
27+
}
28+
29+
def solve(locks: List[Lock], keys: List[Key]) =
30+
locks
31+
.map(lock =>
32+
keys
33+
.filter(key => lock.zip(key).map((l, k) => l + k).forall(_ <= 5))
34+
.length
35+
)
36+
.sum
37+
38+
@main def main(filename: String) =
39+
val (locks, keys) = readLocksAndKeys(filename)
40+
println(solve(locks, keys))

0 commit comments

Comments
 (0)