generated from Jadarma/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Y2024D04.kt
49 lines (42 loc) · 1.73 KB
/
Y2024D04.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package aockt.y2024
import aockt.util.parse
import aockt.util.spacial.Direction.*
import aockt.util.spacial.Grid
import aockt.util.spacial.get
import aockt.util.spacial.Point
import aockt.util.spacial.move
import aockt.util.spacial.points
import io.github.jadarma.aockt.core.Solution
object Y2024D04 : Solution {
/** Parse the [input] and return the word-search as a grid of characters. */
private fun parseInput(input: String): Grid<Char> = parse { Grid(input) }
override fun partOne(input: String): Int = with(parseInput(input)) {
val directions = listOf<(Point, Any) -> Point>(
{ p, _ -> p.move(Up) },
{ p, _ -> p.move(Right).move(Up) },
{ p, _ -> p.move(Right) },
{ p, _ -> p.move(Right).move(Down) },
{ p, _ -> p.move(Down) },
{ p, _ -> p.move(Left).move(Down) },
{ p, _ -> p.move(Left) },
{ p, _ -> p.move(Left).move(Up) },
)
points()
.filter { (_, v) -> v == 'X' }
.sumOf { (p, _) ->
directions
.map { direction -> move(p, direction).take(4).joinToString("") { it.value.toString() } }
.count { it == "XMAS" }
}
}
override fun partTwo(input: String): Int = with(parseInput(input)) {
val mas = setOf("MAS", "SAM")
points()
.filter { (p, v) -> v == 'A' && p.x in 1..<width - 1 && p.y in 1..<height - 1 }
.count { (p, v) ->
val mainDiag = "${get(p.move(Left).move(Up))}$v${get(p.move(Right).move(Down))}"
val sideDiag = "${get(p.move(Right).move(Up))}$v${get(p.move(Left).move(Down))}"
mainDiag in mas && sideDiag in mas
}
}
}