Skip to content

Commit 6e621b3

Browse files
committed
Show the correct bits
1 parent 0fa3aa5 commit 6e621b3

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

20.bstate.swift

+24-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
enum Module {
22
case flip(String, [Int], UInt8)
3-
case conjunction(String, Int, [Int], UInt64)
3+
case conjunction(String, [Int], [Int], UInt64)
44
case broadcast(String, [Int])
55
case other(String)
66
}
@@ -29,14 +29,24 @@ extension Module: CustomStringConvertible {
2929

3030
var stateString: String? {
3131
switch self {
32-
case .conjunction(_, let inputCount, _, let state):
33-
String(String(state, radix: 2).suffix(inputCount))
32+
case .conjunction(_, let ik, _, let state): _conjunctionString(ik, state)
3433
case .flip(_, _, let state): state == 1 ? "1" : "0"
3534
default: nil
3635
}
3736
}
3837
}
3938

39+
func _conjunctionString(_ ik: [Int], _ state: UInt64) -> String {
40+
var result = [Character]()
41+
for (i, c) in Array(String(state, radix: 2)).reversed().enumerated() {
42+
if ik.firstIndex(of: i) != nil {
43+
result.append(c)
44+
}
45+
}
46+
return String(result)
47+
}
48+
49+
4050
var modulesIndexToName: [Int: String] = [:]
4151
var rxIndex: Int?
4252
let verbose = switch CommandLine.arguments.last {
@@ -46,7 +56,6 @@ let verbose = switch CommandLine.arguments.last {
4656
default: 0
4757
}
4858

49-
5059
func readInput() -> [Module] {
5160
let broadcast = "broadcaster"
5261

@@ -56,7 +65,7 @@ func readInput() -> [Module] {
5665
let name = String(words.first!)
5766
let partial: Module? = switch line.first {
5867
case "%": .flip("", [], 0)
59-
case "&": .conjunction("", 0, [], 0)
68+
case "&": .conjunction("", [], [], 0)
6069
default: name == broadcast ? .broadcast("", []) : .other(name)
6170
}
6271
let outputs = Array(words.dropFirst(1).map { String($0) })
@@ -93,11 +102,13 @@ func readInput() -> [Module] {
93102
case .conjunction:
94103
var state = UInt64.max
95104
let ins = inputs[name]!
105+
var ik = [Int]()
96106
for input in ins {
97107
let k = keys.firstIndex(of: input)!
108+
ik.append(k)
98109
state ^= (1 << k)
99110
}
100-
result.append(.conjunction(name, ins.count, js!, state))
111+
result.append(.conjunction(name, ik, js!, state))
101112
}
102113

103114
modulesIndexToName[result.count - 1] = name
@@ -142,14 +153,14 @@ func propogate(pulse: Pulse, module: Module) -> ([Pulse], Module)? {
142153
state = toggle(state)
143154
return (emit(state == 1, outputs), .flip(name, outputs, state))
144155
}
145-
case .conjunction(let name, let n, let outputs, var state):
156+
case .conjunction(let name, let ik, let outputs, var state):
146157
if pulse.value {
147158
state |= (1 << pulse.from)
148159
} else {
149160
state &= ~(1 << pulse.from)
150161
}
151162
let value = state != .max
152-
return (emit(value, outputs), .conjunction(name, n, outputs, state))
163+
return (emit(value, outputs), .conjunction(name, ik, outputs, state))
153164
}
154165
}
155166

@@ -206,8 +217,10 @@ func showPreamble(modules: [Module]) {
206217
var names = [String]()
207218
for m in modules {
208219
switch m {
209-
case .flip(let name, _, _): names.append(padn(name, n: 2))
210-
case .conjunction(let name, let n, _, _): names.append(padn(name, n: max(n + 1, 2)))
220+
case .flip(let name, _, _):
221+
names.append(padn(name, n: 2))
222+
case .conjunction(let name, let ik, _, _):
223+
names.append(padn(name, n: max(ik.count + 1, 2)))
211224
default: continue
212225
}
213226
}
@@ -219,7 +232,7 @@ func show(modules: [Module]) {
219232
if verbose > 1 {
220233
print(modules.map({ $0.description }).joined(separator: " · "))
221234
} else if verbose > 0 {
222-
print(modules.compactMap({ $0.stateString }).joined(separator: "·"))
235+
print(modules.compactMap({ $0.stateString }).joined(separator: " "))
223236
}
224237
}
225238

0 commit comments

Comments
 (0)