
from collections import namedtuple
Inst = namedtuple('Inst', ['cmd', 'args'])

def get_val(vstr, state):
    try:
        val = int(vstr)
    except ValueError:
        val = int(state[vstr])
    return val

def run_instructions(instructions, init_state=None):
    state = {'a': 0, 'b': 0, 'c': 0, 'd': 0}
    if init_state:
        state.update(init_state)
    
    inst_ptr = 0

    while 0 <= inst_ptr < len(instructions):
        inst = instructions[inst_ptr]
        print(state)
        if inst.cmd == 'jnz':
            val = get_val(inst.args[0], state)
            if val:
                inst_ptr += int(inst.args[1])
                continue
        if inst.cmd == 'cpy':   
            state[inst.args[1]] = get_val(inst.args[0], state)
        elif inst.cmd == 'inc':
            state[inst.args[0]] += 1
        elif inst.cmd == 'dec':
            state[inst.args[0]] -= 1
        inst_ptr += 1

    return state

def solve_p1(data):
    state = run_instructions(data)
    return state['a']

def solve_p2(data):
    state = run_instructions(data, {'c': 1})
    return state['a']

def load_data(inputfile):
    """
    Converts the data from the input file into something usable 
    by the solve functions.
    """
    instructions = []
    for line in inputfile:
        parts = line.strip().split(' ')
        instructions.append(Inst(parts[0], parts[1:]))
    return instructions

if __name__ == '__main__':
    with open('input.txt', 'r') as inputfile:
        data = load_data(inputfile)

    print("Part 1 Solution")
    print(solve_p1(data))
    print("Part 2 Solution")
    print(solve_p2(data))
