import Foundation struct Coord : Hashable, CustomStringConvertible { let (x, y): (Int, Int) var description: String { return "(\(x),\(y))" } } func readInput(_ fileName: String) throws -> [Coord] { let xy = try Regex(#"([0-9]+),([0-9]+)"#) return try String(contentsOfFile: fileName, encoding: .ascii) .split(separator: "\n") .compactMap { $0.wholeMatch(of: xy) } .map { Coord( x: Int($0.output[1].substring!)!, y: Int($0.output[2].substring!)! ) } } struct Map : CustomStringConvertible { let walls: Set let (w, h): (Int, Int) var description: String { let top = " \u{001B}[4m" + (0.. Int { return abs(a.x - b.x) + abs(a.y - b.y) } func astar(from start: Coord, to end: Coord) -> Int? { var q = Heap<(Coord, Int)>(comparator: { l, r in l.1 < r.1 }) var seen: [Coord: Int] = [start: 0] q.insert((start, heur(start, end))) while let (cell, _) = q.pop() { if cell == end { return seen[cell] } [(0, -1), (0, 1), (-1, 0), (1, 0)].map { dy, dx in Coord(x: cell.x + dx, y: cell.y + dy)} .filter { c in c.x >= 0 && c.y >= 0 && c.x < w && c.y < h } .filter { !walls.contains($0) } .forEach { nb in let newCost = seen[cell]! + 1 if seen[nb, default: w*h] > newCost { seen[nb] = newCost q.insert((nb, newCost + heur(nb, end))) } } } return nil } } @main struct AoC { static func main() throws { let bytes = try readInput(CommandLine.arguments[1]) let w = Int(CommandLine.arguments[2]) ?? 71 let h = Int(CommandLine.arguments[3]) ?? 71 let startLimit = Int(CommandLine.arguments[4]) ?? 1024 for limit in startLimit..