コード例 #1
0
def solve2():
    threshold = 100
    upleft_corners = {}
    y = 1200  # guessing that first suitable is after row 1200
    x_start = 0
    while True:
        hits = 0
        last_hit = 0
        x = x_start
        while True:
            c = Comp(a)
            c.add_one_input(x)
            c.add_one_input(y)
            c.run()
            o = c.get_one_output()
            if o == 1:
                if hits == 0:
                    x_start = x
                    possible_corner = y - threshold + 1
                    if (possible_corner in upleft_corners
                            and upleft_corners[possible_corner] == x):
                        return 10000 * x + possible_corner
                hits += 1
                last_hit = x
            if o == 0 and hits != 0:
                break
            x += 1
        if hits >= threshold:
            upleft_corners[y] = last_hit - threshold + 1
        y += 1
コード例 #2
0
ファイル: a.py プロジェクト: vaderkvarn/aoc19
def run_auto(prog):
    prog = list("".join(prog))
    row = []
    p = load()

    def get_input(comp):
        nonlocal prog
        if len(prog) == 0:
            comp.pause()
            return "\n"
        if "".join(prog).startswith("load"):
            prog = prog[4:]
            comp.p = load("save_file")
            return "\n"
        return prog.pop(0)

    getting_inv = False

    def cb(comp, x):
        nonlocal row
        nonlocal getting_inv
        if x == '\n':
            s = "".join(row)
            if s.startswith("\"Oh"):
                print(int(s.split("typing ")[1].split(" ")[0]))
                exit()
            if getting_inv and s.startswith("-"):
                inv.append(s[2:])
            if s.startswith("Items in"): getting_inv = True
            row = []
        else:
            row.append(x)

    c = Comp(p, get_input, cb, ascii_mode=True)
    c.run()
コード例 #3
0
ファイル: ss_runtime.py プロジェクト: vaderkvarn/aoc19
def run():
    prog = open(sys.argv[2], "r").readlines()
    prog = [row for row in prog if row[0] != '#']
    prog = list("".join(prog))
    row = []
    m = []

    def get_input(comp):
        nonlocal prog
        return prog.pop(0)

    def cb(comp, x):
        nonlocal row
        nonlocal m
        if isinstance(x, int):
            print(x)
            exit()
        if x == '\n' and len(row) > 0:
            m.append(row)
            row = []
        else:
            row.append(x)

    c = Comp(p, get_input, cb, ascii_mode=True)
    c.run()
    for row in m:
        print("".join(row))
コード例 #4
0
ファイル: both.py プロジェクト: vaderkvarn/aoc19
def run_a():
    def on_exit(comp):
        print(len(visited))

    visited[cur] = 0
    c = Comp(p, get_input, cb, on_exit)
    c.run()
コード例 #5
0
ファイル: a.py プロジェクト: vaderkvarn/aoc19
def run():
    prog = []
    row = []

    def get_input(comp):
        nonlocal prog
        if len(prog) == 0:
            prog = list("".join(input()))
            if "".join(prog) == "save":
                print("saving")
                prog = []
                save(comp.p)
                return "\n"
            if "".join(prog) == "load":
                print("loading")
                prog = []
                comp.p = load()
                return "\n"
            prog.append('\n')
        return prog.pop(0)

    def cb(comp, x):
        nonlocal row
        if isinstance(x, int):
            print(x)
            exit()
        if x == '\n':
            print("".join(row))
            row = []
        else:
            row.append(x)

    c = Comp(p, get_input, cb, ascii_mode=True)
    c.run()
