Skip to content

Commit 653e6e0

Browse files
committed
2022 day 14 part 2
1 parent df9a227 commit 653e6e0

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

aoc22.playground/Pages/Day13.xcplaygroundpage/Contents.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
import Foundation
44

55
part1()
6+
part2()
67

78
//: [Next](@next)

aoc22.playground/Pages/Day14.xcplaygroundpage/Contents.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
import Foundation
44

55
part1()
6+
part2()
67

78
//: [Next](@next)

aoc22.playground/Pages/Day14.xcplaygroundpage/Sources/Day14.swift

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func boundingBox(_ canvas: Dictionary<Point, Character>) -> (Point, Point) {
2929
minX = min(minX, k.x)
3030
minY = min(minY, k.y)
3131
}
32-
return (Point(x: minX - 1, y: minY - 1), Point(x: maxX + 1, y: maxY + 1))
32+
return (Point(x: minX, y: minY), Point(x: maxX, y: maxY))
3333
}
3434

3535
func makeCanvas(_ lines: [[Point]]) -> Dictionary<Point, Character> {
@@ -60,8 +60,8 @@ func makeCanvas(_ lines: [[Point]]) -> Dictionary<Point, Character> {
6060
func canvasAsString(_ canvas: Dictionary<Point, Character>) -> String {
6161
var result = [Character]()
6262
let bounds = boundingBox(canvas)
63-
for y in (bounds.0.y)...(bounds.1.y) {
64-
for x in (bounds.0.x)...(bounds.1.x) {
63+
for y in (bounds.0.y - 1)...(bounds.1.y + 1) {
64+
for x in (bounds.0.x - 1)...(bounds.1.x + 1) {
6565
if let ch = canvas[Point(x: x, y: y)] {
6666
result.append(ch)
6767
} else {
@@ -103,7 +103,23 @@ public func part1() -> Int {
103103
var canvas = makeCanvas(inputData())
104104
let bounds = boundingBox(canvas)
105105
var result = 0
106-
while dropSand(at: Point(x: 500, y: 0), maxY: bounds.1.y, in: &canvas) {
106+
while dropSand(at: Point(x: 500, y: 0), maxY: bounds.1.y + 2, in: &canvas) {
107+
result += 1
108+
}
109+
return result
110+
}
111+
112+
public func part2() -> Int {
113+
var canvas = makeCanvas(inputData())
114+
let bounds = boundingBox(canvas)
115+
for x in -1000...1000 {
116+
let q = Point(x: x, y: bounds.1.y + 2)
117+
canvas[q] = "#"
118+
}
119+
let origin = Point(x: 500, y: 0)
120+
var result = 0
121+
while nil == canvas[origin] {
122+
_ = dropSand(at: origin, maxY: bounds.1.y + 3, in: &canvas)
107123
result += 1
108124
}
109125
return result

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy