Exemplo n.º 1
0
 def __init__(self, brain_code) -> None:
     self.brain = IntComputer(
         code=brain_code,
         inputs=self.get_panel_color  # All black
     )
     self.panels = {}
     self.curr_pos = Point(0, 0)
     self.curr_dir = 0
Exemplo n.º 2
0
 def __init__(self, ip, nic, port):
     self.ip = ip
     self.port = port
     packet_getter = self.get_packets()
     self.nic = IntComputer(code=nic, inputs=lambda _: next(packet_getter))
     self.idle = False
     self.idle_time = 0
Exemplo n.º 3
0
 def __init__(self, remote_controller):
     self.droid = IntComputer(code=remote_controller, inputs=self.move)
     self.curr_pos = Point(x=0, y=0)
     self.curr_path = []
     self.tried = defaultdict(set)
     self.command_generator = self.get_next()
     self.backtracking = False
     self.last_dir = None
     self.done = False
Exemplo n.º 4
0
 def __init__(self, code, blacklist=None, avoid=None):
     self.input_gen = self.next_dir()
     self.droid = IntComputer(code=code,
                              inputs=lambda _: next(self.input_gen))
     self.blacklist = blacklist
     self.droid_response = ""
     self.seen = defaultdict(set)
     self.path = []
     self.pos = Point(0, 0)
     self.items = []
     self.avoid = avoid or []
Exemplo n.º 5
0
def check_point(row, col):
    def pos_gen():
        yield col  # column is the x position
        yield row  # row is the y position

    pos = pos_gen()
    drone_sim = IntComputer(code=list(sim_code), inputs=lambda _: next(pos))
    for _ in drone_sim:
        pass
    assert len(drone_sim.outputs) == 1
    res = drone_sim.outputs.pop()
    # print(f"{row} {col} => {res}")
    return res
Exemplo n.º 6
0
class Robot(object):
    DIRECTIONS = (
        Point(0, -1),  # UP
        Point(1, 0),  # RIGHT
        Point(0, 1),  #DOWN
        Point(-1, 0),  #LEFT
    )

    TEST_INSTR = [(0, 1, 0), (0, 0, 0), (0, 1, 0), (0, 1, 0), (1, 0, 1),
                  (0, 1, 0), (0, 1, 0)]

    def __init__(self, brain_code) -> None:
        self.brain = IntComputer(
            code=brain_code,
            inputs=self.get_panel_color  # All black
        )
        self.panels = {}
        self.curr_pos = Point(0, 0)
        self.curr_dir = 0

    def get_panel_color(self, _):
        if self.curr_pos not in self.panels:
            return 0
        return self.panels[self.curr_pos]

    def paint_panel(self, color, turn_dir):
        assert color in (0, 1) and turn_dir in (0, 1)
        self.panels[self.curr_pos] = color
        if turn_dir:
            self.curr_dir += 1
        else:
            self.curr_dir -= 1
        self.curr_dir %= len(Robot.DIRECTIONS)
        self.curr_pos += Robot.DIRECTIONS[self.curr_dir]

    def paint(self):
        for _ in self.brain:
            if len(self.brain.outputs) == 2:
                # print(self.curr_pos, f"color={0 if self.curr_pos not in self.panels else self.panels[self.curr_pos]} dir={self.curr_dir}", end=" -> ")
                self.paint_panel(*self.brain.outputs)
                self.brain.outputs = []
                # print(self.curr_pos, f"color={color} dir={robo_dir}")

    def test(self):
        for instr in Robot.TEST_INSTR:
            assert self.brain.get_input() is instr[0]
            print(self.curr_dir, self.curr_pos)
            self.paint_panel(*instr[1:])
        print(self.curr_dir, self.curr_pos)
Exemplo n.º 7
0
    def __eq__(self, other):
        if isinstance(other, Instruction):
            return self.task == other.task
        return False

    def __repr__(self):
        return f"Instruction({self.task})"

    def __str__(self):
        return f"{self.task if not isinstance(self.task, Turn) else self.task.value}"


# Get scaffold image
robot_code = [int(i) for i in puzzle_input.split(",")]
camera_image = ""
computer = IntComputer(code=list(robot_code))
for _ in computer:
    if computer.outputs:
        out = computer.outputs.pop()
        # print(out)
        camera_image += chr(out)

print(camera_image)
# Parse locations on scaffold image
camera_image = camera_image.strip().splitlines()
width = len(camera_image[0])
height = len(camera_image)

start_dir = Directions.NORTH
start_pos = None
scaffold = set()
Exemplo n.º 8
0
#!/usr/bin/env python3
# Imports
from collections import defaultdict
from intcode_parser import IntComputer

from aoc import AdventOfCode

# Input Parse
puzzle = AdventOfCode(year=2019, day=13)
puzzle_input = puzzle.get_input()

# Actual Code
arcade_machine = [int(i) for i in puzzle_input.split(",")]
computer = IntComputer(code=list(arcade_machine))

# Get map
game_map = {}
walls = set()
blocks = []
paddle_pos = None
last_ball_pos = None
ball_pos = None


def shift_joystick(idx):
    # draw_map()
    if last_ball_pos:
        ball_x_dir = ball_pos[0] - last_ball_pos[0]
        next_x = ball_x_dir + ball_pos[0]
        if ball_x_dir * (next_x - paddle_pos[0]) > 1:
            return ball_x_dir
Exemplo n.º 9
0
#!/usr/bin/env python3
# Imports
from itertools import permutations
from intcode_parser import IntComputer
from aoc import AdventOfCode

# Input Parse
puzzle = AdventOfCode(year=2019, day=7)
puzzle_input = puzzle.get_input()
# puzzle_input = "3,23,3,24,1002,24,10,24,1002,23,-1,23,101,5,23,23,1,24,23,23,4,23,99,0,0"

# Actual Code
code = [int(line) for line in puzzle_input.split(",")]

max_output = 0
for amp_phases in permutations(range(5)):
    amp_input = 0
    for amp_idx in range(5):
        amp = IntComputer(list(code), inputs=(lambda phase: lambda idx: phase if idx == 0 else amp_input )(amp_phases[amp_idx]))
        for _ in amp:
            pass
        assert len(amp.outputs) == 1
        amp_input = amp.outputs[0]
        # print(amp_input)
    max_output = max(amp_input, max_output)
    if max_output == amp_input:
        print(amp_phases, max_output)

# Result
print(max_output)
Exemplo n.º 10
0
#!/usr/bin/env python3
# Imports
from mapping import Directions, Point
from enum import Enum
from intcode_parser import IntComputer
from aoc import AdventOfCode

# Input Parse
puzzle = AdventOfCode(year=2019, day=17)
puzzle_input = puzzle.get_input()

camera_image = ""
computer = IntComputer(code=[int(i) for i in puzzle_input.split(",")])
for _ in computer:
    if computer.outputs:
        out = computer.outputs.pop()
        # print(out)
        camera_image += chr(out)

camera_image = camera_image.strip().splitlines()
width = len(camera_image[0])
height = len(camera_image)
total_alignment = 0
for y, row in enumerate(camera_image):
    for x, pixel in enumerate(row):
        pos = Point(x, y)
        if pixel != "#":
            continue
        possible_turns = 0
        for direction in Directions:
            new_pos = pos + direction.value
Exemplo n.º 11
0
 def __init__(self, parser, script):
     self.script = script
     self.script_input = self.script_reader()
     self.parser = IntComputer(code=parser, inputs=self.feed_script)
Exemplo n.º 12
0
#!/usr/bin/env python3
# Imports
from aoc import AdventOfCode
from intcode_parser import IntComputer
# Input Parse
puzzle = AdventOfCode(year=2019, day=9)
puzzle_input = puzzle.get_input()
# TESTS

# Actual Code
computer = IntComputer(code=[int(i) for i in puzzle_input.split(",")],
                       inputs=lambda _: 1)
