This commit is contained in:
Max Nuding 2021-12-17 06:55:22 +01:00
parent ac6082f993
commit 4a62be948b
Signed by: phlaym
GPG Key ID: A06651BAB6777237

View File

@ -10,8 +10,8 @@ import Runner
import Collections import Collections
struct Point:Hashable { struct Point:Hashable {
let x: Int var x: Int
let y: Int var y: Int
} }
struct Area { struct Area {
@ -30,24 +30,17 @@ struct Area {
} }
func overshot(point: Point) -> Bool { func overshot(point: Point) -> Bool {
overshotX(point: point) || overshotY(point: point) point.x > botRight.x || point.y < botRight.y
}
func overshotX(point: Point) -> Bool {
point.x > botRight.x
}
func overshotY(point: Point) -> Bool {
point.y < botRight.y
} }
} }
struct Probe { struct Probe {
var position: Point var position = Point(x: 0, y: 0)
var velocity: Point var velocity: Point
mutating func move() { mutating func move() {
position = Point(x: position.x + velocity.x, y: position.y + velocity.y) position.x += velocity.x
position.y += velocity.y
var dX: Int var dX: Int
switch velocity.x { switch velocity.x {
case _ where velocity.x < 0: case _ where velocity.x < 0:
@ -59,8 +52,8 @@ struct Probe {
default: default:
fatalError() fatalError()
} }
velocity = Point(x: velocity.x + dX, y: velocity.y - 1) velocity.x += dX
velocity.y -= 1
} }
} }
@ -82,23 +75,24 @@ class Day17: Runnable {
var maxY = Int.min var maxY = Int.min
var found = Set<Point>() var found = Set<Point>()
for x in -500...500 { for x in 0...500 {
for y in -500...500 { for y in -500...500 {
let velocity = Point(x: x, y: y) let velocity = Point(x: x, y: y)
var probe = Probe(position: Point(x: 0, y: 0), velocity: velocity) var probe = Probe(velocity: velocity)
var maxYCurrent = Int.min var maxYCurrent = Int.min
while !area.contains(point: probe.position) && !area.overshot(point: probe.position) { repeat {
probe.move() probe.move()
if probe.position.y > maxYCurrent { if probe.position.y > maxYCurrent {
maxYCurrent = probe.position.y maxYCurrent = probe.position.y
} }
}
if area.contains(point: probe.position) { if area.contains(point: probe.position) {
if maxYCurrent > maxY { if maxYCurrent > maxY {
maxY = maxYCurrent maxY = maxYCurrent
} }
found.insert(velocity) found.insert(velocity)
break
} }
} while !area.overshot(point: probe.position)
} }
} }
print(maxY) print(maxY)