Day 17 (part2)

This commit is contained in:
Sven Weidauer 2020-12-17 07:40:19 +01:00
parent c815aeb942
commit 677d88923b
2 changed files with 194 additions and 1 deletions

View file

@ -17,6 +17,9 @@
268D956A257A07B80030EC4D /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 268D9569257A07B80030EC4D /* main.swift */; }; 268D956A257A07B80030EC4D /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 268D9569257A07B80030EC4D /* main.swift */; };
268D9579257B52290030EC4D /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 268D9578257B52290030EC4D /* main.swift */; }; 268D9579257B52290030EC4D /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 268D9578257B52290030EC4D /* main.swift */; };
268D9589257CB8DA0030EC4D /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 268D9588257CB8DA0030EC4D /* 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 */; }; 26E2ACD8257ECFFA00702405 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26E2ACD7257ECFFA00702405 /* main.swift */; };
26E2ACE6257ED09000702405 /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26E2ACE5257ED09000702405 /* Extensions.swift */; }; 26E2ACE6257ED09000702405 /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26E2ACE5257ED09000702405 /* Extensions.swift */; };
26E2ACE7257ED09000702405 /* 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; runOnlyForDeploymentPostprocessing = 1;
}; };
26BD8860258B2D5300E92A8E /* CopyFiles */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647;
dstPath = /usr/share/man/man1/;
dstSubfolderSpec = 0;
files = (
);
runOnlyForDeploymentPostprocessing = 1;
};
26E2ACD3257ECFFA00702405 /* CopyFiles */ = { 26E2ACD3257ECFFA00702405 /* CopyFiles */ = {
isa = PBXCopyFilesBuildPhase; isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647; buildActionMask = 2147483647;
@ -221,6 +233,8 @@
268D9578257B52290030EC4D /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; }; 268D9578257B52290030EC4D /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
268D9586257CB8DA0030EC4D /* day6 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = day6; sourceTree = BUILT_PRODUCTS_DIR; }; 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 = "<group>"; }; 268D9588257CB8DA0030EC4D /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
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 = "<group>"; };
26E2ACD5257ECFFA00702405 /* day8 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = day8; sourceTree = BUILT_PRODUCTS_DIR; }; 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 = "<group>"; }; 26E2ACD7257ECFFA00702405 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
26E2ACE5257ED09000702405 /* Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = "<group>"; }; 26E2ACE5257ED09000702405 /* Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = "<group>"; };
@ -298,6 +312,13 @@
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };
26BD885F258B2D5300E92A8E /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
26E2ACD2257ECFFA00702405 /* Frameworks */ = { 26E2ACD2257ECFFA00702405 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase; isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647; buildActionMask = 2147483647;
@ -393,6 +414,7 @@
26E2ADE225873BBE00702405 /* day14 */, 26E2ADE225873BBE00702405 /* day14 */,
26E2AE0B2587E07A00702405 /* day15 */, 26E2AE0B2587E07A00702405 /* day15 */,
261C554625895FB70035FEC0 /* day16 */, 261C554625895FB70035FEC0 /* day16 */,
26BD8863258B2D5300E92A8E /* day17 */,
268D953A25781DD80030EC4D /* Products */, 268D953A25781DD80030EC4D /* Products */,
); );
sourceTree = "<group>"; sourceTree = "<group>";
@ -416,6 +438,7 @@
26E2ADE125873BBD00702405 /* day14 */, 26E2ADE125873BBD00702405 /* day14 */,
26E2AE0A2587E07A00702405 /* day15 */, 26E2AE0A2587E07A00702405 /* day15 */,
261C554525895FB70035FEC0 /* day16 */, 261C554525895FB70035FEC0 /* day16 */,
26BD8862258B2D5300E92A8E /* day17 */,
); );
name = Products; name = Products;
sourceTree = "<group>"; sourceTree = "<group>";
@ -468,6 +491,14 @@
path = day6; path = day6;
sourceTree = "<group>"; sourceTree = "<group>";
}; };
26BD8863258B2D5300E92A8E /* day17 */ = {
isa = PBXGroup;
children = (
26BD8864258B2D5300E92A8E /* main.swift */,
);
path = day17;
sourceTree = "<group>";
};
26E2ACD6257ECFFA00702405 /* day8 */ = { 26E2ACD6257ECFFA00702405 /* day8 */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
@ -680,6 +711,23 @@
productReference = 268D9586257CB8DA0030EC4D /* day6 */; productReference = 268D9586257CB8DA0030EC4D /* day6 */;
productType = "com.apple.product-type.tool"; 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 */ = { 26E2ACD4257ECFFA00702405 /* day8 */ = {
isa = PBXNativeTarget; isa = PBXNativeTarget;
buildConfigurationList = 26E2ACDB257ECFFA00702405 /* Build configuration list for PBXNativeTarget "day8" */; buildConfigurationList = 26E2ACDB257ECFFA00702405 /* Build configuration list for PBXNativeTarget "day8" */;
@ -822,7 +870,7 @@
268D953125781DD80030EC4D /* Project object */ = { 268D953125781DD80030EC4D /* Project object */ = {
isa = PBXProject; isa = PBXProject;
attributes = { attributes = {
LastSwiftUpdateCheck = 1220; LastSwiftUpdateCheck = 1230;
LastUpgradeCheck = 1220; LastUpgradeCheck = 1220;
TargetAttributes = { TargetAttributes = {
2617623E257DFD0800D00A66 = { 2617623E257DFD0800D00A66 = {
@ -849,6 +897,9 @@
268D9585257CB8DA0030EC4D = { 268D9585257CB8DA0030EC4D = {
CreatedOnToolsVersion = 12.2; CreatedOnToolsVersion = 12.2;
}; };
26BD8861258B2D5300E92A8E = {
CreatedOnToolsVersion = 12.3;
};
26E2ACD4257ECFFA00702405 = { 26E2ACD4257ECFFA00702405 = {
CreatedOnToolsVersion = 12.2; CreatedOnToolsVersion = 12.2;
}; };
@ -904,6 +955,7 @@
26E2ADE025873BBD00702405 /* day14 */, 26E2ADE025873BBD00702405 /* day14 */,
26E2AE092587E07A00702405 /* day15 */, 26E2AE092587E07A00702405 /* day15 */,
261C554425895FB70035FEC0 /* day16 */, 261C554425895FB70035FEC0 /* day16 */,
26BD8861258B2D5300E92A8E /* day17 */,
); );
}; };
/* End PBXProject section */ /* End PBXProject section */
@ -989,6 +1041,16 @@
); );
runOnlyForDeploymentPostprocessing = 0; 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 */ = { 26E2ACD1257ECFFA00702405 /* Sources */ = {
isa = PBXSourcesBuildPhase; isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647; buildActionMask = 2147483647;
@ -1363,6 +1425,28 @@
}; };
name = Release; 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 */ = { 26E2ACD9257ECFFA00702405 /* Debug */ = {
isa = XCBuildConfiguration; isa = XCBuildConfiguration;
buildSettings = { buildSettings = {
@ -1623,6 +1707,15 @@
defaultConfigurationIsVisible = 0; defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release; 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" */ = { 26E2ACDB257ECFFA00702405 /* Build configuration list for PBXNativeTarget "day8" */ = {
isa = XCConfigurationList; isa = XCConfigurationList;
buildConfigurations = ( buildConfigurations = (

100
day17/main.swift Normal file
View file

@ -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) }))