From 3ba710552d43c47b5ed09f484ec76d1bb60d3f18 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Sat, 18 Dec 2021 13:33:50 +0100 Subject: [PATCH] Day 17 --- AoC21.xcodeproj/project.pbxproj | 99 +++++++++++++++++++++++++++++++++ day17.swift | 71 +++++++++++++++++++++++ 2 files changed, 170 insertions(+) create mode 100644 day17.swift diff --git a/AoC21.xcodeproj/project.pbxproj b/AoC21.xcodeproj/project.pbxproj index 614d3a5..2ac86f0 100644 --- a/AoC21.xcodeproj/project.pbxproj +++ b/AoC21.xcodeproj/project.pbxproj @@ -19,6 +19,8 @@ 263CCF6A276606010061B353 /* day11.swift in Sources */ = {isa = PBXBuildFile; fileRef = 263CCF69276606010061B353 /* day11.swift */; }; 263CCF782766066D0061B353 /* day12.swift in Sources */ = {isa = PBXBuildFile; fileRef = 263CCF76276606670061B353 /* day12.swift */; }; 263CCF79276606730061B353 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; + 263D3E33276DDA6600B0AC5B /* day17.swift in Sources */ = {isa = PBXBuildFile; fileRef = 263D3E32276DDA6600B0AC5B /* day17.swift */; }; + 263D3E34276DDA7000B0AC5B /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; 265112972767D16D009B7607 /* day13.swift in Sources */ = {isa = PBXBuildFile; fileRef = 265112962767D16D009B7607 /* day13.swift */; }; 265112982767D171009B7607 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; 269BE5BC2762959C00871C85 /* day8part2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5BB2762959B00871C85 /* day8part2.swift */; }; @@ -95,6 +97,15 @@ ); runOnlyForDeploymentPostprocessing = 1; }; + 263D3E29276DDA5400B0AC5B /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; 2651128D2767D15A009B7607 /* CopyFiles */ = { isa = PBXCopyFilesBuildPhase; buildActionMask = 2147483647; @@ -148,6 +159,8 @@ 263CCF69276606010061B353 /* day11.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = day11.swift; sourceTree = ""; }; 263CCF6F2766065B0061B353 /* Day12 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day12; sourceTree = BUILT_PRODUCTS_DIR; }; 263CCF76276606670061B353 /* day12.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day12.swift; sourceTree = ""; }; + 263D3E2B276DDA5400B0AC5B /* Day17 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day17; sourceTree = BUILT_PRODUCTS_DIR; }; + 263D3E32276DDA6600B0AC5B /* day17.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day17.swift; sourceTree = ""; }; 2651128F2767D15A009B7607 /* Day13 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day13; sourceTree = BUILT_PRODUCTS_DIR; }; 265112962767D16D009B7607 /* day13.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day13.swift; sourceTree = ""; }; 269BE5B42762958800871C85 /* Day8Part2 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day8Part2; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -209,6 +222,13 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 263D3E28276DDA5400B0AC5B /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; 2651128C2767D15A009B7607 /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; @@ -243,6 +263,7 @@ 263BA594275E974800839C92 = { isa = PBXGroup; children = ( + 263D3E32276DDA6600B0AC5B /* day17.swift */, 26EA5DFF276BA680003E0305 /* day16.swift */, 26155467276A6D0A00374D18 /* day15.swift */, 26155459276A6C2C00374D18 /* day14.swift */, @@ -273,6 +294,7 @@ 26155452276A6C1C00374D18 /* Day14 */, 26155460276A6CF700374D18 /* Day15 */, 26EA5DF7276BA668003E0305 /* Day16 */, + 263D3E2B276DDA5400B0AC5B /* Day17 */, ); name = Products; sourceTree = ""; @@ -399,6 +421,23 @@ productReference = 263CCF6F2766065B0061B353 /* Day12 */; productType = "com.apple.product-type.tool"; }; + 263D3E2A276DDA5400B0AC5B /* Day17 */ = { + isa = PBXNativeTarget; + buildConfigurationList = 263D3E31276DDA5400B0AC5B /* Build configuration list for PBXNativeTarget "Day17" */; + buildPhases = ( + 263D3E27276DDA5400B0AC5B /* Sources */, + 263D3E28276DDA5400B0AC5B /* Frameworks */, + 263D3E29276DDA5400B0AC5B /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = Day17; + productName = Day17; + productReference = 263D3E2B276DDA5400B0AC5B /* Day17 */; + productType = "com.apple.product-type.tool"; + }; 2651128E2767D15A009B7607 /* Day13 */ = { isa = PBXNativeTarget; buildConfigurationList = 265112952767D15A009B7607 /* Build configuration list for PBXNativeTarget "Day13" */; @@ -504,6 +543,10 @@ 263CCF6E2766065B0061B353 = { CreatedOnToolsVersion = 13.1; }; + 263D3E2A276DDA5400B0AC5B = { + CreatedOnToolsVersion = 13.2; + LastSwiftMigration = 1320; + }; 2651128E2767D15A009B7607 = { CreatedOnToolsVersion = 13.1; LastSwiftMigration = 1310; @@ -544,6 +587,7 @@ 26155451276A6C1C00374D18 /* Day14 */, 2615545F276A6CF700374D18 /* Day15 */, 26EA5DF6276BA668003E0305 /* Day16 */, + 263D3E2A276DDA5400B0AC5B /* Day17 */, ); }; /* End PBXProject section */ @@ -612,6 +656,15 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 263D3E27276DDA5400B0AC5B /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 263D3E34276DDA7000B0AC5B /* common.swift in Sources */, + 263D3E33276DDA6600B0AC5B /* day17.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; 2651128B2767D15A009B7607 /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; @@ -985,6 +1038,43 @@ }; name = Release; }; + 263D3E2F276DDA5400B0AC5B /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + "@loader_path/../Frameworks", + ); + MACOSX_DEPLOYMENT_TARGET = 12.1; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 5.0; + }; + name = Debug; + }; + 263D3E30276DDA5400B0AC5B /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + "@loader_path/../Frameworks", + ); + MACOSX_DEPLOYMENT_TARGET = 12.1; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Release; + }; 265112932767D15A009B7607 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { @@ -1176,6 +1266,15 @@ defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; + 263D3E31276DDA5400B0AC5B /* Build configuration list for PBXNativeTarget "Day17" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 263D3E2F276DDA5400B0AC5B /* Debug */, + 263D3E30276DDA5400B0AC5B /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; 265112952767D15A009B7607 /* Build configuration list for PBXNativeTarget "Day13" */ = { isa = XCConfigurationList; buildConfigurations = ( diff --git a/day17.swift b/day17.swift new file mode 100644 index 0000000..9d17df7 --- /dev/null +++ b/day17.swift @@ -0,0 +1,71 @@ +@main +struct Day17: Puzzle { + func run() { + var maxHeight: Int = 0 + var count = 0 + + for speedX in 1..<300 { + for speedY in -600..<300 { + if case .hit(let height) = run(vx: speedX, vy: speedY) { + maxHeight = max(height, maxHeight) + count += 1 + } + } + } + + print("Part 1:", maxHeight) + print("Part 2:", count) + } + + enum Result { + case tooFar + case tooShort + case hit(height: Int) + } + + struct Vector: Hashable { + var x: Int + var y: Int + } + + func run(vx: Int, vy: Int) -> Result { + var state = State(vx: vx, vy: vy) + while state.x <= xMax && state.y >= yMin { + state.step() + if xMin...xMax ~= state.x && yMin...yMax ~= state.y { + return .hit(height: state.maxHeight) + } + } + + if state.x > xMax { + return .tooFar + } + + return .tooShort + } + + struct State { + var vx: Int + var vy: Int + var x: Int = 0 + var y: Int = 0 + var maxHeight = 0 + + mutating func step() { + if y > maxHeight { + maxHeight = y + } + + x += vx + y += vy + vx -= vx.signum() + vy -= 1 + } + } + + // target area: x=138..184, y=-125..-71 + let xMin = 138 + let xMax = 184 + let yMin = -125 + let yMax = -71 +}