From 65b3920d515d5f45e0ddc2725fa35d5d5f5c6533 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Tue, 21 Dec 2021 10:53:36 +0100 Subject: [PATCH] Day 21 Part 1 --- AoC21.xcodeproj/project.pbxproj | 85 +++++++++++++++++++++++++++++++++ day21.swift | 55 +++++++++++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 day21.swift diff --git a/AoC21.xcodeproj/project.pbxproj b/AoC21.xcodeproj/project.pbxproj index ca5d4b9..069ed41 100644 --- a/AoC21.xcodeproj/project.pbxproj +++ b/AoC21.xcodeproj/project.pbxproj @@ -29,6 +29,8 @@ 2680ECD3276F240200CAB23C /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; 2680ECE02770A2FC00CAB23C /* day20.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2680ECDF2770A2FC00CAB23C /* day20.swift */; }; 2680ECE12770A30300CAB23C /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; + 2680ECEE2771D84100CAB23C /* day21.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2680ECE22771D82400CAB23C /* day21.swift */; }; + 2680ECEF2771D84100CAB23C /* 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 */; }; @@ -148,6 +150,15 @@ ); runOnlyForDeploymentPostprocessing = 1; }; + 2680ECE52771D82F00CAB23C /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; 269BE5B22762958800871C85 /* CopyFiles */ = { isa = PBXCopyFilesBuildPhase; buildActionMask = 2147483647; @@ -202,6 +213,8 @@ 2680ECD1276F23FE00CAB23C /* day19.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day19.swift; sourceTree = ""; }; 2680ECD82770A2E900CAB23C /* Day20 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day20; sourceTree = BUILT_PRODUCTS_DIR; }; 2680ECDF2770A2FC00CAB23C /* day20.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day20.swift; sourceTree = ""; }; + 2680ECE22771D82400CAB23C /* day21.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day21.swift; sourceTree = ""; }; + 2680ECE72771D82F00CAB23C /* Day21 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day21; sourceTree = BUILT_PRODUCTS_DIR; }; 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; }; @@ -296,6 +309,13 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 2680ECE42771D82F00CAB23C /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; 269BE5B12762958800871C85 /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; @@ -323,6 +343,7 @@ 263BA594275E974800839C92 = { isa = PBXGroup; children = ( + 2680ECE22771D82400CAB23C /* day21.swift */, 2680ECDF2770A2FC00CAB23C /* day20.swift */, 2680ECD1276F23FE00CAB23C /* day19.swift */, 263D3E41276E10AE00B0AC5B /* day18.swift */, @@ -361,6 +382,7 @@ 263D3E39276E109D00B0AC5B /* Day18 */, 2680ECCA276F23E800CAB23C /* Day19 */, 2680ECD82770A2E900CAB23C /* Day20 */, + 2680ECE72771D82F00CAB23C /* Day21 */, ); name = Products; sourceTree = ""; @@ -572,6 +594,23 @@ productReference = 2680ECD82770A2E900CAB23C /* Day20 */; productType = "com.apple.product-type.tool"; }; + 2680ECE62771D82F00CAB23C /* Day21 */ = { + isa = PBXNativeTarget; + buildConfigurationList = 2680ECEB2771D82F00CAB23C /* Build configuration list for PBXNativeTarget "Day21" */; + buildPhases = ( + 2680ECE32771D82F00CAB23C /* Sources */, + 2680ECE42771D82F00CAB23C /* Frameworks */, + 2680ECE52771D82F00CAB23C /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = Day21; + productName = Day21; + productReference = 2680ECE72771D82F00CAB23C /* Day21 */; + productType = "com.apple.product-type.tool"; + }; 269BE5B32762958800871C85 /* Day8Part2 */ = { isa = PBXNativeTarget; buildConfigurationList = 269BE5BA2762958800871C85 /* Build configuration list for PBXNativeTarget "Day8Part2" */; @@ -679,6 +718,9 @@ CreatedOnToolsVersion = 13.2.1; LastSwiftMigration = 1320; }; + 2680ECE62771D82F00CAB23C = { + CreatedOnToolsVersion = 13.2.1; + }; 269BE5B32762958800871C85 = { CreatedOnToolsVersion = 13.1; LastSwiftMigration = 1310; @@ -719,6 +761,7 @@ 263D3E38276E109D00B0AC5B /* Day18 */, 2680ECC9276F23E800CAB23C /* Day19 */, 2680ECD72770A2E900CAB23C /* Day20 */, + 2680ECE62771D82F00CAB23C /* Day21 */, ); }; /* End PBXProject section */ @@ -832,6 +875,15 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 2680ECE32771D82F00CAB23C /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 2680ECEE2771D84100CAB23C /* day21.swift in Sources */, + 2680ECEF2771D84100CAB23C /* common.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; 269BE5B02762958800871C85 /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; @@ -1366,6 +1418,30 @@ }; name = Release; }; + 2680ECEC2771D82F00CAB23C /* 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; + }; + 2680ECED2771D82F00CAB23C /* 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; + }; 269BE5B82762958800871C85 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { @@ -1567,6 +1643,15 @@ defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; + 2680ECEB2771D82F00CAB23C /* Build configuration list for PBXNativeTarget "Day21" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 2680ECEC2771D82F00CAB23C /* Debug */, + 2680ECED2771D82F00CAB23C /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; 269BE5BA2762958800871C85 /* Build configuration list for PBXNativeTarget "Day8Part2" */ = { isa = XCConfigurationList; buildConfigurations = ( diff --git a/day21.swift b/day21.swift new file mode 100644 index 0000000..78d6bb5 --- /dev/null +++ b/day21.swift @@ -0,0 +1,55 @@ +@main +struct Day21: Puzzle { + func run() { + part1() + } + + func part1() { + var die = Die() + var player1 = Player(position: 6) + var player2 = Player(position: 7) + + while true { + if player1.move(die.roll3()) { + print("Part 1: Player 1 won. ", die.rolls * player2.score) + break + } + + if player2.move(die.roll3()) { + print("Part 1: Player 2 won. ", die.rolls * player1.score) + break + } + } + } + + struct Player { + var position: Int + var score: Int = 0 + + mutating func move(_ roll: Int) -> Bool { + position = 1 + (position + roll - 1) % 10 + score += position + return score >= 1000 + } + } + + struct Die { + + var rolls = 0 + var nextRoll = 1 + + mutating func roll() -> Int { + defer { + rolls += 1 + nextRoll += 1 + } + + return nextRoll + } + + mutating func roll3() -> Int { + roll() + roll() + roll() + } + } + +}