|
| 1 | +import Foundation |
| 2 | + |
| 3 | +enum Shape: CaseIterable { |
| 4 | + case horizontal |
| 5 | + case plus |
| 6 | + case ell |
| 7 | + case vertical |
| 8 | + case square |
| 9 | +} |
| 10 | + |
| 11 | +enum Direction: Character { |
| 12 | + case right = ">" |
| 13 | + case left = "<" |
| 14 | +} |
| 15 | + |
| 16 | +struct Point: Hashable, Equatable { |
| 17 | + let x: Int |
| 18 | + let y: Int |
| 19 | + |
| 20 | + var down: Point { Point(x: x, y: y - 1) } |
| 21 | + var left: Point { Point(x: x - 1, y: y) } |
| 22 | + var right: Point { Point(x: x + 1, y: y) } |
| 23 | +} |
| 24 | + |
| 25 | +struct Block { |
| 26 | + var coord: Point |
| 27 | + let shape: Shape |
| 28 | + |
| 29 | + var points: Set<Point> { |
| 30 | + let x = coord.x |
| 31 | + let y = coord.y |
| 32 | + switch shape { |
| 33 | + case .horizontal: |
| 34 | + return [Point(x: x, y: y), Point(x: x + 1, y: y), Point(x: x + 2, y: y), Point(x: x + 3, y: y)] |
| 35 | + case .plus: |
| 36 | + return [Point(x: x + 1, y: y), Point(x: x, y: y + 1), Point(x: x + 1, y: y + 1), Point(x: x + 2, y: y + 1), Point(x: x + 1, y: y + 2)] |
| 37 | + case .ell: |
| 38 | + return [Point(x: x, y: y), Point(x: x + 1, y: y), Point(x: x + 2, y: y), Point(x: x + 2, y: y + 1), Point(x: x + 2, y: y + 2)] |
| 39 | + case .vertical: |
| 40 | + return [Point(x: x, y: y), Point(x: x, y: y + 1), Point(x: x, y: y + 2), Point(x: x, y: y + 3)] |
| 41 | + case .square: |
| 42 | + return [Point(x: x, y: y), Point(x: x + 1, y: y), Point(x: x, y: y + 1), Point(x: x + 1, y: y + 1)] |
| 43 | + } |
| 44 | + } |
| 45 | + var down: Set<Point> { |
| 46 | + Set(points.map(\.down)) |
| 47 | + } |
| 48 | + var left: Set<Point> { |
| 49 | + Set(points.map(\.left)) |
| 50 | + } |
| 51 | + var right: Set<Point> { |
| 52 | + Set(points.map(\.right)) |
| 53 | + } |
| 54 | + mutating func push(_ direction: Direction, in canvas: Set<Point>) { |
| 55 | + switch direction { |
| 56 | + case .left: |
| 57 | + let points = left |
| 58 | + if points.allSatisfy({ $0.x >= 0 }) && canvas.intersection(points).isEmpty { |
| 59 | + coord = coord.left |
| 60 | + } |
| 61 | + case .right: |
| 62 | + let points = right |
| 63 | + if points.allSatisfy({ $0.x <= 6 }) && canvas.intersection(points).isEmpty { |
| 64 | + coord = coord.right |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + mutating func fall(in canvas: Set<Point>) -> Bool { |
| 69 | + let points = down |
| 70 | + if points.allSatisfy({ $0.y >= 0 }) && canvas.intersection(points).isEmpty { |
| 71 | + coord = coord.down |
| 72 | + return true |
| 73 | + } |
| 74 | + return false |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func show(_ canvas: Set<Point>, _ block: Block) -> String { |
| 79 | + let maxY = canvas.union(block.points).reduce(0) { max($0, $1.y) } |
| 80 | + var result = [Character]() |
| 81 | + for y in (-1 * maxY)...0 { |
| 82 | + for x in 0...6 { |
| 83 | + let p = Point(x: x, y: -1 * y) |
| 84 | + if canvas.contains(p) { |
| 85 | + result.append("#") |
| 86 | + } else if block.points.contains(p) { |
| 87 | + result.append("@") |
| 88 | + } else { |
| 89 | + result.append(".") |
| 90 | + } |
| 91 | + } |
| 92 | + result.append("\n") |
| 93 | + } |
| 94 | + result.append("\n") |
| 95 | + return String(result) |
| 96 | +} |
| 97 | + |
| 98 | +public func part1() -> Int { |
| 99 | + let wind = stringsFromFile()[0].compactMap { Direction(rawValue: $0) } |
| 100 | + func shape(_ i: Int) -> Shape { |
| 101 | + Shape.allCases[i % 5] |
| 102 | + } |
| 103 | + var canvas = Set<Point>() |
| 104 | + var y = 3 |
| 105 | + var w = 0 |
| 106 | + for s in 0..<2022 { |
| 107 | + var block = Block(coord: Point(x: 2, y: y), shape: shape(s)) |
| 108 | + while true { |
| 109 | + block.push(wind[w], in: canvas) |
| 110 | + w = (w + 1) % wind.count |
| 111 | + if !block.fall(in: canvas) { |
| 112 | + let points = block.points |
| 113 | + canvas.formUnion(points) |
| 114 | + y = 4 + canvas.reduce(0) { max($0, $1.y) } |
| 115 | + break |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + return 1 + canvas.reduce(0) { max($0, $1.y) } |
| 120 | +} |
| 121 | + |
0 commit comments