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
struct Point:Hashable {
let x: Int
let y: Int
var x: Int
var y: Int
}
struct Area {
@ -30,24 +30,17 @@ struct Area {
}
func overshot(point: Point) -> Bool {
overshotX(point: point) || overshotY(point: point)
}
func overshotX(point: Point) -> Bool {
point.x > botRight.x
}
func overshotY(point: Point) -> Bool {
point.y < botRight.y
point.x > botRight.x || point.y < botRight.y
}
}
struct Probe {
var position: Point
var position = Point(x: 0, y: 0)
var velocity: Point
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
switch velocity.x {
case _ where velocity.x < 0:
@ -59,8 +52,8 @@ struct Probe {
default:
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 found = Set<Point>()
for x in -500...500 {
for x in 0...500 {
for y in -500...500 {
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
while !area.contains(point: probe.position) && !area.overshot(point: probe.position) {
repeat {
probe.move()
if probe.position.y > maxYCurrent {
maxYCurrent = probe.position.y
}
}
if area.contains(point: probe.position) {
if maxYCurrent > maxY {
maxY = maxYCurrent
if area.contains(point: probe.position) {
if maxYCurrent > maxY {
maxY = maxYCurrent
}
found.insert(velocity)
break
}
found.insert(velocity)
}
} while !area.overshot(point: probe.position)
}
}
print(maxY)