Skip to content

Instantly share code, notes, and snippets.

@BeyondEvil
Created January 2, 2018 00:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BeyondEvil/164f6c3f17b82090b7f857370990653d to your computer and use it in GitHub Desktop.
Save BeyondEvil/164f6c3f17b82090b7f857370990653d to your computer and use it in GitHub Desktop.
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