To solve this problem, we'll parse each line of the input to identify the type of operation to perform and then execute the operation. As we process each line, we'll store the result of each operation in a dictionary. Once we've processed all the instructions, we can find the signal provided to wire a.
I'll write a function, parse_operation, that takes an input line and returns the type of operation, the operands, and the output wire.
Then, using that function, I'll implement the main solution function find_signal_on_wire_a. This function will initialize an empty wire_signals dictionary to store the signals on the wires. It will then loop through the input instructions, parsing each line with the parse_operation function, and performing the required operation on the operands using the wire_signals dictionary.
Finally it will return the signal on wire 'a'.
Here is my solution:
import re
def parse_operation(line):
AND_RE = r"(\w+) AND (\w+) -> (\w+)"
OR_RE = r"(\w+) OR (\w+) -> (\w+)"
LSHIFT_RE = r"(\w+) LSHIFT (\d+) -> (\w+)"
RSHIFT_RE = r"(\w+) RSHIFT (\d+) -> (\w+)"
NOT_RE = r"NOT (\w+) -> (\w+)"
ASSIGN_RE = r"(\d+|\w+) -> (\w+)"
if (match := re.match(AND_RE, line)):
return 'AND', match.group(1), match.group(2), match.group(3)
elif (match := re.match(OR_RE, line)):
return 'OR', match.group(1), match.group(2), match.group(3)
elif (match := re.match(LSHIFT_RE, line)):
return 'LSHIFT', match.group(1), int(match.group(2)), match.group(3)
elif (match := re.match(RSHIFT_RE, line)):
return 'RSHIFT', match.group(1), int(match.group(2)), match.group(3)
elif (match := re.match(NOT_RE, line)):
return 'NOT', match.group(1), match.group(2)
elif (match := re.match(ASSIGN_RE, line)):
return 'ASSIGN', match.group(1), match.group(2)
def get_value(operand, wire_signals):
if operand.isdigit():
return int(operand)
else:
return wire_signals.get(operand, None)
def find_signal_on_wire_a(instructions):
wire_signals = {}
while instructions:
line = instructions.pop(0)
operation, *operands, output = parse_operation(line)
if operation == 'AND':
a, b = operands
if (value_a := get_value(a, wire_signals)) is not None and \
(value_b := get_value(b, wire_signals)) is not None:
wire_signals[output] = value_a & value_b
else:
instructions.append(line)
elif operation == 'OR':
a, b = operands
if (value_a := get_value(a, wire_signals)) is not None and \
(value_b := get_value(b, wire_signals)) is not None:
wire_signals[output] = value_a | value_b
else:
instructions.append(line)
elif operation == 'LSHIFT':
a, b = operands
if (value_a := get_value(a, wire_signals)) is not None:
wire_signals[output] = value_a << b
else:
instructions.append(line)
elif operation == 'RSHIFT':
a, b = operands
if (value_a := get_value(a, wire_signals)) is not None:
wire_signals[output] = value_a >> b
else:
instructions.append(line)
elif operation == 'NOT':
a = operands[0]
if (value_a := get_value(a, wire_signals)) is not None:
wire_signals[output] = ~value_a & 0xFFFF
else:
instructions.append(line)
elif operation == 'ASSIGN':
a = operands[0]
if (value_a := get_value(a, wire_signals)) is not None:
wire_signals[output] = value_a
else:
instructions.append(line)
return wire_signals['a']
And here's how to use this function:
with open("input.txt", "r") as file:
instructions = file.readlines()
signal_on_wire_a = find_signal_on_wire_a(instructions)
print(signal_on_wire_a)
This code should correctly solve the problem and find the signal on wire a.