コード例 #6
0
ファイル: d11.py プロジェクト: otahontas/adventofcode
def paint(start_from_white = False):
    x = n // 2 
    y = n // 2
    grid = [["."] * n for _ in range(n)]
    c = Comp(a)
    d = "U"
    colored = set()
    if start_from_white:
        grid[y][x] = "#"

    while not c.is_halted():
        inp = 0 if grid[y][x] == "." else 1
        c.add_one_input(inp)
        c.run()

        # Paint
        color = "." if c.get_one_output() == 0 else "#"
        grid[y][x] = color
        colored.add((y, x))

        # Turn
        if d == "U":
            d = "L" if c.get_one_output() == 0 else "R"
        elif d == "R":
            d = "U" if c.get_one_output() == 0 else "D"
        elif d == "D":
            d = "R" if c.get_one_output() == 0 else "L"
        elif d == "L":
            d = "D" if c.get_one_output() == 0 else "U"
        else:
            print("something not right)")
            break

        # Move
        if d == "U":
            y += 1
        elif d == "R":
            x += 1
        elif d == "D":
            y -= 1
        elif d == "L":
            x -= 1
        else:
            print("something not right)")
            break


    if start_from_white:
        A = np.asanyarray(grid)
        B= np.flipud(A)
        for row in B:
            print("".join(row))
    else:
        print(len(colored))
コード例 #7
0
def solve(code):
    c = Comp(a);
    for i in code:
        for j in [ord(c) for c in i]:
            c.add_one_input(j)
        c.add_one_input(10)
    c.run()
    o = c.get_all_outputs()
    for output in o:
        if output <= 127:
            print(chr(output), end="")
        else:
            print("ans:", output)
コード例 #8
0
def solve1():
    n = 50
    ans = 0
    for y in range(n):
        for x in range(n):
            c = Comp(a)
            c.add_one_input(x)
            c.add_one_input(y)
            c.run()
            o = c.get_one_output()
            if o == 1:
                ans += 1
    return ans
コード例 #9
0
def run(p, main, funcs, vid):
    p[0] = 2
    [a, b, c] = funcs
    inputs = main + a + b + c + vid
    output = 0
    def cb(comp, x):
        nonlocal output
        output = x
    def get_input(comp):
        return inputs.pop(0)
    c = Comp(p, get_input, cb)
    c.run()
    print(output)
コード例 #10
0
def get_map(p):
    row = []
    m = []
    def cb(comp, x):
        nonlocal row
        nonlocal m
        if chr(x) == '\n' and len(row) > 0:
            m.append(row)
            row = []
        else: row.append(chr(x))
    c = Comp(p, None, cb)
    c.run()
    return m
コード例 #11
0
ファイル: both.py プロジェクト: vaderkvarn/aoc19
def run_b():
    def on_exit(comp):
        max_x = 0
        max_y = 0
        for (x, y) in visited:
            if x >= max_x: max_x = x + 1
            if y >= max_y: max_y = y + 1
        print('P2', max_x, max_y, 1)
        for y in range(max_y):
            for x in range(max_x):
                if (x, y) in visited:
                    print(visited[(x, y)], end=' ')
                else:
                    print(0, end=' ')
            print()

    visited[cur] = 1
    c = Comp(p_orig, get_input, cb, on_exit)
    c.run()
コード例 #12
0
ファイル: d17.py プロジェクト: otahontas/adventofcode
def first():
    global grid
    global w
    global h
    c = Comp(a)
    c.run()

    sa = []
    while c.outputs:
        sa.append(chr(c.get_one_output()))

    s = "".join(sa)
    grid = s.splitlines()
    grid = grid[:-1]

    w = len(grid[0])
    h = len(grid)

    def is_intersection(y, x):
        if y < 1 or x < 1 or y > h - 2 or x > w - 2:
            return False
        if (grid[y][x] == "#" and grid[y - 1][x] == "#"
                and grid[y + 1][x] == "#" and grid[y][x - 1] == "#"
                and grid[y][x + 1] == "#"):
            return True
        return False

    ans = 0
    start = (0, 0)

    for y in range(h):
        for x in range(w):
            if grid[y][x] == "^":
                start = (y, x)
            if is_intersection(y, x):
                ans += y * x

    print(ans)
    return start
コード例 #13
0
class Worker():
    def __init__(self, in_q, out_q, addr):
        self.in_q = in_q
        self.out_q = out_q
        self.addr = addr
        self.msg = []
        self.in_q.append(self.addr)
        self.comp = Comp(p[:], self.get_input, self.cb)
    
    def get_input(self, comp):
        if len(self.in_q) == 0:
            comp.pause()
            return -1
        
        return self.in_q.popleft()
    def cb(self, comp, x):
        self.msg.append(x)        
        if len(self.msg) == 3:
            self.out_q.append(tuple(self.msg))
            self.msg = []
    def run(self):
        self.comp.run()
コード例 #14
0
def get_coords(program):
    path = []
    p = (0, 0)
    coords = {(0, 0): 'X'}
    def get_input(c):
        nonlocal p
        next = 0
        for i in range(1, 5):
            new_p = next_p(p, i)
            if not new_p in coords:
                next = i
                break
        if next == 0: #backtrack 
            if len(path) == 0: #done
                c.pause()
                return
            next = rev(path[-1])
        path.append(next)
        return next

    def cb(c, o):
        nonlocal p
        new_p = next_p(p, path[-1])
        if o == 0:
            coords[new_p] = '#'
            path.pop()
        else:
            if new_p in coords: #backtrack
                path.pop()
                path.pop()
            elif o == 1: coords[new_p] = '.'
            elif o == 2: coords[new_p] = 'O'
            p = new_p

    c = Comp(program, get_input, cb, None)
    c.run()
    return coords
コード例 #15
0
#!/usr/bin/env python3
import sys
from comp import Comp

if len(sys.argv) == 1:
    print("Usage: python3 both.py <program>")
    exit(1)

p = [int(x) for x in open(sys.argv[1], "r").readline().split(',')]

def get_input(comp):
    return int(input())
def cb(comp, x):
    print(x)
def on_exit(comp):
    exit(0)
c = Comp(p, get_input, cb, on_exit)
c.run()
コード例 #16
0
ファイル: d17.py プロジェクト: otahontas/adventofcode
def second(p):
    dirs = {"U": (-1, 0), "R": (0, 1), "D": (1, 0), "L": (0, -1)}
    visited = set()

    def is_ok(p, move):
        new_point = (p[0] + dirs[d][0], p[1] + dirs[d][1])
        y = new_point[0]
        x = new_point[1]
        if y < 0 or x < 0 or y > h - 1 or x > w - 1:
            return False
        if grid[y][x] == ".":
            return False
        return True

    def find_next_dir(p):
        for d in dirs:
            y = p[0] + dirs[d][0]
            x = p[1] + dirs[d][1]
            if y < 0 or x < 0 or y > h - 1 or x > w - 1:
                continue
            if grid[y][x] == "#" and (y, x) not in visited:
                return d

    def get_relative_dir(prev_dir, new_dir):
        if prev_dir == "U":
            return "R" if new_dir == "R" else "L"
        if prev_dir == "L":
            return "R" if new_dir == "U" else "L"
        if prev_dir == "D":
            return "R" if new_dir == "L" else "L"
        if prev_dir == "R":
            return "R" if new_dir == "D" else "L"

    path = []
    prev_dir = "U"
    while True:
        d = find_next_dir(p)
        if not d:
            break
        rel_dir = get_relative_dir(prev_dir, d)
        prev_dir = d
        path.append(rel_dir)
        visited.add(p)
        steps = 0
        while is_ok(p, dirs[d]):
            p = (p[0] + dirs[d][0], p[1] + dirs[d][1])
            visited.add(p)
            steps += 1
        path.append(str(steps))

    # Print path
    print(",".join(path))

    # Based on previous print result, check what routines to use. Mine are:
    M = "A,A,B,C,C,A,C,B,C,B"
    A = "L,4,L,4,L,6,R,10,L,6"
    B = "L,12,L,6,R,10,L,6"
    C = "R,8,R,10,L,6"

    a[0] = 2
    c = Comp(a)

    def give_ascii_input(s, c):
        for asc in s:
            c.add_one_input(ord(asc))
        c.add_one_input(10)  # add line break

    give_ascii_input(M, c)
    give_ascii_input(A, c)
    give_ascii_input(B, c)
    give_ascii_input(C, c)
    give_ascii_input("n", c)

    c.run()
    o = c.get_all_outputs()
    for output in o:
        if output > 127:
            print(output)