Added some utility functions to CharGrid.

This commit is contained in:
Chris Hodges 2024-12-04 06:38:33 +01:00
parent 77618b3534
commit 4bf79c0e89
2 changed files with 36 additions and 21 deletions

View File

@ -159,6 +159,36 @@ class CharGrid {
relposes.map { get(c + it.dc, r + it.dr) } relposes.map { get(c + it.dc, r + it.dr) }
} }
fun countStrings(relposeList: Iterable<Iterable<RelPos>>, string: String) = findMatches(relposeList) { it == string }.count()
fun findMatches(relposeList: Iterable<Iterable<RelPos>>, predicate: (content: String) -> Boolean): List<Pair<String, RelPos>> {
val matches = ArrayList<Pair<String, RelPos>>()
for (r in 0 until height) {
for (c in 0 until width) {
for (rp in relposeList) {
val st = collectRelative(c, r, rp).joinToString("")
if (predicate(st)) {
matches.add(st to RelPos(c, r))
}
}
}
}
return matches
}
fun findCombinedMatches(relposeList: Iterable<Iterable<RelPos>>, predicate: (content: List<String>) -> Boolean): List<RelPos> {
val matches = ArrayList<RelPos>()
for (r in 0 until height) {
for (c in 0 until width) {
val str = relposeList.map { collectRelative(c, r, it).joinToString("") }
if (predicate(str)) {
matches.add(RelPos(c, r))
}
}
}
return matches
}
fun matchRelative(c: Int, r: Int, relposes: Iterable<RelPos>, predicate: (char: Char) -> Boolean): List<RelPos> = fun matchRelative(c: Int, r: Int, relposes: Iterable<RelPos>, predicate: (char: Char) -> Boolean): List<RelPos> =
relposes.filter { predicate(get(c + it.dc, r + it.dr)) } relposes.filter { predicate(get(c + it.dc, r + it.dr)) }

View File

@ -70,31 +70,16 @@ MXMXAXMASX
listOf(RelPos(0, 0), RelPos(0, 1), RelPos(0, 2), RelPos(0, 3)), listOf(RelPos(0, 0), RelPos(0, 1), RelPos(0, 2), RelPos(0, 3)),
listOf(RelPos(0, 0), RelPos(0, -1), RelPos(0, -2), RelPos(0, -3)), listOf(RelPos(0, 0), RelPos(0, -1), RelPos(0, -2), RelPos(0, -3)),
) )
var sum = 0 return grid.countStrings(relposes, "XMAS")
for (x in 0 until grid.width) {
for (y in 0 until grid.height) {
for (rp in relposes) {
val st = grid.collectRelative(x, y, rp).joinToString("")
if (st == "XMAS") sum++
}
}
}
return sum
} }
fun part2(input: List<String>): Int { fun part2(input: List<String>): Int {
val grid = CharGrid(input) val grid = CharGrid(input)
val rp1 = listOf(RelPos(-1, -1), RelPos(0, 0), RelPos(1, 1)) val relposes = listOf(
val rp2 = listOf(RelPos(-1, 1), RelPos(0, 0), RelPos(1, -1)) listOf(RelPos(-1, -1), RelPos(0, 0), RelPos(1, 1)),
var sum = 0 listOf(RelPos(-1, 1), RelPos(0, 0), RelPos(1, -1)),
for (x in 0 until grid.width) { )
for (y in 0 until grid.height) { return grid.findCombinedMatches(relposes) { it.all { it == "MAS" || it == "SAM" } }.count()
val st1 = grid.collectRelative(x, y, rp1).joinToString("")
val st2 = grid.collectRelative(x, y, rp2).joinToString("")
if ((st1 == "MAS" || st1 == "SAM") && (st2 == "MAS" || st2 == "SAM")) sum++
}
}
return sum
} }
// test if implementation meets criteria from the description, like: // test if implementation meets criteria from the description, like: