Some optimisations

main
Max Nuding 2021-12-17 07:02:13 +01:00
parent 4a62be948b
commit fe8e901212
Signed by: phlaym
GPG Key ID: A06651BAB6777237
1 changed files with 9 additions and 15 deletions

View File

@ -30,7 +30,7 @@ struct Area {
}
func overshot(point: Point) -> Bool {
point.x > botRight.x || point.y < botRight.y
point.y < botRight.y || point.x > botRight.x
}
}
@ -41,18 +41,11 @@ struct Probe {
mutating func move() {
position.x += velocity.x
position.y += velocity.y
var dX: Int
switch velocity.x {
case _ where velocity.x < 0:
dX = 1
case 0:
dX = 0
case _ where velocity.x > 0:
dX = -1
default:
fatalError()
if velocity.x < 0 {
velocity.x += 1
} else if velocity.x > 0 {
velocity.x -= 1
}
velocity.x += dX
velocity.y -= 1
}
}
@ -74,7 +67,8 @@ class Day17: Runnable {
let area = Area(x: parts.first!, y: parts.last!)
var maxY = Int.min
var found = Set<Point>()
var found = [Point]()
found.reserveCapacity(3000)
for x in 0...500 {
for y in -500...500 {
let velocity = Point(x: x, y: y)
@ -89,13 +83,13 @@ class Day17: Runnable {
if maxYCurrent > maxY {
maxY = maxYCurrent
}
found.insert(velocity)
found.append(velocity)
break
}
} while !area.overshot(point: probe.position)
}
}
print(maxY)
print(found.count)
print(Set(found).count)
}
}