Esempio n. 1
0
    def paint(self):
        opCode = None
        base=None
        nOut = 1
        currDir='UP'
        currPos=[self.start[0], self.start[1]]
        self.hull[currPos[0]][currPos[1]]=self.startColor
        pointer = None
        processedInstr = None
        while opCode != 99:
            
            computer = IntcodeComputer(processedInstr if processedInstr else
                                    self.instructions, [self.hull[currPos[0]][currPos[1]]], pointer if pointer else 0,base if base else 0)
            if not nOut % 2:
                pointer, outputDirection, processedInstr, opCode,base = computer.run('loop')
                
                nextD,nextP=self.nextPos(currPos,currDir, outputDirection)
                nOut += 1
                currPos=nextP
                currDir=nextD
            
            else:
                pointer, outputColor, processedInstr, opCode,base = computer.run('loop')
                
                if outputColor==0:
                    self.hull[currPos[0]][currPos[1]]=0
                if outputColor==1:
                    self.hull[currPos[0]][currPos[1]]=1 
                nOut += 1

                self.painted.setdefault(str(currPos[0])+str(currPos[1]),1 )
                
        return self.painted, self.hull
Esempio n. 2
0
def createMem(str): return list(map(int, str.strip("\n").split(",")))


f = open("aoc19.txt", "r")
mem = createMem(f.readline())

sz = 3

inputs = []
r, c = 5, 0
while True:
    r, c = r+1, c+1
    machine = IntcodeComputer(mem, list((r, c)))
    while(not machine.halted):
        machine.run()
    outputs = machine.output[:]
    while(outputs != [1]):
        print('ayy', r, c)
        c += 1
        mac = IntcodeComputer(mem, list((r, c)))
        while(not mac.halted):
            mac.run()
        outputs = mac.output[:]

    machine = IntcodeComputer(mem, list((r-(sz-1), c)))
    while(not machine.halted):
        machine.run()
    if machine.output != [1]:
        continue
    #print(r, c, "2")
Esempio n. 3
0
import sys
from IntcodeComputer import IntcodeComputer

f = open(sys.argv[1], "r")
computer = IntcodeComputer([int(element) for element in f.read().split(",")])
computer.set_phase(None)
computer.set_signal(1)
print(computer.run())
Esempio n. 4
0
import sys
from IntcodeComputer import IntcodeComputer
import copy

f = open(sys.argv[1], 'r')

Intcode = [int(element) for element in f.read().split(",")]
# computer = IntcodeComputer(Intcode[:], stop_at_print=True, DEBUG=True)

space_map = dict()
dust = 0
count = 0
for y in range(0, 50):
  for x in range(0, 50):
    comp = IntcodeComputer(Intcode[:], stop_at_print=True, DEBUG=False)
    comp.set_phase(x)
    comp.set_signal(y)
    code = comp.run()
    count += int(code)
    print code, 
  print ''

print (count)
Esempio n. 5
0
# for r,c in intersections:
# 	alignment += r*c
# print(f'Part 1: {alignment}')


def string_to_ascii_list(s):
    res = []
    for ch in s:
        res.append(ord(ch))
    res.append(10)
    return res


routine = "A,B,A,C,A,B,A,C,B,C"
funcA = "R,4,L,12,L,8,R,4"
funcB = "L,8,R,10,R,10,R,6"
funcC = "R,4,R,10,L,12"

routine = string_to_ascii_list(routine)
funcA = string_to_ascii_list(funcA)
funcB = string_to_ascii_list(funcB)
funcC = string_to_ascii_list(funcC)
no = string_to_ascii_list("n")
inpoot = routine + funcA + funcB + funcC + no
print(inpoot)
# input mem[0] from 1 -> 2 make robot woke

while not machine.halted:
    v = machine.run(inpoot)
    print(v)
Esempio n. 6
0
def next_input():
  global game_map
  global ball
  global paddle
  # for y in range(0, 25):
  #   for x in range(0, 42):
  #     if (x, y) in game_map and game_map[(x, y)] != 0:
  #       print (game_map[(x, y)], end='')
  #     else:
  #       print (' ', end='')
  #   print ('')
  # print("_________________________________________________________________________________")
  code_input_val = -1 if paddle > ball else 1 if paddle < ball else 0
  return code_input_val

computer = IntcodeComputer([int(element) for element in f.read().split(",")], stop_at_print=True, DEBUG=False, input_func=next_input)
computer.set_mem(0, 2)
ball = -1
paddle = -1
while True:
  x = computer.run()
  if x == None:
    break
  y = computer.run()
  tile_id = computer.run()
  if x == -1 and y == 0:
    print ('Score', tile_id)
  ball = x if tile_id == 4 else ball
  paddle = x if tile_id == 3 else paddle
  game_map[(x,y)] = tile_id
Esempio n. 7
0
    Second, it will output a value indicating the direction the robot should turn:
        0 means it should turn left 90 degrees, and 1 means it should turn right 90 degrees.
'''


def createMem(str):
    return list(map(int, str.strip("\n").split(",")))


f = open("aoc11.txt", "r")

robot = IntcodeComputer(createMem(f.readline()))
direct, pos, visited = 0, [0, 0], {}
increments = {0: (0, 1), 1: (1, 0), 2: (0, -1), 3: (-1, 0)}
while (not robot.halted):
    robot.run([visited.get(tuple(pos), 0)])
    robot.run()

    if robot.output:
        visited[tuple(pos)] = robot.output[0]
        direct += 1 if robot.output[1] == 1 else -1
        if direct == 4: direct = 0
        if direct == -1: direct = 3
        inc = increments[direct]
        pos = ([i + j for i, j in zip(pos, inc)])
        robot.output.clear()
# print(emergencyHullvisited)
print("Part 1:", len(visited))

print("---------------------------")
print("Part 2:")
Esempio n. 8
0
import sys
from IntcodeComputer import IntcodeComputer

f = open(sys.argv[1], "r")
program = [int(element) for element in f.read().split(",")]
program[1] = 12  #noun
program[2] = 2  #verb
computer = IntcodeComputer(program)
computer.run()
print(computer.fetch_mem(0))
Esempio n. 9
0
import csv
from itertools import permutations

from IntcodeComputer import IntcodeComputer

# Prepare instructions
with open('input.txt') as csv_file:
    programInstructions = list(csv.reader(csv_file, delimiter=','))[0]

programInstructions = [int(instr) for instr in programInstructions]

# Run computer
inputValues = [1, 2]
for inputValue in inputValues:
    computer = IntcodeComputer(programInstructions, [inputValue])
    result = computer.run()
    print(f'Input: {inputValue} --->  Output: {result[1]}')
Esempio n. 10
0
    counter += 1
    return ord(val)


computer = IntcodeComputer([int(element) for element in f.read().split(",")],
                           stop_at_print=True,
                           DEBUG=False,
                           input_func=next_input)
computer.set_mem(0, 2)

space_map = dict()
# intersections = set()
x = 0
y = 0
dust = 0
code = computer.run()
while code != None:
    if counter == len(input_list):
        if code > 128:
            print('dust', code)
            break
    else:
        print chr(code),
        if code == 10:
            y += 1
            max_x = x
            x = 0
        else:
            space_map[(x, y)] = code
            x += 1
    code = computer.run()
Esempio n. 11
0
    ('down', 0): lambda xy, current_dir, new_dir:
    ((xy[0] + 1, xy[1]), 'right'),
    ('left', 0): lambda xy, current_dir, new_dir: ((xy[0], xy[1] + 1), 'down'),
    ('right', 1): lambda xy, current_dir, new_dir: (
        (xy[0], xy[1] + 1), 'down'),
    ('left', 1): lambda xy, current_dir, new_dir: ((xy[0], xy[1] - 1), 'up'),
    ('right', 0): lambda xy, current_dir, new_dir: ((xy[0], xy[1] - 1), 'up'),
}

xy = (0, 0)
current_dir = 'up'
color = 0

min_x = 0
min_y = 0
max_x = 0
max_y = 0

while True:
    computer.set_signal(color)
    new_color = computer.run()
    if new_color == None:
        break
    new_dir = computer.run()
    panels[xy] = new_color
    xy, current_dir = new_xy_dir[(current_dir, new_dir)](xy, current_dir,
                                                         new_dir)
    color = panels[xy] if xy in panels else 0

print(len(panels))
Esempio n. 12
0
            sequence.append(i)
            generate_sequences(iter, have, sequence)
            sequence.pop()
            have.remove(i)
    return allSeqs


max_output = 0
for seq in generate_sequences(range(5)):
    p1, p2, p3, p4, p5 =  seq
    A = Amplifier(mem, p1)
    B = Amplifier(mem, p2)
    C = Amplifier(mem, p3)
    D = Amplifier(mem, p4)
    E = Amplifier(mem, p5)
    a = A.run([0])
    b = B.run([a])
    c = C.run([b])
    d = D.run([c])
    e = E.run([d])
    max_output = max(max_output, E.output[-1])
print("Part1", max_output)


print("--------------------------------------------------------------------------")


max_output = 0
for seq in generate_sequences(range(5,10)):
    p1, p2, p3, p4, p5 =  seq
    A = Amplifier(mem, p1)
Esempio n. 13
0
base = None
pointer = None
processedInstr = None
countBlocks = 0
i = 0
nOut=1
score=0
joystick=None
ballX=0
paddleX=0
while opCode != 99:
    
    computer = IntcodeComputer(processedInstr if processedInstr else
                               programInstructions, [joystick if joystick else 0], pointer if pointer else 0, base if base else 0)
    if nOut==(1+i*3):
        pointer, outputX, processedInstr, opCode, base = computer.run('loop')    
        
    if nOut==(2+i*3):    
        pointer, outputY, processedInstr, opCode, base = computer.run('loop')
        
        
    if nOut==(3+i*3):
        pointer, outputID, processedInstr, opCode, base = computer.run('loop')
        
        
    
    
    if not nOut%3:

        i+=1
        if outputID==2: