Part 1 in one line: fun calc(input: List): Int = input.flatMapIndexed { y, s -> s.mapIndexed { x, c -> x to c } .filter { it.second == 'X' }.map { it.first to y } }.sumOf { p -> listOf(0 to -1, 1 to -1, 1 to 0, 1 to 1, 0 to 1, -1 to 1, -1 to 0, -1 to -1) .count { o -> "XMAS".indices.map { input.getOrNull(p.second + it * o.second) ?.getOrNull(p.first + it * o.first) ?: "" }.joinToString("") == "XMAS" } } Part 2 in one line: fun calc(input:List): Int = input.flatMapIndexed { y, s -> s.mapIndexed { x, c -> x to c } .filter { it.second == 'A' }.map { it.first to y } }.count { p -> listOf(listOf(-1 to -1, 0 to 0, 1 to 1), listOf(1 to -1, 0 to 0, -1 to 1)).map{ it.map { s -> input.getOrNull(p.second + s.second) ?.getOrNull(p.first + s.first) ?: "" }.joinToString("").let{ it == "MAS" || it == "SAM" } }.all { it } }
they made their solution so complex that it's just useful for content creations and not any real world usage but they gonna scare away newbees from kotlin 🤣
Part 1- way of thinking for the solution is amazing
Part 1 in one line:
fun calc(input: List): Int = input.flatMapIndexed { y, s -> s.mapIndexed { x, c -> x to c }
.filter { it.second == 'X' }.map { it.first to y } }.sumOf { p ->
listOf(0 to -1, 1 to -1, 1 to 0, 1 to 1, 0 to 1, -1 to 1, -1 to 0, -1 to -1)
.count { o -> "XMAS".indices.map { input.getOrNull(p.second + it * o.second)
?.getOrNull(p.first + it * o.first) ?: "" }.joinToString("") == "XMAS" } }
Part 2 in one line:
fun calc(input:List): Int = input.flatMapIndexed { y, s -> s.mapIndexed { x, c -> x to c }
.filter { it.second == 'A' }.map { it.first to y } }.count { p -> listOf(listOf(-1 to -1, 0 to 0, 1 to 1),
listOf(1 to -1, 0 to 0, -1 to 1)).map{ it.map { s -> input.getOrNull(p.second + s.second)
?.getOrNull(p.first + s.first) ?: "" }.joinToString("").let{ it == "MAS" || it == "SAM" } }.all { it } }
they made their solution so complex that it's just useful for content creations and not any real world usage but they gonna scare away newbees from kotlin 🤣