Beispiel #1
0
def p2(inp, debug=False):
    if DISPLAY:
        import pygame as pg
    mode = MPX
    data = np.array([0, 0, 0])

    if DISPLAY:
        pg.init()
        screen = pg.display.set_mode((GRIDSIZE[0] * 16, GRIDSIZE[1] * 16))
        grid = pg.surface.Surface(GRIDSIZE)

    ballpos = 0
    padpos = 0
    score = 0

    def display():
        pg.transform.scale(grid, (GRIDSIZE[0] * 16, GRIDSIZE[1] * 16), screen)
        pg.display.flip()

    def events():
        for ev in pg.event.get():
            if ev.type == pg.QUIT:
                sys.exit(0)

    def read():
        nonlocal display, events, ballpos, padpos
        if DISPLAY:
            events()
            display()
        return np.sign(padpos - ballpos)

    def write(val):
        nonlocal mode, data, grid, ballpos, padpos, score
        data[mode] = val

        if mode == MTI:
            if data[MPX] == -1 and data[MPY] == 0:
                score = data[MTI]
            else:
                if DISPLAY:
                    pg.draw.circle(grid, COLORS[data[MTI]],
                                   (data[MPX], data[MPY]), 0)
                if data[MTI] == 3:
                    ballpos = data[MPX]
                elif data[MTI] == 4:
                    padpos = data[MPX]

        mode = (mode + 1) % MMX

    robot = IntComputer("2" + inp[1:],
                        name="Day 13",
                        readf=read,
                        writef=write,
                        debug=debug)
    robot.run()

    return score
Beispiel #2
0
def bothParts(inp, debug=False):
    grid = np.zeros((GRIDSIZE, GRIDSIZE), dtype=np.int8)
    rX, rY, rA = GRIDMID, GRIDMID, 0
    readingColor = True

    seen = set()

    def read():
        nonlocal grid, rX, rY
        return grid[rX, rY]

    def write(val):
        nonlocal grid, rX, rY, rA, readingColor
        if readingColor:
            grid[rX, rY] = val
            seen.add((rX, rY))
        else:
            if val == 0:
                rA = np.mod(rA - np.pi / 2, np.pi * 2)
            else:
                rA = np.mod(rA + np.pi / 2, np.pi * 2)
            rX += int(np.sin(rA))
            rY += int(np.cos(rA))
        readingColor = not readingColor

    robot = IntComputer(inp, name="Day 11", readf=read, writef=write, debug=debug)

    robot.run()
    p1 = len(seen)

    ### Part 2

    grid.fill(0)
    grid[GRIDMID, GRIDMID] = 1
    rX, rY, rA = GRIDMID, GRIDMID, (-np.pi / 2)
    readingColor = True

    robot.run()

    coords = np.argwhere(grid)
    mx, my = coords.min(axis=0)
    Mx, My = coords.max(axis=0)
    img = grid[mx : Mx + 1, my : My + 1]

    return (
        p1,
        "\n".join(map(lambda row: "".join(map(str, row)), img))
        .replace("0", " ")
        .replace("1", "█"),
    )
Beispiel #3
0
def scan():
    global text, in_buf
    s = 0
    c = IntComputer(text, f_in)
    rep = {0: '.', 1: '#'}
    f_edges = []
    r_edges = []
    points = {}
    for x in range(1074, 1179):
        in_beam = False
        for y in range(1724, 1830):
            in_buf.append(x)
            in_buf.append(y)
            c.run()
            if c.output:
                o = c.output.pop()
                s += o
                if o == 1 and not in_beam:
                    f_edges.append((x, y))
                    in_beam = True
                if in_beam and o == 0:
                    r_edges.append((x, y - 1))
                    in_beam = False
                points[(x, y)] = o
                print(rep[o], end="")
            c.reset()
        print()

    import math
    f_thetas = []
    f_edges.pop(0)
    r_edges.pop(0)
    for p in f_edges:
        f_thetas.append(math.degrees(math.atan(p[0] / p[1])))

    r_thetas = []
    for p in r_edges:
        r_thetas.append(math.degrees(math.atan(p[0] / p[1])))

    print(f_thetas)
    print(r_thetas)

    f_avg = sum(f_thetas) / len(f_thetas)
    r_avg = sum(r_thetas) / len(r_thetas)

    print(f_avg)
    print(r_avg)
    print("min/max f", min(f_thetas), max(f_thetas))
    print("min/max r", min(r_thetas), max(r_thetas))
Beispiel #4
0
def p1(inp, debug=False):
  grid = np.zeros(GRIDSIZE, dtype=np.int8)
  mode = MPX
  data = np.array([0, 0, 0])

  def write(val):
    nonlocal grid, mode
    data[mode] = val

    if mode == MTI:
      grid[data[MPX], data[MPY]] = data[MTI]

    mode = (mode + 1) % MMX

  robot = IntComputer(inp, name="Day 13", writef=write, debug=debug)

  robot.run()

  return grid[grid == 2].size
