diff --git a/day14/d14p1.swift b/day14/d14p1.swift new file mode 100644 index 0000000..4402999 --- /dev/null +++ b/day14/d14p1.swift @@ -0,0 +1,51 @@ +import Foundation + +let (w, h) = (101, 103) + +infix operator %%: MultiplicationPrecedence +func %%(lhs: T, rhs: T) -> T { + return (lhs % rhs + rhs) % rhs +} + +struct Robot : CustomStringConvertible { + var (x, y): (Int, Int) + let (vx, vy): (Int, Int) + var description: String { return "p=(\(x), \(y)) v=(\(vx), \(vy))" } + + mutating func move() { + x = (x + vx) %% w + y = (y + vy) %% h + } +} + +func readInput(_ filePath: String) throws -> [Robot] { + let robot = try Regex(#"p=([0-9]+),([0-9]+) v=([0-9-]+),([0-9-]+)"#) + var robots: [Robot] = [] + let content = try String(contentsOfFile: filePath, encoding: .ascii) + content.split(separator: "\n").forEach { line in + if let m = line.wholeMatch(of: robot) { + robots.append(Robot( + x: Int(m.output[1].substring!)!, + y: Int(m.output[2].substring!)!, + vx: Int(m.output[3].substring!)!, + vy: Int(m.output[4].substring!)! + )) + } + } + return robots +} + +var robots = try readInput(CommandLine.arguments[1]) +for _ in 0..<100 { + for i in 0.. w/2 && robot.y < h/2 { ur += 1 } + if robot.x < w/2 && robot.y > h/2 { ll += 1 } + if robot.x > w/2 && robot.y > h/2 { lr += 1 } +} +print("\(ul) x \(ur) x \(ll) x \(lr) = \(ul * ur * ll * lr)") diff --git a/day14/d14p2.swift b/day14/d14p2.swift new file mode 100644 index 0000000..3756293 --- /dev/null +++ b/day14/d14p2.swift @@ -0,0 +1,70 @@ +import Foundation + +let (w, h) = (101, 103) + +infix operator %%: MultiplicationPrecedence +func %%(lhs: T, rhs: T) -> T { + return (lhs % rhs + rhs) % rhs +} + +struct Robot : CustomStringConvertible { + var (x, y): (Int, Int) + let (vx, vy): (Int, Int) + var description: String { return "p=(\(x), \(y)) v=(\(vx), \(vy))" } + + mutating func move() { + x = (x + vx) %% w + y = (y + vy) %% h + } +} + +func readInput(_ filePath: String) throws -> [Robot] { + let robot = try Regex(#"p=([0-9]+),([0-9]+) v=([0-9-]+),([0-9-]+)"#) + var robots: [Robot] = [] + let content = try String(contentsOfFile: filePath, encoding: .ascii) + content.split(separator: "\n").forEach { line in + if let m = line.wholeMatch(of: robot) { + robots.append(Robot( + x: Int(m.output[1].substring!)!, + y: Int(m.output[2].substring!)!, + vx: Int(m.output[3].substring!)!, + vy: Int(m.output[4].substring!)! + )) + } + } + return robots +} + +func complexity(_ input: String) -> Int { + var last: Character = " " + var changes = 0 + for c in input { + if c != last { + last = c + changes += 1 + } + } + return changes +} + +func printRobots(_ robots: [Robot]) -> String { + var screen: [[String]] = Array(repeating: Array(repeating: " ", count: 101), count: 103) + robots.forEach { robot in screen[robot.y][robot.x] = "█" } + return screen.map { row in row.joined() }.joined(separator: "\n") +} + +var bots = try readInput(CommandLine.arguments[1]) +var i = 0 +while true { + i += 1 + for i in 0..