Exemple #1
0
def main():
    with open("dec9/input.txt", "r") as file:
        memory = list(map(int, file.read().split(",")))

    ic = IntCode(memory, inp=[2])
    ic.start()
    return ic.out[0]
Exemple #2
0
def test(x, y):
    if x < 0 or y < 0:
        return False
    ic = IntCode(memory.copy())
    ic.inp += [x, y]
    ic.start()
    out = ic.popout()
    return out[0] == 1
Exemple #3
0
 def __init__(self, coord, panel: Panel):
     self.panel = panel
     self.direction = Robot.DIRECTION.UP
     self.coord = coord
     self.memory = Memory()
     with open('input.txt') as f:
         s = f.readline()
         tape = list(map(int, s.split(',')))
     self.brain = IntCode(tape, self.__brain_input(), self.__brain_output)
def run_amplifier_loop(config: [int], tape=TAPE):
    inputs = [Memory() for _ in config]
    outputs = [mem.store for mem in inputs]
    machines = []
    for x in range(len(config)):
        m_in = inputs[x]
        m_in.store(config[x])
        m_out = outputs[(x + 1) % len(config)]
        machines.append(IntCode(tape, m_in, m_out))

    inputs[0].store(0)

    while machines[-1].status != STATUS_CODES.FINISHED:
        for machine in machines:
            machine.run()
    return inputs[0].memory[0]
Exemple #5
0
def main():
    with open("dec7/input.txt", "r") as file:
        memory = list(map(int, file.read().split(",")))
    m = 0
    for p in itertools.permutations(list(range(5))):
        index = 0
        currinput = [0]
        a, b, c, d, e = p
        ic_a = IntCode(memory.copy(), inp=[a])
        ic_b = IntCode(memory.copy(), inp=[b])
        ic_c = IntCode(memory.copy(), inp=[c])
        ic_d = IntCode(memory.copy(), inp=[d])
        ic_e = IntCode(memory.copy(), inp=[e])
        ics = [ic_a, ic_b, ic_c, ic_d, ic_e]
        for ic in ics:
            ics[index].inp += currinput
            ics[index].start()
            currinput = ics[index].popout()
            index = (index + 1) % 5
        ic_a.inp.append(0)
        ic_a.start()
        m = max(m, currinput[0])
    return m
Exemple #6
0
import sys, os
sys.path.append(os.getcwd())
from util.intcode import IntCode

with open("dec13/input.txt", "r") as file:
    memory = list(map(int, file.read().split(",")))

ic = IntCode(memory)
ic.start()

k = [ic.out[i] for i in range(2, len(ic.out), 3)]

print(k.count(2))
Exemple #7
0
screen = pygame.display.set_mode((440, 240))

colors = {
    0: (0, 0, 0),
    1: (255, 255, 255),
    2: (255, 0, 0),
    3: (0, 255, 0),
    4: (0, 0, 255)
}

with open("dec13/input.txt", "r") as file:
    memory = list(map(int, file.read().split(",")))
memory[0] = 2

ic = IntCode(memory)
move = 0
ballpos = 0
paddlepos = 0

images = []

img = Image.new("RGB", (44, 24), (0, 0, 0))
img2 = img.resize((440, 240))
images.append(img)
pixels = img.load()
raw_str = img2.tobytes("raw", "RGB")
pg = pygame.image.fromstring(raw_str, (440, 240), "RGB")
while True:
    screen.fill((0, 0, 0))
    if ballpos < paddlepos:
Exemple #8
0
import sys, os, pygame

sys.path.append(os.getcwd())
from util.intcode import IntCode
from PIL import Image

with open("dec15/input.txt", "r") as file:
    memory = list(map(int, file.read().split(",")))

ic = IntCode(memory.copy())
direction = 2
position = (0, 0)
order = [2, 4, 1, 3]
directions = {1: (0, -1), 2: (0, 1), 3: (-1, 0), 4: (1, 0)}

data = {(0, 0): 3}

screen = pygame.display.set_mode((430, 430))
clock = pygame.time.Clock()

minx = -22
miny = -22
maxx = 20
maxy = 20

done = False
while not done:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN and event.key in (
                pygame.K_RIGHT, pygame.K_LEFT, pygame.K_UP, pygame.K_DOWN):
            if event.key == pygame.K_RIGHT:
Exemple #9
0
class Robot:
    DIRECTION = Enum('DIRECTION', 'UP RIGHT DOWN LEFT')

    direction: DIRECTION
    coord: (int, int)
    brain: IntCode
    panel: Panel
    memory: Memory

    def __init__(self, coord, panel: Panel):
        self.panel = panel
        self.direction = Robot.DIRECTION.UP
        self.coord = coord
        self.memory = Memory()
        with open('input.txt') as f:
            s = f.readline()
            tape = list(map(int, s.split(',')))
        self.brain = IntCode(tape, self.__brain_input(), self.__brain_output)

    def run(self):
        self.brain.run()

    def act(self, colour, rotation_bit):
        self.panel.set(self.coord, colour)
        self.change_direction(rotation_bit)
        self.move_forward()

    def move_forward(self):
        dx, dy = {
            Robot.DIRECTION.UP: (0, 1),
            Robot.DIRECTION.RIGHT: (1, 0),
            Robot.DIRECTION.DOWN: (0, -1),
            Robot.DIRECTION.LEFT: (-1, 0)
        }[self.direction]
        x, y = self.coord
        self.coord = (x + dx, y + dy)

    def change_direction(self, rotation_bit):
        if rotation_bit == 0:
            self.__turn_left()
        elif rotation_bit == 1:
            self.__turn_right()
        else:
            raise ValueError()

    def __turn_left(self):
        self.direction = {
            Robot.DIRECTION.UP: Robot.DIRECTION.LEFT,
            Robot.DIRECTION.RIGHT: Robot.DIRECTION.UP,
            Robot.DIRECTION.DOWN: Robot.DIRECTION.RIGHT,
            Robot.DIRECTION.LEFT: Robot.DIRECTION.DOWN
        }[self.direction]

    def __turn_right(self):
        self.direction = {
            Robot.DIRECTION.UP: Robot.DIRECTION.RIGHT,
            Robot.DIRECTION.RIGHT: Robot.DIRECTION.DOWN,
            Robot.DIRECTION.DOWN: Robot.DIRECTION.LEFT,
            Robot.DIRECTION.LEFT: Robot.DIRECTION.UP
        }[self.direction]

    def __brain_output(self, v):
        self.memory.store(v)
        assert len(self.memory.memory) <= 2
        if len(self.memory.memory) == 2:
            self.act(*list(self.memory))

    def __brain_input(self):
        while True:
            yield self.panel.get(self.coord)
Exemple #10
0
import sys, os
sys.path.append(os.getcwd())
from util.intcode import IntCode

with open("dec5/input.txt", "r") as file:
    field = list(map(int, file.read().split(",")))

ic = IntCode(field)
ic.inp.append(1)
ic.start()
print(ic.out[-1])
Exemple #11
0
import sys, os
sys.path.append(os.getcwd())
from util.intcode import IntCode

with open("dec21/input.txt", "r") as file:
    memory = list(map(int, file.read().split(",")))

instructions = "NOT C J\nAND D J\nNOT A T\nOR T J\nWALK"
ic = IntCode(memory)
ic.inp += list(map(ord, list(instructions)))
ic.start()

print(ic.popout())
Exemple #12
0
import sys, os, imageio, colorsys
sys.path.append(os.getcwd())
from util.intcode import IntCode
from PIL import Image

with open("dec11/input2.txt", "r") as file:
    field = list(map(int, file.read().split(",")))

ic = IntCode(field)
# grid = {(0, 0): 1}
grid = {}
x, y = 0, 0
d = 0

# minx = min(g[0] for g in grid.keys())
# miny = min(g[1] for g in grid.keys())
# maxx = max(g[0] for g in grid.keys())
# maxy = max(g[1] for g in grid.keys())
# minx = -40
# miny = -74
# maxx = 31
# maxy = 13
minx = -28
miny = -24
maxx = 50
maxy = 25

images = []
steps = 0
hue = 0
while True:
Exemple #13
0
import sys, os

sys.path.append(os.getcwd())
from util.intcode import IntCode
from PIL import Image

with open("dec11/input2.txt", "r") as file:
    field = list(map(int, file.read().split(",")))

ic = IntCode(field)
grid = {(0, 0): 1}
x, y = 0, 0
d = 0

while True:
    if (x, y) in grid:
        color = grid[x, y]
    else:
        color = 0
    ic.inp.append(color)
    ic.start()
    if len(ic.out) < 2:
        break
    newcolor, direction = ic.popout()
    grid[x, y] = newcolor
    if direction == 0:
        d = (d - 1) % 4
    else:
        d = (d + 1) % 4

    if d == 0: y -= 1
Exemple #14
0
screen = pygame.display.set_mode((440, 240))
clock = pygame.time.Clock()

colors = {
    0: (0, 0, 0),
    1: (255, 255, 255),
    2: (255, 0, 0),
    3: (0, 255, 0),
    4: (0, 0, 255)
}

with open("dec13/input.txt", "r") as file:
    memory = list(map(int, file.read().split(",")))
memory[0] = 2

ic = IntCode(memory)
move = 0
ballpos = 0
paddlepos = 0

images = []

img = Image.new("RGB", (44, 24), (0, 0, 0))
img2 = img.resize((440, 240))
images.append(img)
pixels = img.load()
raw_str = img2.tobytes("raw", "RGB")
pg = pygame.image.fromstring(raw_str, (440, 240), "RGB")
joystick = 0
while True:
    screen.fill((0, 0, 0))
Exemple #15
0
import sys, os
sys.path.append(os.getcwd())
from util.intcode import IntCode
from PIL import Image

with open("dec15/input.txt", "r") as file:
    memory = list(map(int, file.read().split(",")))

ic = IntCode(memory.copy())
direction = 2
position = (0, 0)
order = [2, 4, 1, 3]
directions = {1: (0, -1), 2: (0, 1), 3: (-1, 0), 4: (1, 0)}

data = {(0, 0): 3}

while True:
    ic.inp.append(direction)
    ic.start()
    out = ic.popout()[0]
    dx, dy = directions[direction]
    if out != 0:
        position = (position[0] + dx, position[1] + dy)
    if position not in data:
        data[position] = out
    if out == 0:
        direction = order[(order.index(direction) + 1) % 4]
    else:
        direction = order[(order.index(direction) - 1) % 4]
    if len(data) == 799:
        break
Exemple #16
0
import sys, os
sys.path.append(os.getcwd())
from util.intcode import IntCode

with open("dec17/input.txt", "r") as file:
    memory = list(map(int, file.read().split(",")))

ic = IntCode(memory)
ic.start()
string = "".join(chr(k) for k in ic.popout())
lines = string.strip().split("\n")
print(lines)
total = 0
for x in range(1, len(lines[0]) - 2):
    for y in range(1, len(lines) - 2):
        if lines[y][x] == "#" and lines[y - 1][x] == "#" and lines[
                y + 1][x] == "#" and lines[y][x +
                                              1] == "#" and lines[y][x -
                                                                     1] == "#":
            total += y * x
print(total)