diff --git a/Package.swift b/Package.swift index 046f1cb..b9b1956 100644 --- a/Package.swift +++ b/Package.swift @@ -25,7 +25,8 @@ let package = Package( .executable(name: "13", targets: ["13"]), .executable(name: "14", targets: ["14"]), .executable(name: "15", targets: ["15"]), - .executable(name: "16", targets: ["16"]) + .executable(name: "16", targets: ["16"]), + .executable(name: "17", targets: ["17"]) ], dependencies: [ // Dependencies declare other packages that this package depends on. @@ -121,6 +122,10 @@ let package = Package( ), .executableTarget( name: "16", + dependencies: [ .targetItem(name: "Runner", condition: nil) ] + ), + .executableTarget( + name: "17", dependencies: [ .targetItem(name: "Runner", condition: nil), .product(name: "Collections", package: "swift-collections")] ) diff --git a/Sources/16/16.swift b/Sources/16/16.swift index 7cd2a51..8c50c95 100644 --- a/Sources/16/16.swift +++ b/Sources/16/16.swift @@ -7,7 +7,6 @@ import Foundation import Runner -import Collections enum OperatorType: Int { diff --git a/Sources/17/17.swift b/Sources/17/17.swift new file mode 100644 index 0000000..59e6bf6 --- /dev/null +++ b/Sources/17/17.swift @@ -0,0 +1,107 @@ +// +// File.swift +// +// +// Created by Max Nuding on 17.12.21. +// + +import Foundation +import Runner +import Collections + +struct Point:Hashable { + let x: Int + let y: Int +} + +struct Area { + let topLeft: Point + let botRight: Point + + init(x: String, y: String) { + let xVals = x[x.index(x.startIndex, offsetBy: 2)...].components(separatedBy: "..").map { Int($0)! } + let yVals = y[y.index(y.startIndex, offsetBy: 2)...].components(separatedBy: "..").map { Int($0)! } + topLeft = Point(x: xVals.first!, y: yVals.last!) + botRight = Point(x: xVals.last!, y: yVals.first!) + } + + func contains(point: Point) -> Bool { + point.x >= topLeft.x && point.x <= botRight.x && point.y <= topLeft.y && point.y >= botRight.y + } + + 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 + } +} + +struct Probe { + var position: Point + var velocity: Point + + mutating func move() { + position = Point(x: position.x + velocity.x, y: 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() + } + velocity = Point(x: velocity.x + dX, y: velocity.y - 1) + + } +} + +class Day17: Runnable { + let inputPath: String + + required init(inputPath: String) { + self.inputPath = inputPath + } + + public func run() { + let input = try! String(contentsOfFile: inputPath) + let parts = input + .trimmingCharacters(in: .newlines) + .components(separatedBy: ": ") + .last! + .components(separatedBy: ", ") + let area = Area(x: parts.first!, y: parts.last!) + + var maxY = Int.min + var found = Set() + for x in -500...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 maxYCurrent = Int.min + while !area.contains(point: probe.position) && !area.overshot(point: probe.position) { + probe.move() + if probe.position.y > maxYCurrent { + maxYCurrent = probe.position.y + } + } + if area.contains(point: probe.position) { + if maxYCurrent > maxY { + maxY = maxYCurrent + } + found.insert(velocity) + } + } + } + print(maxY) + print(found.count) + } +} diff --git a/Sources/17/main.swift b/Sources/17/main.swift new file mode 100644 index 0000000..f0d9cdb --- /dev/null +++ b/Sources/17/main.swift @@ -0,0 +1,12 @@ +// +// File.swift +// +// +// Created by Max Nuding on 17.12.21. +// + +import Foundation +import Runner + +//Runner(target: Day17.self, day: "17", isTest: true).run() +Runner(target: Day17.self, day: "17", isTest: false).run() diff --git a/Sources/Runner/Resources/input/17.txt b/Sources/Runner/Resources/input/17.txt new file mode 100644 index 0000000..9938bdf --- /dev/null +++ b/Sources/Runner/Resources/input/17.txt @@ -0,0 +1 @@ +target area: x=128..160, y=-142..-88 diff --git a/Sources/Runner/Resources/input/17_test.txt b/Sources/Runner/Resources/input/17_test.txt new file mode 100644 index 0000000..a07e02d --- /dev/null +++ b/Sources/Runner/Resources/input/17_test.txt @@ -0,0 +1 @@ +target area: x=20..30, y=-10..-5