22 lines
793 B
Swift
22 lines
793 B
Swift
import Foundation
|
|
|
|
func readInput(_ filePath: String) throws -> [[Character]] {
|
|
let content = try String(contentsOfFile: filePath, encoding: .ascii)
|
|
return content.split(separator: "\n").map(Array.init)
|
|
}
|
|
|
|
let MAS: (Character, Character, Character) = ("M", "A", "S")
|
|
|
|
func dX(_ map: [[Character]], _ i: Int, _ j: Int) -> Bool {
|
|
return ((map[i-1][j-1], map[i][j], map[i+1][j+1]) == MAS
|
|
|| (map[i+1][j+1], map[i][j], map[i-1][j-1]) == MAS)
|
|
&& ((map[i-1][j+1], map[i][j], map[i+1][j-1]) == MAS
|
|
|| (map[i+1][j-1], map[i][j], map[i-1][j+1]) == MAS)
|
|
}
|
|
|
|
let input = try readInput(CommandLine.arguments[1])
|
|
let answer = (1..<input.count-1).reduce(0, { n, i in
|
|
n + (1..<input[i].count-1).reduce(0, { n, j in dX(input, i, j) ? n+1 : n })
|
|
})
|
|
print(answer)
|