Day 12 Part 1

This commit is contained in:
Sven Weidauer 2021-12-12 12:11:27 +01:00
parent 1f20585622
commit 76784119c0
2 changed files with 182 additions and 0 deletions

View file

@ -13,6 +13,8 @@
263BA5C0275EA5BD00839C92 /* day6.swift in Sources */ = {isa = PBXBuildFile; fileRef = 263BA5BF275EA5BD00839C92 /* day6.swift */; };
263CCF68276605F40061B353 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; };
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 */; };
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 */; };
@ -58,6 +60,15 @@
);
runOnlyForDeploymentPostprocessing = 1;
};
263CCF6D2766065B0061B353 /* CopyFiles */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647;
dstPath = /usr/share/man/man1/;
dstSubfolderSpec = 0;
files = (
);
runOnlyForDeploymentPostprocessing = 1;
};
269BE5B22762958800871C85 /* CopyFiles */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647;
@ -87,6 +98,8 @@
263BA5BF275EA5BD00839C92 /* day6.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day6.swift; sourceTree = "<group>"; };
263CCF61276605320061B353 /* Day11 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day11; sourceTree = BUILT_PRODUCTS_DIR; };
263CCF69276606010061B353 /* day11.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = day11.swift; sourceTree = "<group>"; };
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 = "<group>"; };
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 = "<group>"; };
269BE5C12762A03F00871C85 /* Day9 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day9; sourceTree = BUILT_PRODUCTS_DIR; };
@ -123,6 +136,13 @@
);
runOnlyForDeploymentPostprocessing = 0;
};
263CCF6C2766065B0061B353 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
269BE5B12762958800871C85 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
@ -143,6 +163,7 @@
263BA594275E974800839C92 = {
isa = PBXGroup;
children = (
263CCF76276606670061B353 /* day12.swift */,
263CCF69276606010061B353 /* day11.swift */,
261F317E2763DACC0018D49C /* day10.swift */,
269BE5CB2762A08800871C85 /* common.swift */,
@ -163,6 +184,7 @@
269BE5C12762A03F00871C85 /* Day9 */,
261F31772763DAB60018D49C /* Day10 */,
263CCF61276605320061B353 /* Day11 */,
263CCF6F2766065B0061B353 /* Day12 */,
);
name = Products;
sourceTree = "<group>";
@ -238,6 +260,23 @@
productReference = 263CCF61276605320061B353 /* Day11 */;
productType = "com.apple.product-type.tool";
};
263CCF6E2766065B0061B353 /* Day12 */ = {
isa = PBXNativeTarget;
buildConfigurationList = 263CCF732766065B0061B353 /* Build configuration list for PBXNativeTarget "Day12" */;
buildPhases = (
263CCF6B2766065B0061B353 /* Sources */,
263CCF6C2766065B0061B353 /* Frameworks */,
263CCF6D2766065B0061B353 /* CopyFiles */,
);
buildRules = (
);
dependencies = (
);
name = Day12;
productName = Day12;
productReference = 263CCF6F2766065B0061B353 /* Day12 */;
productType = "com.apple.product-type.tool";
};
269BE5B32762958800871C85 /* Day8Part2 */ = {
isa = PBXNativeTarget;
buildConfigurationList = 269BE5BA2762958800871C85 /* Build configuration list for PBXNativeTarget "Day8Part2" */;
@ -297,6 +336,9 @@
263CCF60276605320061B353 = {
CreatedOnToolsVersion = 13.1;
};
263CCF6E2766065B0061B353 = {
CreatedOnToolsVersion = 13.1;
};
269BE5B32762958800871C85 = {
CreatedOnToolsVersion = 13.1;
LastSwiftMigration = 1310;
@ -325,6 +367,7 @@
269BE5C02762A03F00871C85 /* Day9 */,
261F31762763DAB60018D49C /* Day10 */,
263CCF60276605320061B353 /* Day11 */,
263CCF6E2766065B0061B353 /* Day12 */,
);
};
/* End PBXProject section */
@ -366,6 +409,15 @@
);
runOnlyForDeploymentPostprocessing = 0;
};
263CCF6B2766065B0061B353 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
263CCF79276606730061B353 /* common.swift in Sources */,
263CCF782766066D0061B353 /* day12.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
269BE5B02762958800871C85 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
@ -629,6 +681,28 @@
};
name = Release;
};
263CCF742766065B0061B353 /* 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;
};
263CCF752766065B0061B353 /* 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;
};
269BE5B82762958800871C85 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
@ -734,6 +808,15 @@
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
263CCF732766065B0061B353 /* Build configuration list for PBXNativeTarget "Day12" */ = {
isa = XCConfigurationList;
buildConfigurations = (
263CCF742766065B0061B353 /* Debug */,
263CCF752766065B0061B353 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
269BE5BA2762958800871C85 /* Build configuration list for PBXNativeTarget "Day8Part2" */ = {
isa = XCConfigurationList;
buildConfigurations = (

99
day12.swift Normal file
View file

@ -0,0 +1,99 @@
@main
struct Day12: Puzzle {
func run() {
let rooms = input.reduce(into: Set<String>()) { partialResult, pair in
partialResult.insert(pair.0)
partialResult.insert(pair.1)
}.sorted()
let small = Set(rooms.enumerated().compactMap { $0.element.first!.isLowercase ? $0.offset : nil })
let matrix = buildMatrix(rooms, small: small)
let startIndex = rooms.firstIndex(of: "start")!
let endIndex = rooms.firstIndex(of: "end")!
let paths = matrix.findPaths(from: startIndex, to: endIndex, continuing: Path())
print("total paths", paths.count)
}
struct Path {
var rooms: [Int] = []
}
struct Matrix {
var data: [Bool]
let size: Int
let small: Set<Int>
func neighbors(of index: Int) -> [Int] {
let slice = data[index * size ..< (index + 1) * size]
return slice.enumerated().compactMap { $0.element ? $0.offset : nil }
}
func findPaths(from: Int, to: Int, continuing: Path) -> [Path] {
guard from != to else {
return [continuing]
}
var current = continuing
current.rooms.append(from)
var result: [Path] = []
for next in neighbors(of: from) {
if small.contains(next) && current.rooms.contains(next) {
continue
}
let nextPaths = findPaths(from: next, to: to, continuing: current)
result.append(contentsOf: nextPaths)
}
return result
}
}
func buildMatrix(_ rooms: [String], small: Set<Int>) -> Matrix {
let count = rooms.count
var matrix = [Bool](repeating: false, count: count * count)
for (from, to) in input {
let fromIndex = rooms.firstIndex(of: from)!
let toIndex = rooms.firstIndex(of: to)!
matrix[fromIndex + count * toIndex] = true
matrix[toIndex + count * fromIndex] = true
}
return Matrix(data: matrix, size: count, small: small)
}
let input: [(String, String)] = [
("rf", "RL"),
("rf", "wz"),
("wz", "RL"),
("AV", "mh"),
("end", "wz"),
("end", "dm"),
("wz", "gy"),
("wz", "dm"),
("cg", "AV"),
("rf", "AV"),
("rf", "gy"),
("end", "mh"),
("cg", "gy"),
("cg", "RL"),
("gy", "RL"),
("VI", "gy"),
("AV", "gy"),
("dm", "rf"),
("start", "cg"),
("start", "RL"),
("rf", "mh"),
("AV", "start"),
("qk", "mh"),
("wz", "mh"),
]
}