From 677d88923b5c3a5389629997b0c1c468d3df4901 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Thu, 17 Dec 2020 07:40:19 +0100 Subject: [PATCH] Day 17 (part2) --- AdventOfCode2020.xcodeproj/project.pbxproj | 95 +++++++++++++++++++- day17/main.swift | 100 +++++++++++++++++++++ 2 files changed, 194 insertions(+), 1 deletion(-) create mode 100644 day17/main.swift diff --git a/AdventOfCode2020.xcodeproj/project.pbxproj b/AdventOfCode2020.xcodeproj/project.pbxproj index 57110f0..e75ad49 100644 --- a/AdventOfCode2020.xcodeproj/project.pbxproj +++ b/AdventOfCode2020.xcodeproj/project.pbxproj @@ -17,6 +17,9 @@ 268D956A257A07B80030EC4D /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 268D9569257A07B80030EC4D /* main.swift */; }; 268D9579257B52290030EC4D /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 268D9578257B52290030EC4D /* main.swift */; }; 268D9589257CB8DA0030EC4D /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 268D9588257CB8DA0030EC4D /* main.swift */; }; + 26BD8865258B2D5300E92A8E /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26BD8864258B2D5300E92A8E /* main.swift */; }; + 26BD887A258B2D5A00E92A8E /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26E2ACE5257ED09000702405 /* Extensions.swift */; }; + 26BD887B258B2D5A00E92A8E /* LoadData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26E2AD3A2581713A00702405 /* LoadData.swift */; }; 26E2ACD8257ECFFA00702405 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26E2ACD7257ECFFA00702405 /* main.swift */; }; 26E2ACE6257ED09000702405 /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26E2ACE5257ED09000702405 /* Extensions.swift */; }; 26E2ACE7257ED09000702405 /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26E2ACE5257ED09000702405 /* Extensions.swift */; }; @@ -130,6 +133,15 @@ ); runOnlyForDeploymentPostprocessing = 1; }; + 26BD8860258B2D5300E92A8E /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; 26E2ACD3257ECFFA00702405 /* CopyFiles */ = { isa = PBXCopyFilesBuildPhase; buildActionMask = 2147483647; @@ -221,6 +233,8 @@ 268D9578257B52290030EC4D /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; 268D9586257CB8DA0030EC4D /* day6 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = day6; sourceTree = BUILT_PRODUCTS_DIR; }; 268D9588257CB8DA0030EC4D /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; + 26BD8862258B2D5300E92A8E /* day17 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = day17; sourceTree = BUILT_PRODUCTS_DIR; }; + 26BD8864258B2D5300E92A8E /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; 26E2ACD5257ECFFA00702405 /* day8 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = day8; sourceTree = BUILT_PRODUCTS_DIR; }; 26E2ACD7257ECFFA00702405 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; 26E2ACE5257ED09000702405 /* Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = ""; }; @@ -298,6 +312,13 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 26BD885F258B2D5300E92A8E /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; 26E2ACD2257ECFFA00702405 /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; @@ -393,6 +414,7 @@ 26E2ADE225873BBE00702405 /* day14 */, 26E2AE0B2587E07A00702405 /* day15 */, 261C554625895FB70035FEC0 /* day16 */, + 26BD8863258B2D5300E92A8E /* day17 */, 268D953A25781DD80030EC4D /* Products */, ); sourceTree = ""; @@ -416,6 +438,7 @@ 26E2ADE125873BBD00702405 /* day14 */, 26E2AE0A2587E07A00702405 /* day15 */, 261C554525895FB70035FEC0 /* day16 */, + 26BD8862258B2D5300E92A8E /* day17 */, ); name = Products; sourceTree = ""; @@ -468,6 +491,14 @@ path = day6; sourceTree = ""; }; + 26BD8863258B2D5300E92A8E /* day17 */ = { + isa = PBXGroup; + children = ( + 26BD8864258B2D5300E92A8E /* main.swift */, + ); + path = day17; + sourceTree = ""; + }; 26E2ACD6257ECFFA00702405 /* day8 */ = { isa = PBXGroup; children = ( @@ -680,6 +711,23 @@ productReference = 268D9586257CB8DA0030EC4D /* day6 */; productType = "com.apple.product-type.tool"; }; + 26BD8861258B2D5300E92A8E /* day17 */ = { + isa = PBXNativeTarget; + buildConfigurationList = 26BD8868258B2D5300E92A8E /* Build configuration list for PBXNativeTarget "day17" */; + buildPhases = ( + 26BD885E258B2D5300E92A8E /* Sources */, + 26BD885F258B2D5300E92A8E /* Frameworks */, + 26BD8860258B2D5300E92A8E /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = day17; + productName = day17; + productReference = 26BD8862258B2D5300E92A8E /* day17 */; + productType = "com.apple.product-type.tool"; + }; 26E2ACD4257ECFFA00702405 /* day8 */ = { isa = PBXNativeTarget; buildConfigurationList = 26E2ACDB257ECFFA00702405 /* Build configuration list for PBXNativeTarget "day8" */; @@ -822,7 +870,7 @@ 268D953125781DD80030EC4D /* Project object */ = { isa = PBXProject; attributes = { - LastSwiftUpdateCheck = 1220; + LastSwiftUpdateCheck = 1230; LastUpgradeCheck = 1220; TargetAttributes = { 2617623E257DFD0800D00A66 = { @@ -849,6 +897,9 @@ 268D9585257CB8DA0030EC4D = { CreatedOnToolsVersion = 12.2; }; + 26BD8861258B2D5300E92A8E = { + CreatedOnToolsVersion = 12.3; + }; 26E2ACD4257ECFFA00702405 = { CreatedOnToolsVersion = 12.2; }; @@ -904,6 +955,7 @@ 26E2ADE025873BBD00702405 /* day14 */, 26E2AE092587E07A00702405 /* day15 */, 261C554425895FB70035FEC0 /* day16 */, + 26BD8861258B2D5300E92A8E /* day17 */, ); }; /* End PBXProject section */ @@ -989,6 +1041,16 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 26BD885E258B2D5300E92A8E /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 26BD8865258B2D5300E92A8E /* main.swift in Sources */, + 26BD887A258B2D5A00E92A8E /* Extensions.swift in Sources */, + 26BD887B258B2D5A00E92A8E /* LoadData.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; 26E2ACD1257ECFFA00702405 /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; @@ -1363,6 +1425,28 @@ }; name = Release; }; + 26BD8866258B2D5300E92A8E /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Debug; + }; + 26BD8867258B2D5300E92A8E /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Release; + }; 26E2ACD9257ECFFA00702405 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { @@ -1623,6 +1707,15 @@ defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; + 26BD8868258B2D5300E92A8E /* Build configuration list for PBXNativeTarget "day17" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 26BD8866258B2D5300E92A8E /* Debug */, + 26BD8867258B2D5300E92A8E /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; 26E2ACDB257ECFFA00702405 /* Build configuration list for PBXNativeTarget "day8" */ = { isa = XCConfigurationList; buildConfigurations = ( diff --git a/day17/main.swift b/day17/main.swift new file mode 100644 index 0000000..effc7b8 --- /dev/null +++ b/day17/main.swift @@ -0,0 +1,100 @@ +import Foundation + +let input = loadData(day: 17).lines() + +struct Coordinate: Hashable { + var x, y, z, w: Int +} + + + +struct Cube { + var cube: [Coordinate: Bool] = [:] + var minX = 0 + var maxX = 0 + var minY = 0 + var maxY = 0 + var minZ = 0 + var maxZ = 0 + var minW = 0 + var maxW = 0 + + func neighbors(_ c: Coordinate) -> Int { + var count = 0 + for xOffset in -1...1 { + for yOffset in -1...1 { + for zOffset in -1...1 { + for wOffset in -1 ... 1 { + guard !(xOffset == 0 && yOffset == 0 && zOffset == 0 && wOffset == 0) else { continue } + let current = Coordinate(x: c.x + xOffset, y: c.y + yOffset, z: c.z + zOffset, w: c.w + wOffset) + if let value = cube[current], value { + count += 1 + } + } + } + } + } + return count + } + + subscript(_ x: Int, _ y : Int, _ z : Int, _ w : Int ) -> Bool { + get { + cube[Coordinate(x: x, y: y, z: z, w: w)] ?? false + } + + set { + cube[Coordinate(x: x, y: y, z: z, w: w)] = newValue + if newValue { + minX = min(x, minX) + minY = min(y, minY) + minZ = min(z, minZ) + maxX = max(x, maxX) + maxY = max(y, maxY) + maxZ = max(z, maxZ) + minW = min(w, minW) + maxW = max(w, maxW) + } + } + } + +} + +var cube = Cube() + +func cycle() -> Cube { + var result = cube + + for x in cube.minX - 1 ... cube.maxX + 1 { + for y in cube.minY - 1 ... cube.maxY + 1 { + for z in cube.minZ - 1 ... cube.maxZ + 1 { + for w in cube.minW - 1 ... cube.maxW + 1 { + let n = cube.neighbors(Coordinate(x: x, y: y, z: z, w: w)) + if cube[x, y, z, w] { + if n < 2 || n > 3 { + result[x, y, z, w] = false + } + } else { + if n == 3 { + result[x, y, z, w] = true + } + } + } + } + } + } + + return result +} + + +for (y, line) in input.enumerated() { + for (x, char) in line.enumerated() { + cube[x, y, 0, 0] = char == "#" + } +} + +for _ in 0..<6 { + cube = cycle() +} + +print(cube.cube.values.reduce(0, { $0 + ($1 ? 1 : 0) }))