<jupyter_start><jupyter_text>part 1<jupyter_code>exprog = '''nop +0
acc +1
jmp +4
acc +3
jmp -3
acc -99
acc +1
jmp -4
acc +6'''.split('\n')
def step(ic, acc, seen, prog):
    tokens = prog[ic].strip().split()
    op = tokens[0]
    val = int(tokens[1])
    seen.add(ic)
    if 'nop' == op:
        return ic+1, acc, seen, prog
    elif 'acc' == op:
        return ic+1, acc+val, seen, prog
    elif 'jmp' == op:
        return ic+val, acc, seen, prog
def run(prog):
    ic = 0
    acc = 0
    seen = set()
    while True:
        if ic in seen:
            break
        ic, acc, seen, prog = step(ic, acc, seen, prog)
    return acc
run(exprog)
with open('inputs/day8.input') as fp:
    dataprog = fp.readlines()
dataprog[-1]
run(dataprog)<jupyter_output><empty_output><jupyter_text>part 2<jupyter_code>def run2(prog):
    ic = 0
    acc = 0
    seen = set()
    while True:
        if ic in seen:
            #infinite loop
            return None
        if ic == len(prog):
            # finished
            return acc
        ic, acc, seen, prog = step(ic, acc, seen, prog)<jupyter_output><empty_output><jupyter_text>run2(exprog)<jupyter_code>exprog2 = exprog.copy()
exprog2[-2] = 'nop -4'
run2(exprog2)
def modify(prog):
    for i, line in enumerate(prog):
        if ('jmp' not in line) and ('nop' not in line):
            continue
        oldline = line
        if 'jmp' in line:
            prog[i] = line.replace('jmp', 'nop')
        elif 'nop' in line:
            prog[i] = line.replace('nop', 'jmp')
        res = run2(prog)
        if res is not None:
            return res
        prog[i] = oldline
    raise ValueError('Did not work')
    
modify(exprog)
modify(dataprog)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13945380592678674, 'fraction_numerical': 0.01801278326554329, 'mean_word_length': 2.2551984877126654, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '460763', 'license': 'MIT', 'n_tokens_mistral': 676, 'n_tokens_neox': 632, 'n_words': 178, 'path': 'day8.ipynb', 'repo_name': 'g2boojum/AdventOfCode2020', 'size': '1721'}