03a
This commit is contained in:
parent
bceb0b5405
commit
8072d84a5a
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
.swiftpm/
|
||||
.DS_Store
|
||||
/.build
|
||||
/Packages
|
||||
|
@ -1,16 +0,0 @@
|
||||
{
|
||||
"object": {
|
||||
"pins": [
|
||||
{
|
||||
"package": "swift-collections",
|
||||
"repositoryURL": "https://github.com/apple/swift-collections.git",
|
||||
"state": {
|
||||
"branch": null,
|
||||
"revision": "48254824bb4248676bf7ce56014ff57b142b77eb",
|
||||
"version": "1.0.2"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"version": 1
|
||||
}
|
@ -5,13 +5,14 @@ import PackageDescription
|
||||
|
||||
let package = Package(
|
||||
name: "aoc2021",
|
||||
platforms: [.macOS(.v12)],
|
||||
dependencies: [
|
||||
// Dependencies declare other packages that this package depends on.
|
||||
// .package(url: /* package url */, from: "1.0.0"),
|
||||
.package(
|
||||
/*.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.
|
||||
@ -19,7 +20,7 @@ let package = Package(
|
||||
.executableTarget(
|
||||
name: "aoc2021",
|
||||
dependencies: [
|
||||
.product(name: "Collections", package: "swift-collections")
|
||||
//.product(name: "Collections", package: "swift-collections")
|
||||
],
|
||||
resources: [
|
||||
.process("Resources"),
|
||||
|
64
Sources/aoc2021/03.swift
Normal file
64
Sources/aoc2021/03.swift
Normal file
@ -0,0 +1,64 @@
|
||||
//
|
||||
// File.swift
|
||||
//
|
||||
//
|
||||
// Created by Max Nuding on 02.12.21.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import AppKit
|
||||
|
||||
struct Day03 {
|
||||
let inputPath: String
|
||||
|
||||
func run() {
|
||||
let input = try! String(contentsOfFile: inputPath)
|
||||
let commands = input
|
||||
.components(separatedBy: .newlines)
|
||||
.filter { !$0.isEmpty }
|
||||
//.compactMap { MoveCommand(line: $0) }
|
||||
runA(commands)
|
||||
//runB(commands)
|
||||
}
|
||||
|
||||
func runA(_ commands: [String]) {
|
||||
let neededForMajority = commands.count / 2
|
||||
let lastBitIndex = commands.first!.count
|
||||
var countsOnes = [Int](repeating: 0, count: lastBitIndex)
|
||||
for command in commands {
|
||||
let m = command.map { Bool.init(digit: $0)! }.map { $0 ? 1 : 0 }
|
||||
countsOnes.enumerated().forEach { (idx, elem) in
|
||||
countsOnes[idx] += m[idx]
|
||||
}
|
||||
}
|
||||
let dec = countsOnes.indices.reduce((gamma: 0, episolon: 0), { partialResult, idx in
|
||||
let isOne = countsOnes[idx] > neededForMajority
|
||||
let val = Int(truncating: NSDecimalNumber(decimal: pow(2, countsOnes.count - idx - 1)))
|
||||
var (gamma, episolon) = partialResult
|
||||
if isOne {
|
||||
gamma += val
|
||||
} else {
|
||||
episolon += val
|
||||
}
|
||||
return (gamma, episolon)
|
||||
})
|
||||
print(dec.episolon * dec.gamma)<
|
||||
}
|
||||
|
||||
func runB(_ commands: [String]) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
extension Bool {
|
||||
init?(digit: Character) {
|
||||
switch digit {
|
||||
case "0":
|
||||
self = false
|
||||
case "1":
|
||||
self = true
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
1000
Sources/aoc2021/Resources/input/03.txt
Normal file
1000
Sources/aoc2021/Resources/input/03.txt
Normal file
File diff suppressed because it is too large
Load Diff
@ -1,5 +1,5 @@
|
||||
import Foundation
|
||||
|
||||
//import Collections
|
||||
/*
|
||||
print("Starting day 01")
|
||||
let input01 = Bundle.module.path(forResource: "01", ofType: ".txt")!
|
||||
@ -10,6 +10,7 @@ let execTimeMs = round((end - start) * 1000.0 * 100.0) / 100.0
|
||||
print("Finished in \(execTimeMs)ms")
|
||||
*/
|
||||
|
||||
/*
|
||||
print("Starting day 02")
|
||||
let input02 = Bundle.module.path(forResource: "02", ofType: ".txt")!
|
||||
let start = CFAbsoluteTimeGetCurrent()
|
||||
@ -17,3 +18,12 @@ Day02(inputPath: input02).run()
|
||||
let end = CFAbsoluteTimeGetCurrent()
|
||||
let execTimeMs = round((end - start) * 1000.0 * 100.0) / 100.0
|
||||
print("Finished in \(execTimeMs)ms")
|
||||
*/
|
||||
|
||||
print("Starting day 03")
|
||||
let input03 = Bundle.module.path(forResource: "03", ofType: ".txt")!
|
||||
let start = CFAbsoluteTimeGetCurrent()
|
||||
Day03(inputPath: input03).run()
|
||||
let end = CFAbsoluteTimeGetCurrent()
|
||||
let execTimeMs = round((end - start) * 1000.0 * 100.0) / 100.0
|
||||
print("Finished in \(execTimeMs)ms")
|
||||
|
Loading…
Reference in New Issue
Block a user