Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- import math
- class operation():
- def __init__(self, op, val):
- self.op = op
- self.val = val
- self.fixed = False
- def __str__(self):
- return '[{}, {}]'.format(self.op, self.val)
- def fix(self):
- print('fixing {}'.format(self.op))
- if self.op == 'jmp':
- self.op = 'nop'
- else:
- self.op = 'jmp'
- self.fixed = True
- def restore(self):
- print('restoring {}'.format(self.op))
- if self.op == 'jmp':
- self.op = 'nop'
- else:
- self.op = 'jmp'
- self.fixed = False
- data = []
- with open('input', 'r') as input_file:
- for line in input_file:
- line = line.strip()
- op = line[0:3]
- val = int(line[4:])
- d = operation(op, val)
- print(d)
- data.append(d)
- nodes_visited = []
- accumulator = 0
- next_node = 0
- stack = []
- fixed = False
- while next_node < len(data):
- if next_node in nodes_visited:
- next_node = nodes_visited.pop()
- while data[next_node].op == 'acc' or fixed:
- if data[next_node].fixed:
- data[next_node].restore()
- fixed = False
- next_node = nodes_visited.pop()
- data[next_node].fix()
- fixed = True
- nodes_visited.append(next_node)
- d = data[next_node]
- print(d.fixed)
- print('{}: {} {}'.format(next_node, d.op, d.val))
- if d.op == 'nop':
- next_node += 1
- elif d.op == 'acc':
- next_node += 1
- elif d.op == 'jmp':
- next_node += d.val
- for n in nodes_visited:
- d = data[n]
- if d.op == 'acc':
- # print(d.val)
- accumulator += d.val
- print('accumulator: {}'.format(accumulator))
Advertisement
Add Comment
Please, Sign In to add comment