This commit is contained in:
Max Nuding 2021-12-16 08:32:02 +01:00
parent a786262683
commit 7326d17948
Signed by: phlaym
GPG Key ID: A06651BAB6777237

View File

@ -2,7 +2,7 @@
// File.swift // File.swift
// //
// //
// Created by Max Nuding on 15s.12.21. // Created by Max Nuding on 15.12.21.
// //
import Foundation import Foundation
@ -23,19 +23,46 @@ enum OperatorType: Int {
protocol Packet { protocol Packet {
var version: Int { get } var version: Int { get }
var type: OperatorType { get }
func getTotalVersion() -> Int
func getValue() -> Int
} }
struct ValuePacket: Packet { struct ValuePacket: Packet {
let type: OperatorType = .value
let version: Int let version: Int
let value: Int let value: Int
func getTotalVersion() -> Int { version }
func getValue() -> Int { value }
} }
struct OperatorPacket: Packet { struct OperatorPacket: Packet {
let type: OperatorType let type: OperatorType
let version: Int let version: Int
let subPackets: [Packet] let subPackets: [Packet]
func getTotalVersion() -> Int { version + subPackets.map { $0.getTotalVersion() }.reduce(0, +) }
func getValue() -> Int {
let spValues = subPackets.map { $0.getValue() }
switch type {
case .sum:
return spValues.reduce(0, +)
case .product:
return spValues.reduce(1, *)
case .minimum:
return spValues.min()!
case .maximum:
return spValues.max()!
case .greaterThan:
return spValues.first! > spValues.last! ? 1 : 0
case .lessThan:
return spValues.first! < spValues.last! ? 1 : 0
case .equal:
return spValues.first! == spValues.last! ? 1 : 0
case .value:
fatalError()
}
}
} }
class Day16: Runnable { class Day16: Runnable {
@ -52,47 +79,8 @@ class Day16: Runnable {
let bytes = parts.ToBinary().joined() let bytes = parts.ToBinary().joined()
let packets = parsePacket(bytes: bytes) let packets = parsePacket(bytes: bytes)
let p: Packet = packets.0 let p: Packet = packets.0
let totalVersion = getPacketVersion(p: p) print(p.getTotalVersion())
print(totalVersion) print(p.getValue())
print(getPacketValue(p: p))
}
func getPacketVersion(p: Packet) -> Int {
//print("Found packet: \(p)")
if let packet = p as? OperatorPacket {
return packet.version + packet.subPackets.map(getPacketVersion).reduce(0, +)
} else {
return p.version
}
}
func getPacketValue(p: Packet) -> Int {
var spValues = [Int]()
if let packet = p as? OperatorPacket {
spValues = packet.subPackets.map(getPacketValue)
} else if let packet = p as? ValuePacket {
spValues = [packet.value]
} else {
fatalError()
}
switch p.type {
case .sum:
return spValues.reduce(0, +)
case .product:
return spValues.reduce(1, *)
case .minimum:
return spValues.min()!
case .maximum:
return spValues.max()!
case .greaterThan:
return spValues.first! > spValues.last! ? 1 : 0
case .lessThan:
return spValues.first! < spValues.last! ? 1 : 0
case .equal:
return spValues.first! == spValues.last! ? 1 : 0
case .value:
return spValues.first!
}
} }
func parsePacket(bytes: String) -> (Packet, String) { func parsePacket(bytes: String) -> (Packet, String) {
@ -156,7 +144,7 @@ class Day16: Runnable {
extension String { extension String {
func ToBinary() -> [String] { func ToBinary() -> [String] {
self.map {$0.toBinary()!} self.map { $0.bits }
} }
func binaryToDecimal() -> Int { func binaryToDecimal() -> Int {
@ -167,7 +155,7 @@ extension String {
} }
extension Character { extension Character {
func toBinary() -> String? { var bits: String {
switch self { switch self {
case "0": case "0":
return "0000" return "0000"
@ -202,6 +190,7 @@ extension Character {
case "F": case "F":
return "1111" return "1111"
default: default:
return nil} fatalError()
}
} }
} }