Ejemplo n.º 1
0
import itertools
import time
from intcode import intcode as ic

if __name__=="__main__":
    _memory = [0] * 64 * 1024 * 1024
    _data = [int(x) for x in open("d09.in").read().split(",")]
    #_data = [int(x) for x in "1102,34915192,34915192,7,4,7,99,0".split(",")]
    _memory[0:len(_data)] = _data

    outq = ic.Queue()
    c = ic.CPU("Part1", _memory, ic.Queue([1]), outq)
    c.interpret()
    print(outq.get())
    c = ic.CPU("Part2", _memory, ic.Queue([2]), outq)
    c.interpret()
    print(outq.get())



Ejemplo n.º 2
0
    return -requires["ORE"]


if __name__ == "__main__":
    data = [x.strip().split("=>") for x in open("d14.in").readlines()]
    formula = {}
    for j, d in enumerate(data):
        i, r = d
        i = [tuple(_i.strip().split(" ")) for _i in i.split(",")]
        i = [(int(x[0]), x[1]) for x in i]
        r = tuple(_r.strip() for _r in r.strip().split(" "))
        r = (int(r[0]), r[1])
        formula[r[1]] = (r[0], i)
    print(formula)
    print(alchemy(1))
    minF = 10**12 // alchemy(1)
    maxF = minF * 10

    while minF != maxF:
        fuel = (minF + maxF + 1) // 2
        if alchemy(fuel) > 10**12:
            maxF = fuel - 1
        else:
            minF = fuel

    print("Part 2:", maxF)

    for x in ic.Queue([1, 2, 3]):
        print(x)
Ejemplo n.º 3
0
from intcode import intcode as ic
from collections import defaultdict

if __name__ == "__main__":
    _memory = [0] * 64 * 1024 * 1024
    _data = [int(x) for x in open("d17.in").read().split(",")]
    _memory[0:len(_data)] = _data

    cpu = ic.CPU("Part1", _memory, ic.Queue(), ic.Queue())
    scr = [["."] * 50 for _ in range(50)]
    count = 0
    for i in range(50):
        for j in range(50):
            cpu._in.put(j)
            cpu._in.put(i)
            cpu.interpret()
            d = chr(cpu._out.get())
            if d == '1':
                count += 1
            scr[i][j] = d
    for l in scr:
        print("".join(l))

    print(count)
Ejemplo n.º 4
0
                m[n] = str(l + 1)
            search(m, n, l + 1, vm)
            vm._in.put(revs[_m])
            vm.interpret()
            vm._out.get()
        if p == 2:
            m[crt] = "*"
            print(l)


if __name__ == "__main__":
    _memory = [0] * 4 * 1024 * 1024
    _data = [int(x) for x in open("d15.in").read().split(",")]
    _memory[0:len(_data)] = _data

    _in = ic.Queue()
    _out = ic.Queue()
    vm = ic.CPU("Part1", _memory, _in, _out)

    N = 60
    crt = (N, N)
    l = 0
    m = {crt: "0"}
    search(m, crt, 0, vm)

    #print(m)
    img = [[" "] * (2 * N) for _ in range(2 * N)]
    for k in m.keys():
        x, y = k
        v = m[k]
        if v == 0: