Created
January 2, 2018 00:18
-
-
Save BeyondEvil/164f6c3f17b82090b7f857370990653d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import operator as op | |
from collections import defaultdict | |
def read_input(): | |
with open('input.txt', 'r') as f: | |
return f.read().strip() | |
def get(parts, v): | |
try: | |
return int(v) | |
except ValueError: | |
return int(parts[v]) | |
def run_it(seq): | |
operation = {'AND': op.and_, | |
'LSHIFT': op.lshift, | |
'RSHIFT': op.rshift, | |
'OR': op.or_} | |
wires = defaultdict(int) | |
while not wires['a']: | |
for instruction in seq.splitlines(): | |
cmd = re.split(' | -> ', instruction) | |
cmd.remove('->') | |
if len(cmd) == 2: # 123 -> x | |
v, w = cmd | |
v = get(wires, v) | |
if v: | |
wires[w] = v | |
elif len(cmd) == 3: # NOT x -> h | |
_, v, w = cmd | |
v = get(wires, v) | |
if v: | |
wires[w] = 65535 - v | |
elif len(cmd) == 4: # x AND y -> d | |
v1, o, v2, w = cmd | |
v1 = get(wires, v1) | |
v2 = get(wires, v2) | |
if v1 and v2: | |
wires[w] = operation[o](v1, v2) | |
else: | |
assert False | |
print('Part 1: ', wires['a']) | |
print('Part 2: ', 0) | |
if __name__ == '__main__': | |
run_it(read_input()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment