d04
This commit is contained in:
21
day04/d04p2.swift
Normal file
21
day04/d04p2.swift
Normal file
@@ -0,0 +1,21 @@
|
||||
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)
|
Reference in New Issue
Block a user