示例#1
0
def single_run(program_to_run, phase_setting):
    last_result = 0
    for i in phase_setting:
        comp = IntcodeComputer(program_to_run.copy(), i)
        last_result = comp.run_with_input(last_result)

    return last_result
示例#2
0
def main():
    with open('./input') as file:
        raw = file.read()
        raw_list = [int(i) for i in raw.split(",")]

    program_list = [0] * len(raw_list) * 10

    for i in range(len(raw_list)):
        program_list[i] = raw_list[i]

    comp = IntcodeComputer(program_list)
    position = (0, 0)
    direction = (0, 1)
    colors = {position: 1}
    panels_painted = set()
    while comp.index >= 0:
        color_bit = comp.run_with_input(colors.get(position, 0))
        direction_bit = comp.run()

        colors[position] = color_bit
        if color_bit:
            panels_painted.add(position)

        if direction_bit:
            if direction == (0, 1):
                direction = (1, 0)
            elif direction == (1, 0):
                direction = (0, -1)
            elif direction == (0, -1):
                direction = (-1, 0)
            else:
                direction = (0, 1)
        else:
            if direction == (0, 1):
                direction = (-1, 0)
            elif direction == (1, 0):
                direction = (0, 1)
            elif direction == (0, -1):
                direction = (1, 0)
            else:
                direction = (0, -1)

        position = (position[0] + direction[0], position[1] + direction[1])

    output = []
    for y in range(-10, 10):
        row = []
        for x in range(-10, 80):
            if colors.get((x,y), 0):
                row.append('X')
            else:
                row.append(' ')
        output.append("".join(row))

    output.reverse()
    print("\n".join(output))
示例#3
0
def main():
    with open('./input') as file:
        raw = file.read()
        raw_list = [int(i) for i in raw.split(",")]

    program_list = [0] * len(raw_list) * 10

    for i in range(len(raw_list)):
        program_list[i] = raw_list[i]

    comp = IntcodeComputer(program_list.copy())
    output = []
    while comp.index >= 0:
        result = comp.run()
        if (comp.index > 0):
            output.append(chr(result))

    output = ("".join(output)).strip()

    rows = output.split("\n")
    width = len(rows[0])
    height = len(rows)

    intersections = []
    surroundings = [(1, 0), (-1, 0), (0, 1), (0, -1)]
    for i in range(1, width - 1):
        for j in range(1, height - 1):
            if rows[j][i] == '#':
                if all([rows[y + j][x + i] == '#' for x, y in surroundings]):
                    intersections.append((i, j))

    part1 = sum([x * y for x, y in intersections])

    # find_routine(width, height, rows)
    #[L,6,R,12,L,4,L,6,R,6,L,6,R,12,R,6,L,6,R,12,L,6,L,10,L,10,R,6,L,6,R,12,L,4,L,6,R,6,L,6,R,12,L,6,L,10,L,10,R,6,L,6,R,12,L,4,L,6,R,6,L,6,R,12,L,6,L,10,L,10,R,6]
    main = 'B,C,C,A,B,C,A,B,C,A\n'
    a = 'L,6,L,10,L,10,R,6\n'
    b = 'L,6,R,12,L,4,L,6\n'
    c = 'R,6,L,6,R,12\n'

    program_list[0] = 2
    comp = IntcodeComputer(program_list.copy())

    inputs = [main, a, b, c, 'n\n']

    for input in inputs:
        for char in input:
            comp.run_with_input(ord(char))

    part2 = -1
    while comp.index > 0:
        result = comp.run()
        if result > 0:
            part2 = result

    return part1, part2
示例#4
0
def single_run(data, setting):
    running_data = [
        IntcodeComputer(data.copy(), i)
        for i in setting]

    amp_pointer = 0
    last_result = 0
    while running_data[4].index >= 0:
        last_result = running_data[amp_pointer].run_with_input(last_result)
        amp_pointer = (amp_pointer + 1) % 5

    return running_data[4].current_result
