From e5f2ac72bd1933e4aa693099858e02d54892f192 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Mon, 13 Dec 2021 20:46:05 +0100 Subject: [PATCH] Day 13 --- AoC21.xcodeproj/project.pbxproj | 97 ++++ day13.swift | 931 ++++++++++++++++++++++++++++++++ 2 files changed, 1028 insertions(+) create mode 100644 day13.swift diff --git a/AoC21.xcodeproj/project.pbxproj b/AoC21.xcodeproj/project.pbxproj index 02aec33..9d36372 100644 --- a/AoC21.xcodeproj/project.pbxproj +++ b/AoC21.xcodeproj/project.pbxproj @@ -15,6 +15,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 */; }; + 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 */; }; 269BE5CA2762A05300871C85 /* day9.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5C82762A04F00871C85 /* day9.swift */; }; 269BE5CC2762A08800871C85 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; @@ -69,6 +71,15 @@ ); runOnlyForDeploymentPostprocessing = 1; }; + 2651128D2767D15A009B7607 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; 269BE5B22762958800871C85 /* CopyFiles */ = { isa = PBXCopyFilesBuildPhase; buildActionMask = 2147483647; @@ -100,6 +111,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 = ""; }; + 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; }; 269BE5BB2762959B00871C85 /* day8part2.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day8part2.swift; sourceTree = ""; }; 269BE5C12762A03F00871C85 /* Day9 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day9; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -143,6 +156,13 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 2651128C2767D15A009B7607 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; 269BE5B12762958800871C85 /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; @@ -163,6 +183,7 @@ 263BA594275E974800839C92 = { isa = PBXGroup; children = ( + 265112962767D16D009B7607 /* day13.swift */, 263CCF76276606670061B353 /* day12.swift */, 263CCF69276606010061B353 /* day11.swift */, 261F317E2763DACC0018D49C /* day10.swift */, @@ -185,6 +206,7 @@ 261F31772763DAB60018D49C /* Day10 */, 263CCF61276605320061B353 /* Day11 */, 263CCF6F2766065B0061B353 /* Day12 */, + 2651128F2767D15A009B7607 /* Day13 */, ); name = Products; sourceTree = ""; @@ -277,6 +299,23 @@ productReference = 263CCF6F2766065B0061B353 /* Day12 */; productType = "com.apple.product-type.tool"; }; + 2651128E2767D15A009B7607 /* Day13 */ = { + isa = PBXNativeTarget; + buildConfigurationList = 265112952767D15A009B7607 /* Build configuration list for PBXNativeTarget "Day13" */; + buildPhases = ( + 2651128B2767D15A009B7607 /* Sources */, + 2651128C2767D15A009B7607 /* Frameworks */, + 2651128D2767D15A009B7607 /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = Day13; + productName = Day13; + productReference = 2651128F2767D15A009B7607 /* Day13 */; + productType = "com.apple.product-type.tool"; + }; 269BE5B32762958800871C85 /* Day8Part2 */ = { isa = PBXNativeTarget; buildConfigurationList = 269BE5BA2762958800871C85 /* Build configuration list for PBXNativeTarget "Day8Part2" */; @@ -339,6 +378,10 @@ 263CCF6E2766065B0061B353 = { CreatedOnToolsVersion = 13.1; }; + 2651128E2767D15A009B7607 = { + CreatedOnToolsVersion = 13.1; + LastSwiftMigration = 1310; + }; 269BE5B32762958800871C85 = { CreatedOnToolsVersion = 13.1; LastSwiftMigration = 1310; @@ -368,6 +411,7 @@ 261F31762763DAB60018D49C /* Day10 */, 263CCF60276605320061B353 /* Day11 */, 263CCF6E2766065B0061B353 /* Day12 */, + 2651128E2767D15A009B7607 /* Day13 */, ); }; /* End PBXProject section */ @@ -418,6 +462,15 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 2651128B2767D15A009B7607 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 265112982767D171009B7607 /* common.swift in Sources */, + 265112972767D16D009B7607 /* day13.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; 269BE5B02762958800871C85 /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; @@ -703,6 +756,41 @@ }; name = Release; }; + 265112932767D15A009B7607 /* 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", + ); + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 5.0; + }; + name = Debug; + }; + 265112942767D15A009B7607 /* 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", + ); + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Release; + }; 269BE5B82762958800871C85 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { @@ -817,6 +905,15 @@ defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; + 265112952767D15A009B7607 /* Build configuration list for PBXNativeTarget "Day13" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 265112932767D15A009B7607 /* Debug */, + 265112942767D15A009B7607 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; 269BE5BA2762958800871C85 /* Build configuration list for PBXNativeTarget "Day8Part2" */ = { isa = XCConfigurationList; buildConfigurations = ( diff --git a/day13.swift b/day13.swift new file mode 100644 index 0000000..4c4869f --- /dev/null +++ b/day13.swift @@ -0,0 +1,931 @@ +@main +struct Day13: Puzzle { + + var maxX: Int = 0 + var maxY: Int = 0 + + mutating func run() { + maxX = dots.map(\.x).max()! + maxY = dots.map(\.y).max()! + + for (pos, fold) in folds.enumerated() { + self.fold(fold) + if (pos == 0) { + print("Part 1:", Set(dots).count) + } + } + + print("\n\nPart 2:\n") + + for y in 0..= x { + print(String(repeating: ".", count: dotX - x) + "#", terminator: "") + x = dotX + 1 + } + print(String(repeating: ".", count: maxX - x)) + } + } + + mutating func fold(_ fold: Fold) { + switch fold { + case .x(let axis): + self.fold(at: axis, keyPath: \.x, max: maxX) + maxX = axis + + case .y(let axis): + self.fold(at: axis, keyPath: \.y, max: maxY) + maxY = axis + } + } + + mutating func fold(at limit: Int, keyPath: WritableKeyPath, max: Int) { + let mid = dots.partition { dot in dot[keyPath: keyPath] > limit } + for index in mid..