Cleanup
This commit is contained in:
parent
7326d17948
commit
632868f7f3
@ -76,18 +76,18 @@ class Day16: Runnable {
|
||||
let input = try! String(contentsOfFile: inputPath)
|
||||
let parts = input
|
||||
.trimmingCharacters(in: .newlines)
|
||||
let bytes = parts.ToBinary().joined()
|
||||
let bytes = parts.bits().joined()
|
||||
let packets = parsePacket(bytes: bytes)
|
||||
let p: Packet = packets.0
|
||||
print(p.getTotalVersion())
|
||||
print(p.getValue())
|
||||
}
|
||||
|
||||
func parsePacket(bytes: String) -> (Packet, String) {
|
||||
let version = bytes[bytes.startIndex..<bytes.index(bytes.startIndex, offsetBy: 3)].description.binaryToDecimal()
|
||||
let typeId = bytes[bytes.index(bytes.startIndex, offsetBy: 3)..<bytes.index(bytes.startIndex, offsetBy: 6)].description.binaryToDecimal()
|
||||
func parsePacket<S>(bytes: S) -> (Packet, S.SubSequence) where S:StringProtocol {
|
||||
let version = bytes[bytes.startIndex..<bytes.index(bytes.startIndex, offsetBy: 3)].binaryToDecimal()
|
||||
let typeId = bytes[bytes.index(bytes.startIndex, offsetBy: 3)..<bytes.index(bytes.startIndex, offsetBy: 6)].binaryToDecimal()
|
||||
var value: Packet
|
||||
var reminder: String
|
||||
var reminder: S.SubSequence
|
||||
switch typeId {
|
||||
case 4:
|
||||
let (v, r) = parseLiteralPacketValue(bytes: bytes)
|
||||
@ -101,17 +101,17 @@ class Day16: Runnable {
|
||||
return (value, reminder)
|
||||
}
|
||||
|
||||
func parseOperatorPacker(bytes: String) -> ([Packet], String) {
|
||||
func parseOperatorPacker<S>(bytes: S) -> ([Packet], S.SubSequence) where S:StringProtocol {
|
||||
let startNextBitsIndex = bytes.index(bytes.startIndex, offsetBy: 7)
|
||||
let lengthTypeId = bytes[bytes.index(bytes.startIndex, offsetBy: 6)..<startNextBitsIndex].description
|
||||
let isTotalLength = lengthTypeId == "0"
|
||||
|
||||
let nextBitsLength = isTotalLength ? 15 : 11
|
||||
let endNextBitsIndex = bytes.index(startNextBitsIndex, offsetBy: nextBitsLength)
|
||||
let nextBits = bytes[startNextBitsIndex..<endNextBitsIndex].description
|
||||
let nextBits = bytes[startNextBitsIndex..<endNextBitsIndex]
|
||||
|
||||
var number = nextBits.binaryToDecimal()
|
||||
var reminder = bytes[endNextBitsIndex...].description
|
||||
var reminder = bytes[endNextBitsIndex...]
|
||||
var subPackets = [Packet]()
|
||||
|
||||
while number > 0 {
|
||||
@ -123,17 +123,17 @@ class Day16: Runnable {
|
||||
return (subPackets, reminder)
|
||||
}
|
||||
|
||||
func parseLiteralPacketValue(bytes: String) -> (Int, String) {
|
||||
func parseLiteralPacketValue<S>(bytes: S) -> (Int, S.SubSequence) where S:StringProtocol {
|
||||
var index = bytes.index(bytes.startIndex, offsetBy: 6)
|
||||
var ei = index
|
||||
var bits = ""
|
||||
var reminder = ""
|
||||
var bits: Substring = ""
|
||||
var reminder: S.SubSequence = ""
|
||||
while true {
|
||||
let si = bytes.index(index, offsetBy: 1)
|
||||
ei = bytes.index(si, offsetBy: 4)
|
||||
bits += bytes[si..<ei].description
|
||||
bits += bytes[si..<ei]
|
||||
if bytes[index] == "0" {
|
||||
reminder = bytes[ei...].description
|
||||
reminder = bytes[ei...]
|
||||
break
|
||||
}
|
||||
index = bytes.index(ei, offsetBy: 0)
|
||||
@ -142,52 +142,48 @@ class Day16: Runnable {
|
||||
}
|
||||
}
|
||||
|
||||
extension String {
|
||||
func ToBinary() -> [String] {
|
||||
extension StringProtocol {
|
||||
func bits() -> [String] {
|
||||
self.map { $0.bits }
|
||||
}
|
||||
|
||||
func binaryToDecimal() -> Int {
|
||||
self.reversed().enumerated().reduce(0, { p, cur in
|
||||
p + (cur.element == "0" ? 0 : Int(pow(2.0, Double(cur.offset))))
|
||||
})
|
||||
}
|
||||
func binaryToDecimal() -> Int { Int(self, radix: 2)! }
|
||||
}
|
||||
|
||||
extension Character {
|
||||
var bits: String {
|
||||
switch self {
|
||||
case "0":
|
||||
switch self.hexDigitValue! {
|
||||
case 0:
|
||||
return "0000"
|
||||
case "1":
|
||||
case 1:
|
||||
return "0001"
|
||||
case "2":
|
||||
case 2:
|
||||
return "0010"
|
||||
case "3":
|
||||
case 3:
|
||||
return "0011"
|
||||
case "4":
|
||||
case 4:
|
||||
return "0100"
|
||||
case "5":
|
||||
case 5:
|
||||
return "0101"
|
||||
case "6":
|
||||
case 6:
|
||||
return "0110"
|
||||
case "7":
|
||||
case 7:
|
||||
return "0111"
|
||||
case "8":
|
||||
case 8:
|
||||
return "1000"
|
||||
case "9":
|
||||
case 9:
|
||||
return "1001"
|
||||
case "A":
|
||||
case 10:
|
||||
return "1010"
|
||||
case "B":
|
||||
case 11:
|
||||
return "1011"
|
||||
case "C":
|
||||
case 12:
|
||||
return "1100"
|
||||
case "D":
|
||||
case 13:
|
||||
return "1101"
|
||||
case "E":
|
||||
case 14:
|
||||
return "1110"
|
||||
case "F":
|
||||
case 15:
|
||||
return "1111"
|
||||
default:
|
||||
fatalError()
|
||||
|
Loading…
Reference in New Issue
Block a user