// // File.swift // // // Created by Max Nuding on 12.12.21. // import Foundation import Runner import Collections struct Coord: Hashable { var x: Int var y: Int init(x: Int, y: Int) { self.x = x self.y = y } init(line: String) { let parts = line.split(separator: ",").map { Int($0)! } x = parts.first! y = parts.last! } func foldingUp(by y: Int) -> Coord { guard self.y > y else { return self } return Coord(x: x, y: y - (self.y - y)) } func foldingLeft(by x: Int) -> Coord { guard self.x > x else { return self } return Coord(x: x - (self.x - x), y: y) } func folding(by instruction: FoldingInstruction) -> Coord { switch instruction { case .up(let y): return foldingUp(by: y) case .left(let x): return foldingLeft(by: x) } } /*mutating func foldUp(by y: Int) { guard self.y > y else { return } self.y = y - (self.y - y) } mutating func foldLeft(by x: Int) { guard self.x > x else { return } self.x = x - (self.x - x) } mutating func fold(by instruction: FoldingInstruction) { switch instruction { case .up(let y): foldUp(by: y) case .left(let x): foldLeft(by: x) } }*/ } enum FoldingInstruction { case up(y: Int) case left(x: Int) init?(line: String) { let i = line.split(separator: " ").last!.split(separator: "=") let coord = Int(i.last!)! switch i.first { case "x": self = .left(x: coord) case "y": self = .up(y: coord) default: return nil } } } class Field { var coords = [Coord]() } class Day13: Runnable { let inputPath: String //var field = [String:Cave]() required init(inputPath: String) { self.inputPath = inputPath } public func run() { let input = try! String(contentsOfFile: inputPath) let parts = input .trimmingCharacters(in: .newlines) //.components(separatedBy: .newlines) .components(separatedBy: "\n\n") var dots = parts.first! .components(separatedBy: .newlines) .map(Coord.init) let instructions = parts.last! .components(separatedBy: .newlines) .map { FoldingInstruction(line: $0)! } //printDots(dots: dots) //print("") for instruction in instructions { //dots.forEach { $0.fold(by: instruction) } dots = dots.map { $0.folding(by: instruction) } //print(instruction) //printDots(dots: dots) print(Set(dots).count) //print("") break } /*print(dots.count) print(Set(dots).count) print(parts)*/ } func printDots(dots:[Coord]) { let maxX = dots.map {$0.x}.max()! let maxY = dots.map {$0.y}.max()! for x in 0...maxX { for y in 0...maxY { print(dots.contains(Coord(x: x, y: y)) ? "#" : ".", terminator: "") } print("") } } }