<jupyter_start><jupyter_text>Day 19<jupyter_code>import operator

OPS = {
    'mu': operator.mul,
    'ad': operator.add,
    'ba': operator.and_,
    'bo': operator.or_,
    'gt': operator.gt,
    'eq': operator.eq,
}

def operate(reg, op, A, B, C):

    # Don't modify in place.
    # Should maybe have done with reg as class.
    out = reg.copy()

    # Set up the registers.
    if op[:2] in ['gt', 'eq']:
        regi = op[-2:]
    else:
        regi = 'r' + op[-1]

    # setitem is a special case.
    if op[:2] == 'se':
        out[C] = A if regi[-1] == 'i' else reg[A]
        return out

    # Set up the operands.
    a = A if regi[0] == 'i' else reg[A]
    b = B if regi[1] == 'i' else reg[B]

    # Do the operation.
    out[C] = OPS[op[:2]](a, b)

    return [int(x) for x in out]<jupyter_output><empty_output><jupyter_text>Part 1 Run the program...<jupyter_code>prog = """seti 5 0 1
seti 6 0 2
addi 0 1 0
addr 1 2 3
setr 1 0 0
seti 8 0 4
seti 9 0 5"""
from day19_data import prog
prog_ = prog.split('\n')
prog_ = prog_[1:]
len(prog_)
reg = [1, 0, 0, 0, 0, 0]
ipr = 4
ip = 0
verbose = 0

cycle = 0
while cycle <= 100000:
    
    if verbose:
        print(reg, end=' ')
    
    try:
        line = prog_[int(ip)]
    except IndexError:
        break
        
    if verbose:
        print(line, end=' ')


    if line.startswith('#'):
        ipr = int(line.split()[-1])
        ip += 1
        continue

    # Record the ip.
    #operate(reg, 'seti', ip, 0, ipr)
    reg[ipr] = ip
    
    # Get the instruction.
    opcode, A, B, C = [int(x) if x.isnumeric() else x for x in line.split()]
    reg = operate(reg, opcode, A, B, C)
    
    # Recover the ip and increment.
    ip = reg[ipr] + 1

    if verbose:
        print(reg)

    cycle += 1

print(reg)<jupyter_output><empty_output><jupyter_text>Part 2<jupyter_code>cycle
948/11
948/3
948/4
948/6
948/8
948/12
1,2,3,4,6,12
1,3,6,10,16,28
10551348
# Sum of factors
def compute(x):
    result = 0
    for i in range(1, x+1):
        if not x % i:
            result += i
    return result
def compute(x):
    return sum(i for i in range(1, x+1) if not x % i)
compute(948)
compute(2450)
compute(10551348)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15129486597001363, 'fraction_numerical': 0.05770104497955475, 'mean_word_length': 2.2865671641791043, 'pattern_counts': {'":': 0, '<': 14, '<?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': '475811', 'license': 'Apache-2.0', 'n_tokens_mistral': 982, 'n_tokens_neox': 886, 'n_words': 270, 'path': 'day19.ipynb', 'repo_name': 'kwinkunks/aoc18', 'size': '2201'}