-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.DS_Store | ||
/.build | ||
/Packages | ||
/*.xcodeproj | ||
xcuserdata/ |
7 changes: 7 additions & 0 deletions
7
day23/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// swift-tools-version:5.3 | ||
// The swift-tools-version declares the minimum version of Swift required to build this package. | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "day23", | ||
dependencies: [ | ||
// Dependencies declare other packages that this package depends on. | ||
// .package(url: /* package url */, from: "1.0.0"), | ||
], | ||
targets: [ | ||
// Targets are the basic building blocks of a package. A target can define a module or a test suite. | ||
// Targets can depend on other targets in this package, and on products in packages this package depends on. | ||
.target( | ||
name: "day23", | ||
dependencies: []), | ||
.testTarget( | ||
name: "day23Tests", | ||
dependencies: ["day23"]), | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# day23 | ||
|
||
A description of this package. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
|
||
import Foundation | ||
|
||
struct FixedSizeBuffer { | ||
var buffer: [Int] | ||
private var currentIndex = 0 | ||
|
||
init(_ values: [Int]) { | ||
buffer = values | ||
} | ||
|
||
func getNextThree() -> [Int] { | ||
[buffer[(currentIndex + 1) % buffer.count], | ||
buffer[(currentIndex + 2) % buffer.count], | ||
buffer[(currentIndex + 3) % buffer.count] | ||
] | ||
} | ||
|
||
func findNextValue(moving: [Int]) -> Int { | ||
var candidate = buffer[currentIndex] | ||
repeat { | ||
if candidate == 1 { | ||
candidate = 9 | ||
} else { | ||
candidate -= 1 | ||
} | ||
} while moving.contains(candidate) | ||
return candidate | ||
} | ||
|
||
mutating func moveThree() { | ||
let moving = getNextThree() | ||
let destValue = findNextValue(moving: moving) | ||
|
||
var destIdx = (currentIndex + 1) % buffer.count | ||
var srcIdx = (currentIndex + 4) % buffer.count | ||
var movedDest = false | ||
while !movedDest { | ||
buffer[destIdx] = buffer[srcIdx] | ||
movedDest = buffer[destIdx] == destValue | ||
destIdx = (destIdx + 1) % buffer.count | ||
srcIdx = (srcIdx + 1) % buffer.count | ||
} | ||
for i in 0..<moving.count { | ||
buffer[(destIdx + i) % buffer.count] = moving[i] | ||
} | ||
currentIndex = (currentIndex + 1) % buffer.count | ||
} | ||
|
||
func printLabelValues() { | ||
let oneIdx = buffer.firstIndex(of: 1)! | ||
for i in 1..<buffer.count { | ||
print(buffer[(oneIdx + i) % buffer.count], terminator: "") | ||
} | ||
} | ||
} | ||
|
||
let input = "326519478" | ||
//let input = "389125467" // test input | ||
|
||
var values = FixedSizeBuffer(input.compactMap { Int(String($0)) }) | ||
for _ in 0..<100 { | ||
values.moveThree() | ||
} | ||
|
||
values.printLabelValues() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import XCTest | ||
|
||
import day23Tests | ||
|
||
var tests = [XCTestCaseEntry]() | ||
tests += day23Tests.allTests() | ||
XCTMain(tests) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import XCTest | ||
|
||
#if !canImport(ObjectiveC) | ||
public func allTests() -> [XCTestCaseEntry] { | ||
return [ | ||
testCase(day23Tests.allTests), | ||
] | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import XCTest | ||
import class Foundation.Bundle | ||
|
||
final class day23Tests: XCTestCase { | ||
func testExample() throws { | ||
// This is an example of a functional test case. | ||
// Use XCTAssert and related functions to verify your tests produce the correct | ||
// results. | ||
|
||
// Some of the APIs that we use below are available in macOS 10.13 and above. | ||
guard #available(macOS 10.13, *) else { | ||
return | ||
} | ||
|
||
let fooBinary = productsDirectory.appendingPathComponent("day23") | ||
|
||
let process = Process() | ||
process.executableURL = fooBinary | ||
|
||
let pipe = Pipe() | ||
process.standardOutput = pipe | ||
|
||
try process.run() | ||
process.waitUntilExit() | ||
|
||
let data = pipe.fileHandleForReading.readDataToEndOfFile() | ||
let output = String(data: data, encoding: .utf8) | ||
|
||
XCTAssertEqual(output, "Hello, world!\n") | ||
} | ||
|
||
/// Returns path to the built products directory. | ||
var productsDirectory: URL { | ||
#if os(macOS) | ||
for bundle in Bundle.allBundles where bundle.bundlePath.hasSuffix(".xctest") { | ||
return bundle.bundleURL.deletingLastPathComponent() | ||
} | ||
fatalError("couldn't find the products directory") | ||
#else | ||
return Bundle.main.bundleURL | ||
#endif | ||
} | ||
|
||
static var allTests = [ | ||
("testExample", testExample), | ||
] | ||
} |