for _ in computer:
    if computer.outputs:
        print(computer.outputs.pop())
# print(computer.outputs)

# Result
# print(computer.outputs)
Exemplo n.º 13
0
#!/usr/bin/env python3
# Imports
from aoc import AdventOfCode
from intcode_parser import IntComputer

# Input Parse
puzzle = AdventOfCode(year=2019, day=5)
puzzle_input = puzzle.get_input()
program = [int(code) for code in puzzle_input.split(",")]

# Actual Code
ship_id = 1
computer = IntComputer(code=program, inputs=lambda _: ship_id)
for curr_instr in computer:
    print(">", curr_instr)

# Result
print(computer.outputs)
Exemplo n.º 14
0
# Input Parse
puzzle = AdventOfCode(year=2019, day=19)
puzzle_input = puzzle.get_input()

# Actual Code
sim_code = [int(i) for i in puzzle_input.split(",")]

affected_points = 0
MAX_X, MAX_Y = 50, 50
for x in range(MAX_X):
    prev = affected_points
    for y in range(MAX_Y):

        def pos_gen():
            yield x
            yield y

        pos = pos_gen()
        drone_sim = IntComputer(code=list(sim_code),
                                inputs=lambda _: next(pos))
        for _ in drone_sim:
            pass
        assert len(drone_sim.outputs) == 1
        res = drone_sim.outputs.pop()
        print("#" if res else ".", end="")
        affected_points += res
    print(affected_points - prev)
print()

# Result
print(affected_points)
Exemplo n.º 15
0
class Robot(object):
    DIRECTIONS = (
        Point(0, -1),  # UP
        Point(1, 0),  # RIGHT
        Point(0, 1),  #DOWN
        Point(-1, 0),  #LEFT
    )

    TEST_INSTR = [(0, 1, 0), (0, 0, 0), (0, 1, 0), (0, 1, 0), (1, 0, 1),
                  (0, 1, 0), (0, 1, 0)]

    def __init__(self, brain_code) -> None:
        self.brain = IntComputer(
            code=brain_code,
            inputs=self.get_panel_color  # All black
        )
        self.panels = {}
        self.curr_pos = Point(0, 0)
        self.curr_dir = 0

    def get_panel_color(self, idx):
        if self.curr_pos not in self.panels:
            return 0 if idx != 0 else 1
        return self.panels[self.curr_pos]

    def paint_panel(self, color, turn_dir):
        assert color in (0, 1) and turn_dir in (0, 1)
        self.panels[self.curr_pos] = color
        if turn_dir:
            self.curr_dir += 1
        else:
            self.curr_dir -= 1
        self.curr_dir %= len(Robot.DIRECTIONS)
        self.curr_pos += Robot.DIRECTIONS[self.curr_dir]

    def paint(self):
        for _ in self.brain:
            if len(self.brain.outputs) == 2:
                # print(self.curr_pos, f"color={0 if self.curr_pos not in self.panels else self.panels[self.curr_pos]} dir={self.curr_dir}", end=" -> ")
                self.paint_panel(*self.brain.outputs)
                self.brain.outputs = []
                # print(self.curr_pos, f"color={color} dir={robo_dir}")

    def draw_panels(self):
        min_point = Point(x=0, y=0)
        max_point = Point(x=0, y=0)
        for point in self.panels:
            if point < min_point:
                min_point = point
            if point > min_point:
                max_point = point

        for y in range(min_point.y - 3, max_point.y + 3):
            for x in range(min_point.x - 3, max_point.x + 3):
                curr = Point(x=x, y=y)
                is_white = 0 if curr not in self.panels else self.panels[curr]
                print("▓" if is_white else "░", end="")
            print()

    def test(self):
        for instr in Robot.TEST_INSTR:
            assert self.brain.get_input() is instr[0]
            print(self.curr_dir, self.curr_pos)
            self.paint_panel(*instr[1:])
        print(self.curr_dir, self.curr_pos)