Skip to content

Commit

Permalink
day 23 part 1 working
Browse files Browse the repository at this point in the history
  • Loading branch information
2bjake committed Dec 23, 2020
1 parent 779debb commit b8e7d36
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 0 deletions.
5 changes: 5 additions & 0 deletions day23/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
xcuserdata/

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions day23/Package.swift
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"]),
]
)
3 changes: 3 additions & 0 deletions day23/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# day23

A description of this package.
66 changes: 66 additions & 0 deletions day23/Sources/day23/main.swift
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()
7 changes: 7 additions & 0 deletions day23/Tests/LinuxMain.swift
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)
9 changes: 9 additions & 0 deletions day23/Tests/day23Tests/XCTestManifests.swift
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
47 changes: 47 additions & 0 deletions day23/Tests/day23Tests/day23Tests.swift
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),
]
}

0 comments on commit b8e7d36

Please sign in to comment.