Skip to content

Commit 64ceaec

Browse files
authored
ensure no duplicates
1 parent c602cfa commit 64ceaec

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

2022/src/day13.scala

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,17 @@ def findOrderedIndices(input: String): Int =
3333

3434
def findDividerIndices(input: String): Int =
3535
val dividers = List("[[2]]", "[[6]]").map(readPacket)
36-
val packets = List.from(input.linesIterator.filter(_.nonEmpty).map(readPacket))
37-
val indices = (dividers ++ packets).sorted.zipWithIndex.map((key, value) => key -> (value + 1)).toMap
38-
dividers.map(indices(_)).product
36+
val lookup = dividers.toSet
37+
val packets = input
38+
.linesIterator
39+
.filter(_.nonEmpty)
40+
.map(readPacket)
41+
val indices = (dividers ++ packets)
42+
.sorted
43+
.iterator
44+
.zipWithIndex
45+
.collect { case (p, i) if lookup.contains(p) => i + 1 }
46+
indices.take(2).product
3947

4048
enum Packet:
4149
case Nested(packets: List[Packet])

0 commit comments

Comments
 (0)