示例#5
0
    def __init__(self):
        with open('./input') as file:
            raw = file.read()
            raw_list = [int(i) for i in raw.split(",")]

        program_list = [0] * len(raw_list) * 10

        for i in range(len(raw_list)):
            program_list[i] = raw_list[i]

        self.comp = IntcodeComputer(program_list)
        self.position = (0, 0)
        self.direction = EAST
        self.completed = False
        self.steps = 0
        self.visited = {self.position: self.steps}
        self.walls = set()
        self.trying_right = True
        self.mapped = False
        self.x_size = [0, 0]
        self.y_size = [0, 0]
示例#6
0
def main():
    with open('./input') as file:
        raw = file.read()
        program_list = [int(i) for i in raw.split(",")]

    bigger_list = [0] * len(program_list) * 10

    for i in range(len(program_list)):
        bigger_list[i] = program_list[i]

    comp = IntcodeComputer(bigger_list, 1)
    while comp.index >= 0:
        result = comp.run()
        if comp.index > 1:
            part1 = result

    comp = IntcodeComputer(bigger_list, 2)
    while comp.index >= 0:
        result = comp.run()
        if comp.index > 1:
            part2 = result

    return part1, part2
示例#7
0
class Day15Runner:
    def __init__(self):
        with open('./input') as file:
            raw = file.read()
            raw_list = [int(i) for i in raw.split(",")]

        program_list = [0] * len(raw_list) * 10

        for i in range(len(raw_list)):
            program_list[i] = raw_list[i]

        self.comp = IntcodeComputer(program_list)
        self.position = (0, 0)
        self.direction = EAST
        self.completed = False
        self.steps = 0
        self.visited = {self.position: self.steps}
        self.walls = set()
        self.trying_right = True
        self.mapped = False
        self.x_size = [0, 0]
        self.y_size = [0, 0]

    def main(self):
        while not self.completed:
            self.do_step()

        part1 = self.steps

        self.map_unknowns()

        while not self.oxygen_complete:
            self.do_oxygen_step()

        part2 = self.oxygen_steps

        return part1, part2

    def unknowns(self):
        result = []
        for x in range(-19, 20):
            for y in range(-19, 20):
                tst_pos = (x, y)
                if not tst_pos in self.walls and not tst_pos in self.visited:
                    result.append(tst_pos)
        return result

    def map_unknowns(self):
        self.oxygenated = set()
        self.oxygenated.add(self.oxygen_spot)
        self.oxygen_steps = 0
        self.oxygen_complete = False

        for x in range(-19, 20):
            self.walls.add((x, self.y_size[0] + 1))
            self.walls.add((x, self.y_size[1] + 1))
        for y in range(-19, 20):
            self.walls.add((self.x_size[0] - 1, y))
            self.walls.add((self.x_size[1] - 1, y))

        while not self.mapped:
            unk = self.unknowns()

            if len(unk) == 0:
                self.mapped = True
                return None

            adjacent = [NORTH, SOUTH, EAST, WEST]
            go = unk[0]

            if go[0] == self.x_size[0]:
                adjacent.remove(WEST)
            if go[0] == self.x_size[1]:
                adjacent.remove(EAST)
            if go[1] == self.y_size[0]:
                adjacent.remove(NORTH)
            if go[1] == self.y_size[1]:
                adjacent.remove(SOUTH)

            if all([self.next_position(go, x) in self.walls for x in adjacent]):
                self.walls.add(go)
            else:
                self.explore_position(go)

            self.check_mapped()

    def explore_position(self, pos):
        adjacent = [self.next_position(pos, x)
                    for x in [NORTH, SOUTH, EAST, WEST]]
        target = next(x for x in adjacent if x in self.visited)
        if target == self.position:
            target = self.oxygen_spot
        while self.position != target:
            self.do_step()

    def check_mapped(self):
        if len(self.walls) + len(self.visited) >= 1677:
            self.mapped = True

    def do_oxygen_step(self):
        self.oxygen_steps += 1

        for pos in list(self.oxygenated):
            adj = [self.next_position(pos, k) for k in [NORTH, SOUTH, EAST, WEST]]
            for k in adj:
                if k in self.visited:
                    self.oxygenated.add(k)

        if len(self.oxygenated) == len(self.visited):
            self.oxygen_complete = True

    def do_step(self):
        if self.trying_right:
            self.turn_right()
        else:
            self.turn_left()

        next = self.next_position()
        result = -1
        if not next in self.walls:
            result = self.comp.run_with_input(self.direction)
            if result == 0:
                self.walls.add(next)
                self.trying_right = False
                return result, next
            elif result == 1:
                self.trying_right = True
                if self.visited.get(next):
                    self.steps = self.visited.get(next)
                    self.position = next
                else:
                    self.steps += 1
                    self.visited[next] = self.steps
                    self.position = next
            elif result == 2:
                self.oxygen_spot = next
                self.visited[next] = self.steps
                self.position = next
                self.steps += 1
                self.completed = 2
        else:
            result = 0
            self.trying_right = False

        if next[0] < self.x_size[0]:
            self.x_size[0] = next[0]
        if next[1] > self.x_size[1]:
            self.x_size[1] = next[1]

        if next[0] < self.y_size[0]:
            self.y_size[0] = next[0]
        if next[1] > self.y_size[1]:
            self.y_size[1] = next[1]

        return result, next

    def turn_right(self):
        if self.direction == NORTH:
            self.direction = EAST
        elif self.direction == SOUTH:
            self.direction = WEST
        elif self.direction == EAST:
            self.direction = SOUTH
        elif self.direction == WEST:
            self.direction = NORTH

    def turn_left(self):
        for _ in range(3):
            self.turn_right()

    def next_position(self, position=None, direction=None):
        position = position or self.position
        direction = direction or self.direction
        if direction == NORTH:
            return (position[0], position[1] + 1)
        elif direction == SOUTH:
            return (position[0], position[1] - 1)
        elif direction == EAST:
            return (position[0] + 1, position[1])
        elif direction == WEST:
            return (position[0] - 1, position[1])
