From fe8e901212f68d290b3a97a4cb0562629f02b3e4 Mon Sep 17 00:00:00 2001 From: Max Nuding Date: Fri, 17 Dec 2021 07:02:13 +0100 Subject: [PATCH] Some optimisations --- Sources/17/17.swift | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/Sources/17/17.swift b/Sources/17/17.swift index 09b82ec..cc1baca 100644 --- a/Sources/17/17.swift +++ b/Sources/17/17.swift @@ -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() + 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) } }