Some optimisations
This commit is contained in:
parent
4a62be948b
commit
fe8e901212
@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user