This commit is contained in:
Max Nuding 2021-12-01 20:11:11 +01:00
commit bdc7895a61
Signed by: phlaym
GPG Key ID: A06651BAB6777237
8 changed files with 2136 additions and 0 deletions

7
.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
xcuserdata/
DerivedData/
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

16
Package.resolved Normal file
View File

@ -0,0 +1,16 @@
{
"object": {
"pins": [
{
"package": "swift-collections",
"repositoryURL": "https://github.com/apple/swift-collections.git",
"state": {
"branch": null,
"revision": "48254824bb4248676bf7ce56014ff57b142b77eb",
"version": "1.0.2"
}
}
]
},
"version": 1
}

32
Package.swift Normal file
View File

@ -0,0 +1,32 @@
// swift-tools-version:5.5
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "aoc2021",
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(
url: "https://github.com/apple/swift-collections.git",
.upToNextMajor(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.
.executableTarget(
name: "aoc2021",
dependencies: [
.product(name: "Collections", package: "swift-collections")
],
resources: [
.process("Resources"),
]
),
.testTarget(
name: "aoc2021Tests",
dependencies: ["aoc2021"]),
]
)

3
README.md Normal file
View File

@ -0,0 +1,3 @@
# aoc2021
A description of this package.

22
Sources/aoc2021/01.swift Normal file
View File

@ -0,0 +1,22 @@
//
// File.swift
//
//
// Created by Max Nuding on 01.12.21.
//
import Foundation
struct Day01 {
let inputPath: String
func run() {
let input = try! String(contentsOfFile: inputPath)
let measurements = input
.split(separator: "\n")
.compactMap { Int32($0) }
.reduce((currentCount: 0, lastValue: Int32.max)) { partialResult, currentValue in
let increaseBy = currentValue > partialResult.lastValue ? 1 : 0
return (currentCount: partialResult.currentCount + increaseBy, lastValue: currentValue)
}
print(measurements.currentCount)
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,9 @@
import Foundation
print("Starting day 01")
let input01 = Bundle.module.path(forResource: "01", ofType: ".txt")!
let start = CFAbsoluteTimeGetCurrent()
Day01(inputPath: input01).run()
let end = CFAbsoluteTimeGetCurrent()
let execTimeMs = round((end - start) * 1000.0 * 100.0) / 100.0
print("Finished in \(execTimeMs)ms")

View File

@ -0,0 +1,47 @@
import XCTest
import class Foundation.Bundle
final class aoc2021Tests: 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
}
// Mac Catalyst won't have `Process`, but it is supported for executables.
#if !targetEnvironment(macCatalyst)
let fooBinary = productsDirectory.appendingPathComponent("aoc2021")
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")
#endif
}
/// 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
}
}