コード例 #1
0
def part_2(loc=DEFAULT_INPUT):
    with open(loc) as f:
        memory = list(map(int, f.readline().rstrip().split(',')))
    grid = defaultdict(int)
    coord = (0, 0)
    grid[(0, 0)] = 1
    direction = (0, -1)
    ic = IntCode(memory, 1)
    while True:
        new_color = ic.get_output()
        if new_color[0]:
            break
        grid[coord] = new_color[1]
        turn = ic.get_output()
        if turn[0]:
            break
        direction = new_direction(direction, turn[1])
        coord = coord[0] + direction[0], coord[1] + direction[1]
        ic.add_inputs(1 if grid[coord] else 0)
    min_x = min(grid.keys(), key=lambda p: p[0])[0]
    min_y = min(grid.keys(), key=lambda p: p[1])[1]
    max_x = max(grid.keys(), key=lambda p: p[0])[0]
    max_y = max(grid.keys(), key=lambda p: p[1])[1]
    rows = []
    for y in range(min_y, max_y + 1):
        row = ''
        for x in range(min_x, max_x + 1):
            if grid[(x, y)]:
                row += '#'
            else:
                row += ' '
        rows.append(row)
    print('\n'.join(rows))
コード例 #2
0
ファイル: day7.py プロジェクト: cpaszul/advent-of-code-2019
def part_2(loc=DEFAULT_INPUT):
    with open(loc) as f:
        memory = list(map(int, f.readline().rstrip().split(',')))
    perms = permutations(range(5, 10))
    max_val = -1
    for perm in perms:
        a, b, c, d, e = perm
        a_comp = IntCode(memory, a)
        b_comp = IntCode(memory, b)
        c_comp = IntCode(memory, c)
        d_comp = IntCode(memory, d)
        e_comp = IntCode(memory, e)
        finished = False
        prev_e_res = 0
        while not finished:
            a_comp.add_inputs(prev_e_res)
            a_res = a_comp.get_output()
            b_comp.add_inputs(a_res[1])
            b_res = b_comp.get_output()
            c_comp.add_inputs(b_res[1])
            c_res = c_comp.get_output()
            d_comp.add_inputs(c_res[1])
            d_res = d_comp.get_output()
            e_comp.add_inputs(d_res[1])
            e_res = e_comp.get_output()
            if e_res[0]:
                finished = True
                max_val = max(max_val, prev_e_res)
            else:
                prev_e_res = e_res[1]
    return max_val
コード例 #3
0
ファイル: day15.py プロジェクト: cpaszul/advent-of-code-2019
def day_15(loc=DEFAULT_INPUT):
    with open(loc) as f:
        memory = list(map(int, f.readline().rstrip().split(',')))
    grid = {(0, 0): '.', (1, 0): '?', (-1, 0): '?', (0, 1): '?', (0, -1): '?'}
    droid = (0, 0)
    move_to_dir = {1: (0, -1), 2: (0, 1), 3: (-1, 0), 4: (1, 0)}
    ic = IntCode(memory)
    while '?' in grid.values():
        path = find_nearest_unknown(grid, droid)
        for m in path[:-1]:
            ic.add_inputs(m)
            ic.get_output()
            droid = (droid[0] + move_to_dir[m][0],
                     droid[1] + move_to_dir[m][1])
        m = path[-1]
        ic.add_inputs(m)
        status = ic.get_output()[1]
        if status == 0:
            wall = (droid[0] + move_to_dir[m][0], droid[1] + move_to_dir[m][1])
            grid[wall] = '#'
        else:
            droid = (droid[0] + move_to_dir[m][0],
                     droid[1] + move_to_dir[m][1])
            if status == 1:
                grid[droid] = '.'
            else:
                grid[droid] = 'O'
                target = droid
            for adj_cell, _ in adjacent(droid):
                if adj_cell not in grid:
                    grid[adj_cell] = '?'
    p1_res = shortest_path(grid, (0, 0), target)
    time = 0
    with_oxygen = [target]
    while '.' in grid.values():
        new_oxygen = []
        for cell in with_oxygen:
            all_adj = adjacent(cell)
            for adj_cell, _ in all_adj:
                if grid[adj_cell] == '.':
                    grid[adj_cell] = 'O'
                    new_oxygen.append(adj_cell)
        with_oxygen = new_oxygen
        time += 1
    return p1_res, time
コード例 #4
0
def part_1(loc=DEFAULT_INPUT):
    with open(loc) as f:
        memory = list(map(int, f.readline().rstrip().split(',')))
    grid = defaultdict(int)
    coord = (0, 0)
    direction = (0, -1)
    painted = set()
    ic = IntCode(memory, 0)
    while True:
        new_color = ic.get_output()
        if new_color[0]:
            break
        grid[coord] = new_color[1]
        painted.add(coord)
        turn = ic.get_output()
        if turn[0]:
            break
        direction = new_direction(direction, turn[1])
        coord = coord[0] + direction[0], coord[1] + direction[1]
        ic.add_inputs(grid[coord])
    return len(painted)
コード例 #5
0
ファイル: day13.py プロジェクト: cpaszul/advent-of-code-2019
def part_2(loc=DEFAULT_INPUT):
    with open(loc) as f:
        memory = list(map(int, f.readline().rstrip().split(',')))
    memory[0] = 2
    ic = IntCode(memory)
    score = -1
    paddle_x = 0
    while True:
        x = ic.get_output()
        y = ic.get_output()
        tile = ic.get_output()
        if x[0] or y[0] or tile[0]:
            return score
        if x[1] == -1 and y[1] == 0:
            score = tile[1]
        elif tile[1] == 3:
            paddle_x = x[1]
        elif tile[1] == 4:
            ball_x = x[1]
            if ball_x > paddle_x:
                ic.add_inputs(1)
            elif ball_x < paddle_x:
                ic.add_inputs(-1)
            else:
                ic.add_inputs(0)