Skip to content

Commit cea612a

Browse files
committed
feat(2024): day 25
1 parent 885a4a2 commit cea612a

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

2024/day25.scala

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package `2024`.day25
2+
3+
import prelude.*
4+
5+
enum Scheme(xs: Vector[Int]):
6+
case Lock(xs: Vector[Int]) extends Scheme(xs)
7+
case Key(xs: Vector[Int]) extends Scheme(xs)
8+
9+
export Scheme.*
10+
11+
extension (l: Lock)
12+
inline infix def isMatching(k: Key)(using height: Int): Boolean =
13+
(l.xs zip k.xs).forall((x, y) => x + y <= height)
14+
15+
def parse(input: String): Scheme =
16+
val lines = input.split('\n').toVector.transpose
17+
val xs = lines.map(_.count(_ == '#'))
18+
if lines.head.head == '#' then Lock(xs) else Key(xs)
19+
20+
@main def main() =
21+
val input = fromFile(".cache/2024/25.txt").mkString.trim
22+
val xs = input.split("\n\n").toVector
23+
24+
given height: Int = xs.head.linesIterator.size
25+
26+
val (locks, keys) = xs.map(parse).partitionMap {
27+
case lock: Lock => Left(lock)
28+
case key: Key => Right(key)
29+
}
30+
val combos = locks.flatMap(l => keys.filter(l.isMatching))
31+
println(combos.size)

0 commit comments

Comments
 (0)