Beispiel #5
0
def run_turtle(code, start_color):
    global dirs
    turtle = IntComputer(code, [start_color], 2)
    painted = defaultdict(int)
    x, y = 0, 0
    d = 0

    ys = []
    xs = []

    turtle.run()
    while not turtle.halted:
        color = turtle.output.pop(0)
        turn = turtle.output.pop(0)
        painted[(x, y)] = color
        d = (d + (1 if turn == 1 else -1)) % 4
        x += dirs[d][0]
        y += dirs[d][1]
        ys.append(y)
        xs.append(x)
        turtle.inbuf.append(painted[(x, y)])
        turtle.run()
    return (painted, xs, ys)
Beispiel #6
0
def check(x, y):
    global text, in_buf
    outs = []
    c = IntComputer(text, f_in)
    in_buf.append(x)
    in_buf.append(y)
    c.run()
    outs.append(c.output.pop())
    c.reset()
    in_buf.append(x + 99)
    in_buf.append(y)
    c.run()
    outs.append(c.output.pop())
    c.reset()
    in_buf.append(x)
    in_buf.append(y + 99)
    c.run()
    outs.append(c.output.pop())
    return all(o == 1 for o in outs)
Beispiel #7
0
bx = 0
px = 0
joystick = 0


def get_joystick():
    global joystick
    return joystick


with open("input2.txt") as f:
    c = IntComputer(f.read(), get_joystick, 3)

part1 = False
c.run()

while not c.halted:
    x, y = c.output.pop(0), c.output.pop(0)
    tile_id = c.output.pop(0)
    if (x == -1 and y == 0):
        score = tile_id
    else:
        pixels[(x, y)] = tile_id
        if not part1 and tile_id == 2:
            boxcount += 1
        if tile_id == 4:
            #time.sleep(0.01)
            #print_screen(pixels, score)
            bx = x
        if tile_id == 3:
Beispiel #8
0
def bothParts(inp, debug=False):
    grid = np.full(GRIDSIZE, UNKNOWN)
    grid[GRIDMID] = FREE
    pos = np.array(GRIDMID)
    cur_move = 0
    last_turn = 0
    ret, ret2 = -1, -1

    if DISPLAY:
        import pygame as pg

        pg.init()
        screen = pg.display.set_mode(
            (GRIDSIZE[0] * SCALE, GRIDSIZE[1] * SCALE))
        gridsurf = pg.surface.Surface(GRIDSIZE)

    def display():
        for ev in pg.event.get():
            if ev.type == pg.QUIT:
                sys.exit(0)
        pg.surfarray.blit_array(gridsurf, grid)
        pg.draw.circle(gridsurf, 0xE8214C, tuple(pos), 0)
        pg.transform.scale(gridsurf,
                           (GRIDSIZE[0] * SCALE, GRIDSIZE[1] * SCALE), screen)
        pg.display.flip()

    def read():
        nonlocal cur_move, pos, last_turn
        move = cur_move

        ORDER = [-1, 0, 1]

        for o in ORDER:
            _move = (move + o) % len(DIRECTIONS)
            last_turn = o
            npos = pos + DIRECTIONS[_move][1]
            cell = grid[npos[0], npos[1]]
            if cell != WALL:
                break

        cur_move = _move
        return DIRECTIONS[cur_move][0]

    def write(val):
        nonlocal grid, pos, cur_move, ret, ret2
        npos = pos + DIRECTIONS[cur_move][1]
        if val == 0:
            if grid[npos[0], npos[1]] == UNKNOWN:
                cur_move = (cur_move - last_turn) % len(DIRECTIONS)
            grid[npos[0], npos[1]] = WALL
        elif val == 1:
            grid[npos[0], npos[1]] = FREE
            pos = npos
        elif val == 2:
            grid[npos[0], npos[1]] = OXYGEN
            pos = npos
            ret = dijkstra(grid, GRIDMID, MODE_LEN, display)
            ret2 = dijkstra(grid,
                            tuple(np.array(np.where(grid == OXYGEN))[:, 0]),
                            MODE_MAX, display)
            if ret != -1:
                return IntComputer.ABORT

        if DISPLAY:
            display()

    droid = IntComputer(inp,
                        readf=read,
                        name="Droid",
                        writef=write,
                        debug=debug)
    droid.run()

    return ret, ret2
Beispiel #9
0
from intcode import IntComputer

def f_in():
   return 0

with open("input.txt") as f:
   codestring = f.read()
   comp = IntComputer(codestring, f_in)

s = ''
comp.run()
while not comp.halted:
   s += chr(comp.output.pop(0))
   comp.run()

lines = s.strip().split('\n')

r_pos = None


rev_dir = {(0, -1): 0, (1, 0): 1, (0, 1): 2, (-1, 0): 3}
moves = [(0, -1), (1, 0), (0, 1), (-1, 0)]
r_ors = ['^', '>', 'v', '<']
r_dir = 0

total = 0
for i in range(1, len(lines)-1):
   for j in range(1, len(lines[i])-1):
      if lines[i][j] == '#':
         if lines[i][j+1] == '#' and lines[i][j-1] == '#' and lines[i+1][j] == '#' and lines[i-1][j] == '#':
            total += i * j