-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay04.kt
33 lines (29 loc) · 1.06 KB
/
Day04.kt
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
30
31
32
33
fun main() {
val text = mapLines { it.toList() }
val diagonals = listOf(Index2(-1, -1), Index2(-1, 1), Index2(1, 1), Index2(1, -1))
val first = text.indexed2Sequence()
.sumOf { (index, _) ->
Direction.entries.asSequence()
.map { it.point }
.plus(diagonals)
.count { direction ->
"XMAS".asSequence().withIndex().all { (i, char) ->
char == text.getOrNull(index + direction * i)
}
}
}
val second = text.indexed2Sequence()
.filter { (_, char) -> char == 'A' }
.count { (index, _) ->
diagonals.asSequence()
.plus(diagonals.first())
.windowed(2)
.any { directions ->
directions.all { direction ->
'M' == text.getOrNull(index + direction)
&& 'S' == text.getOrNull(index + direction * -1)
}
}
}
println("$first $second")
}