Some optimisations

This commit is contained in:
Max Nuding 2021-12-17 07:02:13 +01:00
parent 4a62be948b
commit fe8e901212
Signed by: phlaym
GPG Key ID: A06651BAB6777237

View File

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