Beispiel #1
0
def problem2_intepreted(filename):
    lines = util.read_input(filename)
    regs = [0] * 6
    ip_reg = util.parse_int_line(lines[0])[0]
    ops = []
    args = []
    for l in lines[1:]:
        ops.append(l.split(" ")[0])
        args.append(util.parse_int_line(l))

    found_values = set()

    matches = 0
    while matches < 100:
        ip = regs[ip_reg]
        if ip == 28:
            matches += 1
            if regs[4] not in found_values:
                found_values |= {regs[4]}
                print(datetime.now(), regs[4])
        # make sure it does not terminate
        if ip > 30:
            ip = 5
        if not (ip >= 0 and ip < len(ops)):
            break
        o = ops[ip]
        a = args[ip]
        operations[o](regs, a)
        regs[ip_reg] += 1
Beispiel #2
0
def parse_army(lines, side):
    global index
    lines = lines[1:]
    ret = []

    for l in lines:
        index += 1
        i = util.parse_int_line(l)
        size = i[0]
        hp = i[1]
        damage = i[2]
        initiative = i[3]

        words = l.split(" ")
        damage_index = words.index("damage") - 1
        damage_type = words[damage_index]

        immune = []
        weakness = []
        if "(" in l:
            l2 = l[l.index("(")+1:l.index(")")]
            s = l2.split(";")
            for sp in s:
                sp = sp.strip()
                w = sp.replace(",", "").split(" ")[2:]
                if sp.startswith("weak"):
                    weakness += w
                elif sp.startswith("immune"):
                    immune += w
                else:
                    raise Exception("unable to parse "+sp)

        ret.append(Army(index, side, size, hp, damage, damage_type, initiative, immune, weakness))

    return ret
Beispiel #3
0
def problem1():
    lines = util.read_input(filename)

    count = 0
    i = 0
    while i < len(lines) and lines[i]:
        before = util.parse_int_line(lines[i])
        op = util.parse_int_line(lines[i + 1])
        after = util.parse_int_line(lines[i + 2])

        m = test_op(before, op, after)
        if len(m) >= 3:
            count += 1

        i += 4

    print(count)
Beispiel #4
0
def problem2():
    lines = util.read_input(filename)

    possible_ops = {}

    i = 0
    while i < len(lines) and lines[i]:
        before = util.parse_int_line(lines[i])
        op = util.parse_int_line(lines[i + 1])
        after = util.parse_int_line(lines[i + 2])
        i += 4

        m = test_op(before, op, after)
        opcode = op[0]
        possible_ops[opcode] = possible_ops.get(opcode, []) + [set(m)]

    op_map = {}
    used_ops = set()
    while len(op_map) < len(possible_ops):
        for k, v in possible_ops.items():
            op_set = None
            for op_results in v:
                if op_set is None:
                    op_set = op_results - used_ops
                else:
                    op_set &= op_results
            if len(op_set) != 1:
                print("failed to find unique op!! " + str(op_set))
            else:
                opcode = op_set.pop()
                op_map[k] = opcode
                used_ops |= {opcode}

    i += 2
    lines = lines[i:]
    ops = list(map(lambda l: util.parse_int_line(l), lines))
    regs = [0, 0, 0, 0]

    for op in ops:
        op_method = OPS[op_map[op[0]]]
        op_method(regs, op)

    print(regs)
Beispiel #5
0
def problem1():
    lines = util.read_input(filename)
    regs = [0] * 6
    ip_reg = util.parse_int_line(lines[0])[0]
    ops = []
    args = []
    for l in lines[1:]:
        ops.append(l.split(" ")[0])
        args.append(util.parse_int_line(l))

    while True:
        ip = regs[ip_reg]
        if not (ip >= 0 and ip < len(ops)):
            break
        o = ops[ip]
        a = args[ip]
        operations[o](regs, a)
        regs[ip_reg] += 1
        # print(regs[ip_reg], regs)

    print(regs, regs[0])
Beispiel #6
0
def problem1(filename):
    lines = util.read_input(filename)
    regs = [0] * 6
    ip_reg = util.parse_int_line(lines[0])[0]
    ops = []
    args = []
    for l in lines[1:]:
        ops.append(l.split(" ")[0])
        args.append(util.parse_int_line(l))

    regs[0] = 5745418

    for i in range(1000000):
        ip = regs[ip_reg]
        # solve 1, set breakpoint here
        if not (ip >= 0 and ip < len(ops)):
            break
        o = ops[ip]
        a = args[ip]
        operations[o](regs, a)
        regs[ip_reg] += 1

    print(i)
Beispiel #7
0
def parse(lines):
    clay = set()
    ymin, ymax = 999999999, 0

    for l in lines:
        [s, a, b] = util.parse_int_line(l)
        for i in range(a, b+1):
            if l.startswith("x"):
                clay |= {(s, i)}
                ymin = min(i, ymin)
                ymax = max(i, ymax)
            elif l.startswith("y"):
                clay |= {(i, s)}
                ymin = min(s, ymin)
                ymax = max(s, ymax)
            else:
                raise Exception("parse fail")
    return clay, (ymin, ymax)