示例#8
0
def main():
    with open('./input') as file:
        raw = file.read()
        raw_list = [int(i) for i in raw.split(",")]

    program_list = [0] * len(raw_list) * 10

    for i in range(len(raw_list)):
        program_list[i] = raw_list[i]

    comp = IntcodeComputer(program_list)
    blocks = set()
    while comp.index >= 0:
        x = comp.run()
        y = comp.run()
        tile = comp.run()

        if tile == 2:
            blocks.add((x,y))
        elif tile == 4 and (x,y) in blocks:
            blocks.remove((x,y))

    part1 = len(blocks)

    program_list[0] = 2
    comp = IntcodeComputer(program_list)
    blocks = set()
    score = -1
    paddle_x = 10
    ball_x = 0
    ball_y = -1
    joystick = 0
    ball_vector = 1
    last_tile = -1
    while comp.index >= 0:
        if last_tile == 4:
            comp.queue_input(joystick)
        x = comp.run()
        y = comp.run()
        tile = comp.run()

        if tile == 2:
            blocks.add((x,y))
        elif tile == 4 and (x,y) in blocks:
            blocks.remove((x,y))
        elif tile == 3:
            paddle_x = x
            if paddle_x == 34:
                joystick = -1
        elif tile == 4:
            old_vector = ball_vector
            ball_vector = x - ball_x
            ball_x = x
            ball_y = y

            diff = ball_x - paddle_x
            if diff != 0:
                joystick = diff // abs(diff)
            else:
                joystick = 0
        elif x == -1 and y == 0:
            score = tile

        last_tile = tile

    part2 = score

    return part1, part2
示例#9
0
done = False

# Used to manage how fast the screen updates
clock = pygame.time.Clock()

with open('./input') as file:
    raw = file.read()
    raw_list = [int(i) for i in raw.split(",")]

program_list = [0] * len(raw_list) * 10

for i in range(len(raw_list)):
    program_list[i] = raw_list[i]

program_list[0] = 2
comp = IntcodeComputer(program_list)
blocks = set()
score = -1
paddle_x = 10
ball_x = 0
ball_y = -1
joystick = 0
last_tile = -1
grid = [[BLACK] * 50 for _ in range(50)]
WALL = WHITE
BLANK = BLACK
BALL = RED
BRICK = BLUE
PADDLE = GREEN

painted = False