03a
This commit is contained in:
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")
|
||||
|
Reference in New Issue
Block a user