From c5129af5a161ba4775f183b2bff23ebaf006a892 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Thu, 16 Dec 2021 19:06:25 +0100 Subject: [PATCH] Day 16 --- AoC21.xcodeproj/project.pbxproj | 87 ++++++++++++++++++++- day16.swift | 131 ++++++++++++++++++++++++++++++++ 2 files changed, 217 insertions(+), 1 deletion(-) create mode 100644 day16.swift diff --git a/AoC21.xcodeproj/project.pbxproj b/AoC21.xcodeproj/project.pbxproj index 366484e..05ac05b 100644 --- a/AoC21.xcodeproj/project.pbxproj +++ b/AoC21.xcodeproj/project.pbxproj @@ -27,6 +27,8 @@ 269BE5CD2762A08800871C85 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; 269BE5CE2762A08800871C85 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; 269BE5CF2762A08800871C85 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; + 26EA5DFE276BA670003E0305 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; + 26EA5E00276BA680003E0305 /* day16.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26EA5DFF276BA680003E0305 /* day16.swift */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -120,6 +122,15 @@ ); runOnlyForDeploymentPostprocessing = 1; }; + 26EA5DF5276BA668003E0305 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ @@ -144,6 +155,8 @@ 269BE5C12762A03F00871C85 /* Day9 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day9; sourceTree = BUILT_PRODUCTS_DIR; }; 269BE5C82762A04F00871C85 /* day9.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day9.swift; sourceTree = ""; }; 269BE5CB2762A08800871C85 /* common.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = common.swift; sourceTree = ""; }; + 26EA5DF7276BA668003E0305 /* Day16 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day16; sourceTree = BUILT_PRODUCTS_DIR; }; + 26EA5DFF276BA680003E0305 /* day16.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day16.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -217,12 +230,20 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 26EA5DF4276BA668003E0305 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ 263BA594275E974800839C92 = { isa = PBXGroup; children = ( + 26EA5DFF276BA680003E0305 /* day16.swift */, 26155467276A6D0A00374D18 /* day15.swift */, 26155459276A6C2C00374D18 /* day14.swift */, 265112962767D16D009B7607 /* day13.swift */, @@ -251,6 +272,7 @@ 2651128F2767D15A009B7607 /* Day13 */, 26155452276A6C1C00374D18 /* Day14 */, 26155460276A6CF700374D18 /* Day15 */, + 26EA5DF7276BA668003E0305 /* Day16 */, ); name = Products; sourceTree = ""; @@ -428,6 +450,23 @@ productReference = 269BE5C12762A03F00871C85 /* Day9 */; productType = "com.apple.product-type.tool"; }; + 26EA5DF6276BA668003E0305 /* Day16 */ = { + isa = PBXNativeTarget; + buildConfigurationList = 26EA5DFD276BA668003E0305 /* Build configuration list for PBXNativeTarget "Day16" */; + buildPhases = ( + 26EA5DF3276BA668003E0305 /* Sources */, + 26EA5DF4276BA668003E0305 /* Frameworks */, + 26EA5DF5276BA668003E0305 /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = Day16; + productName = Day16; + productReference = 26EA5DF7276BA668003E0305 /* Day16 */; + productType = "com.apple.product-type.tool"; + }; /* End PBXNativeTarget section */ /* Begin PBXProject section */ @@ -435,7 +474,7 @@ isa = PBXProject; attributes = { BuildIndependentTargetsInParallel = 1; - LastSwiftUpdateCheck = 1310; + LastSwiftUpdateCheck = 1320; LastUpgradeCheck = 1310; TargetAttributes = { 26155451276A6C1C00374D18 = { @@ -475,6 +514,9 @@ 269BE5C02762A03F00871C85 = { CreatedOnToolsVersion = 13.1; }; + 26EA5DF6276BA668003E0305 = { + CreatedOnToolsVersion = 13.2; + }; }; }; buildConfigurationList = 263BA598275E974800839C92 /* Build configuration list for PBXProject "AoC21" */; @@ -500,6 +542,7 @@ 2651128E2767D15A009B7607 /* Day13 */, 26155451276A6C1C00374D18 /* Day14 */, 2615545F276A6CF700374D18 /* Day15 */, + 26EA5DF6276BA668003E0305 /* Day16 */, ); }; /* End PBXProject section */ @@ -595,6 +638,15 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 26EA5DF3276BA668003E0305 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 26EA5DFE276BA670003E0305 /* common.swift in Sources */, + 26EA5E00276BA680003E0305 /* day16.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; /* End PBXSourcesBuildPhase section */ /* Begin XCBuildConfiguration section */ @@ -1024,6 +1076,30 @@ }; name = Release; }; + 26EA5DFB276BA668003E0305 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + MACOSX_DEPLOYMENT_TARGET = 12.1; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Debug; + }; + 26EA5DFC276BA668003E0305 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + MACOSX_DEPLOYMENT_TARGET = 12.1; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Release; + }; /* End XCBuildConfiguration section */ /* Begin XCConfigurationList section */ @@ -1126,6 +1202,15 @@ defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; + 26EA5DFD276BA668003E0305 /* Build configuration list for PBXNativeTarget "Day16" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 26EA5DFB276BA668003E0305 /* Debug */, + 26EA5DFC276BA668003E0305 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; /* End XCConfigurationList section */ }; rootObject = 263BA595275E974800839C92 /* Project object */; diff --git a/day16.swift b/day16.swift new file mode 100644 index 0000000..7bf4ac2 --- /dev/null +++ b/day16.swift @@ -0,0 +1,131 @@ +@main +struct Day16: Puzzle { + mutating func run() { + var start = Cursor.start + + let part2 = packet(at: &start) + print("Part 1:", versionSum) + print("Part 2:", part2) + } + + var versionSum = 0 + + mutating func packet(at cursor: inout Cursor) -> UInt64 { + let version = getBits(at: &cursor, count: 3) + let type = getBits(at: &cursor, count: 3) + + versionSum += Int(version) + + if type == 4 { + return literal(at: &cursor) + } + + let subPackets = operatorPacket(at: &cursor) + + switch type { + case 0: return subPackets.reduce(0, +) + case 1: return subPackets.reduce(1, *) + case 2: return subPackets.min()! + case 3: return subPackets.max()! + case 5: return subPackets[0] > subPackets[1] ? 1 : 0 + case 6: return subPackets[0] < subPackets[1] ? 1 : 0 + case 7: return subPackets[0] == subPackets[1] ? 1 : 0 + default: fatalError("Unknown packet type \(type)") + } + + } + + func literal(at cursor: inout Cursor) -> UInt64 { + var result: UInt64 = 0 + while getBit(at: &cursor) { + result = result << 4 | getBits(at: &cursor, count: 4) + } + result = result << 4 | getBits(at: &cursor, count: 4) + return result + } + + mutating func operatorPacket(at cursor: inout Cursor) -> [UInt64] { + let type = getBit(at: &cursor) + var result: [UInt64] = [] + if type { + let count = getBits(at: &cursor, count: 11) + for _ in 0.. Cursor { + var result = self + result.skip(bits) + return result + } + + var bitPos: Int { + offset * 4 + bit + } + + static func < (lhs: Cursor, rhs: Cursor) -> Bool { + lhs.bitPos < rhs.bitPos + } + } + + func getBit(at cursor: inout Cursor) -> Bool { + getBits(at: &cursor, count: 1) == 1 + } + + func getBitRange(_ x: Int, start: Int, count: Int) -> UInt64 { + let mask = UInt64((1 << count) - 1) + let shift = 4 - count - start + precondition(shift >= 0) + + return UInt64(x) >> shift & mask + } + + func getBits(at cursor: inout Cursor, count: Int) -> UInt64 { + var remaining = count + var result: UInt64 = 0 + + while remaining > 0 { + let take = min(cursor.remainingBits, remaining) + let bits = getBitRange(input[cursor.offset], start: cursor.bit, count: take) + + result = result << take | bits + remaining -= take + + cursor.bit += take + if cursor.bit == 4 { + cursor.bit = 0 + cursor.offset += 1 + } + } + + return result + } + + let input = """ +E20D79005573F71DA0054E48527EF97D3004653BB1FC006867A8B1371AC49C801039171941340066E6B99A6A58B8110088BA008CE6F7893D4E6F7893DCDCFDB9D6CBC4026FE8026200DC7D84B1C00010A89507E3CCEE37B592014D3C01491B6697A83CB4F59E5E7FFA5CC66D4BC6F05D3004E6BB742B004E7E6B3375A46CF91D8C027911797589E17920F4009BE72DA8D2E4523DCEE86A8018C4AD3C7F2D2D02C5B9FF53366E3004658DB0012A963891D168801D08480485B005C0010A883116308002171AA24C679E0394EB898023331E60AB401294D98CA6CD8C01D9B349E0A99363003E655D40289CBDBB2F55D25E53ECAF14D9ABBB4CC726F038C011B0044401987D0BE0C00021B04E2546499DE824C015B004A7755B570013F2DD8627C65C02186F2996E9CCD04E5718C5CBCC016B004A4F61B27B0D9B8633F9344D57B0C1D3805537ADFA21F231C6EC9F3D3089FF7CD25E5941200C96801F191C77091238EE13A704A7CCC802B3B00567F192296259ABD9C400282915B9F6E98879823046C0010C626C966A19351EE27DE86C8E6968F2BE3D2008EE540FC01196989CD9410055725480D60025737BA1547D700727B9A89B444971830070401F8D70BA3B8803F16A3FC2D00043621C3B8A733C8BD880212BCDEE9D34929164D5CB08032594E5E1D25C0055E5B771E966783240220CD19E802E200F4588450BC401A8FB14E0A1805B36F3243B2833247536B70BDC00A60348880C7730039400B402A91009F650028C00E2020918077610021C00C1002D80512601188803B4000C148025010036727EE5AD6B445CC011E00B825E14F4BBF5F97853D2EFD6256F8FFE9F3B001420C01A88915E259002191EE2F4392004323E44A8B4C0069CEF34D304C001AB94379D149BD904507004A6D466B618402477802E200D47383719C0010F8A507A294CC9C90024A967C9995EE2933BA840 +""".compactMap { $0.hexDigitValue } +}