async def part1(grid_size: int):
    input = asyncio.Queue()
    output = asyncio.Queue()
    computer = IntcodeComputer('day19.txt', input, output)

    grid = [['?' for x in range(grid_size)] for y in range(grid_size)]
    affected_points = 0
    for x in range(grid_size):
        for y in range(grid_size):
            computer.reset()
            input.put_nowait(x)
            input.put_nowait(y)
            await computer.execute()
            result = output.get_nowait()

            if result == 1:
                affected_points += 1
                grid[y][x] = '#'
            else:
                grid[y][x] = '.'

    for line in grid:
        print(''.join(line))

    print(f'Part 1: {affected_points}')
class PaintingRobot:

    def __init__(self, program: [int], input: [int] = []):
        self.computer = IntcodeComputer(program, input)
        self.reset()

    def reset(self):
        self.direction = Direction.NORTH
        self.location = 0, 0
        self.seen_locations = set()
        self.painted_state = defaultdict(int)
        self.computer.reset()

    def run(self, program_input: [int] = []):
        self.computer.input = program_input
        while True:
            if self.computer.run_program(interrupting=True):
                break
            self.painted_state[self.location] = self.computer.output.pop()
            if self.computer.run_program(interrupting=True):
                break
            self.rotate_and_move(Rotation(self.computer.output.pop()))
            self.computer.input = [self.painted_state[self.location]]

    def rotate_and_move(self, rotate: Rotation):
        self.seen_locations.add(self.location)

        if rotate == Rotation.LEFT:
            self.direction -= 1
        elif rotate == Rotation.RIGHT:
            self.direction += 1
        else:
            raise Exception("Unexpected rotation: {rotate}")
        self.direction %= 4

        if self.direction == Direction.NORTH:
            self.location = self.location[0], self.location[1] + 1
        elif self.direction == Direction.EAST:
            self.location = self.location[0] + 1, self.location[1]
        elif self.direction == Direction.SOUTH:
            self.location = self.location[0], self.location[1] - 1
        elif self.direction == Direction.WEST:
            self.location = self.location[0] - 1, self.location[1]
        else:
            raise Exception(f"Unexpected direction: {self.direction}")
Beispiel #3
0
# Jump if there is a hole at A or B or C but only if there is ground at D.
springscript_1 = """\
NOT B J
NOT C T
OR T J
NOT A T
OR T J
AND D J
WALK
"""
droid_output = droid.run_ascii(springscript_1)
# print(droid_output)
print(droid_output.splitlines()[-1])  # 19362259

# part 2
droid.reset()
droid_output = droid.run_ascii()
# print(droid_output)
# Same as in part 1 but there must also be ground at H.
springscript_2 = """\
NOT B J
NOT C T
OR T J
NOT A T
OR T J
AND D J
OR H T
AND T J
RUN
"""
droid_output = droid.run_ascii(springscript_2)
Beispiel #4
0
# https://adventofcode.com/2019/day/2

import itertools

from intcode import IntcodeComputer

with open("../../input/2019-02-input.txt") as file:
    program = [int(i) for i in file.read().split(",")]

# part 1
program[1] = 12
program[2] = 2
computer = IntcodeComputer(program)
computer.run()
print(computer.intcode[0])  # 3101844

# part 2
target = 19690720
for noun, verb in itertools.product(range(100), repeat=2):
    computer.original_intcode[1] = noun
    computer.original_intcode[2] = verb
    computer.reset()
    computer.run()
    if computer.intcode[0] == target:
        break
print(100 * noun + verb)  # 8478
Beispiel #5
0
    tile = arcade.run()
    if x is None or y is None or tile is None:
        break
    grid[(x, y)] = tile

# part 1:
print(collections.Counter(grid.values())[2])  # 268

# part 2:
x_min = min(grid)[0]
x_max = max(grid)[0]
y_min = min(grid, key=lambda v: v[1])[1]
y_max = max(grid, key=lambda v: v[1])[1]
assert x_min == 0 and y_min == 0

arcade.reset()
arcade.intcode[0] = 2

tile_size = 20
score = 0
paddle_x = -1
ball_x = -1
show = False  # False for faster testing.
if show:
    pygame.init()
    display = pygame.display.set_mode((
        x_max * tile_size + tile_size,
        y_max * tile_size + tile_size)
    )
    colors = [
        pygame.Color("black"),        # background