From 612abde8f2b32d43b26cffec37f7d5aac2821642 Mon Sep 17 00:00:00 2001 From: Dory Date: Thu, 12 Dec 2024 18:16:45 -0800 Subject: [PATCH] d04 --- day04/d04p1.swift | 51 +++++++++++++++++ day04/d04p2.swift | 21 +++++++ day04/input.txt | 140 ++++++++++++++++++++++++++++++++++++++++++++++ day04/test.txt | 10 ++++ 4 files changed, 222 insertions(+) create mode 100644 day04/d04p1.swift create mode 100644 day04/d04p2.swift create mode 100644 day04/input.txt create mode 100644 day04/test.txt diff --git a/day04/d04p1.swift b/day04/d04p1.swift new file mode 100644 index 0000000..f55dcb3 --- /dev/null +++ b/day04/d04p1.swift @@ -0,0 +1,51 @@ +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 XMAS: (Character, Character, Character, Character) = ("X", "M", "A", "S") + +func dE(_ map: [[Character]], _ i: Int, _ j: Int) -> Bool { + return j < map[i].count - 3 + && (map[i][j], map[i][j+1], map[i][j+2], map[i][j+3]) == XMAS +} +func dW(_ map: [[Character]], _ i: Int, _ j: Int) -> Bool { + return j >= 3 + && (map[i][j], map[i][j-1], map[i][j-2], map[i][j-3]) == XMAS +} +func dS(_ map: [[Character]], _ i: Int, _ j: Int) -> Bool { + return i < map.count - 3 + && (map[i][j], map[i+1][j], map[i+2][j], map[i+3][j]) == XMAS +} +func dN(_ map: [[Character]], _ i: Int, _ j: Int) -> Bool { + return i >= 3 + && (map[i][j], map[i-1][j], map[i-2][j], map[i-3][j]) == XMAS +} +func dNW(_ map: [[Character]], _ i: Int, _ j: Int) -> Bool { + return i >= 3 && j >= 3 + && (map[i][j], map[i-1][j-1], map[i-2][j-2], map[i-3][j-3]) == XMAS +} +func dSE(_ map: [[Character]], _ i: Int, _ j: Int) -> Bool { + return i < map.count - 3 && j < map[i].count - 3 + && (map[i][j], map[i+1][j+1], map[i+2][j+2], map[i+3][j+3]) == XMAS +} +func dNE(_ map: [[Character]], _ i: Int, _ j: Int) -> Bool { + return i >= 3 && j < map[i].count - 3 + && (map[i][j], map[i-1][j+1], map[i-2][j+2], map[i-3][j+3]) == XMAS +} +func dSW(_ map: [[Character]], _ i: Int, _ j: Int) -> Bool { + return i < map.count - 3 && j >= 3 + && (map[i][j], map[i+1][j-1], map[i+2][j-2], map[i+3][j-3]) == XMAS +} + +func xmasCount(_ map: [[Character]], _ i: Int, _ j: Int) -> Int { + let funcs = [dE, dW, dS, dN, dNW, dSE, dNE, dSW] + return funcs.reduce(0, { sum, f in f(map, i, j) ? sum+1 : sum }) +} + +let input = try readInput(CommandLine.arguments[1]) +let answer = (0.. [[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..