Example #1
0
def find_max_thruster_signal_with_feedback() -> int:
	possibilities = itertools.permutations(settings_2)
	max = 0
	for p in possibilities:
		next_input = 0
		amplifiers = []
		
		# queues[0] for comm between A and B
		# queues[1] for comm between B and C
		# queues[2] for comm between C and D
		# queues[3] for comm between D and E
		# queues[4] for comm between E and A
		queues = [Queue() for _ in settings_2]
		
		# Start machines
		for i in range(len(p)):
			user_input = [p[i]] # p[i] is the used setting
			if i == 0:
				user_input.append(0)
			machine = intcode.Machine(program.copy(), user_input, queues[i-1], queues[i])
			t = Thread(target = machine.run)
			t.start()
			amplifiers.append(t)
		
		for a in amplifiers:
			a.join()	
		res = queues[-1].get()
		if res > max:
			max = res
	return max
def amploop(phases):
    amps = [intcode.Machine(mem, [p]) for p in phases]

    a = 0
    signal = 0
    while True:
        amps[a].input.append(signal)
        try:
            amps[a].run()
        except intcode.EOutput:
            signal = amps[a].output.pop()
        except intcode.EHalt:
            return signal
        a = (a + 1) % len(amps)
Example #3
0
def find_max_thruster_signal() -> int:
	global program
	possibilities = itertools.permutations(settings)
	max = 0
	for p in possibilities:
		next_input = 0
		for setting in p:
			#program_copy = program.copy()
			#res = Intcode.run(program, [setting, next_input])
			machine = intcode.Machine(program.copy(), [setting, next_input])
			res = machine.run()
			next_input = res[0]
		if next_input > max:
			max = next_input
	return max
def test(x,y):
    drone = intcode.Machine(args.file)
    drone.input.append(x)
    drone.input.append(y)
    drone.runq()
    return drone.output.pop()
Example #5
0
#
# Advent of Code 2019
# Bryan Clair
#
# Day 5
#
import sys

sys.path.append("..")
import aocutils
import intcode

args = aocutils.parse_args()

# part 1
print "Part 1"
machine = intcode.Machine(args.file, input=[1])
machine.runq()
print machine.output

print "Part 2"
machine = intcode.Machine(args.file, input=[5])
machine.runq()
print machine.output
Example #6
0
#
import sys
sys.path.append("..")
import aocutils
import intcode

args = aocutils.parse_args()

inputlines = [x.strip() for x in open(args.file).readlines()]

mem = []
for line in inputlines:
    mem.extend([int(x) for x in line.split(',')])

# part 1
machine = intcode.Machine(mem)
machine[1] = 12
machine[2] = 2
machine.runq()
print 'part 1:'
print machine[0]

# part 2
print 'part 2:'
for noun in range(100):
    for verb in range(100):
        machine = intcode.Machine(mem)
        machine[1] = noun
        machine[2] = verb
        machine.runq()
        if machine[0] == 19690720:
#
# Advent of Code 2019
# Bryan Clair
#
# Day --
#
import sys
sys.path.append("..")
import aocutils
import intcode
from itertools import izip

args = aocutils.parse_args()

inputlines = [x.strip() for x in open(args.file).readlines()]

arcade = intcode.Machine(args.file)

arcade.runq()

out = arcade.output

screen = {}
for x,y,tile in izip(*[iter(out)]*3):
    screen[(x,y)] = tile

print 'Part 1'
print screen.values().count(2)

Example #8
0
def main_2() -> int:
    machine = intcode.Machine(program, [2])
    return machine.run()[0]
Example #9
0
#
# Advent of Code 2019
# Bryan Clair
#
# Day 9
#
import sys
sys.path.append("..")
import aocutils
import intcode
from itertools import permutations

args = aocutils.parse_args()

with open(args.file, 'r') as memfile:
    content = memfile.read()
    mem = [int(x) for x in content.split(',')]

BOOST = intcode.Machine(mem, input=1)
BOOST.runq()
print BOOST.output

BOOST = intcode.Machine(mem, input=2)
BOOST.runq()
print BOOST.output
	def test_example_1(self):
		program = [109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99]
		machine = intcode.Machine(program.copy())
		res = machine.run()
		self.assertEqual(res, program, "It should output a copy of the program")
	def test_example_3(self):
		program = [104,1125899906842624,99]
		machine = intcode.Machine(program.copy())
		res = machine.run()
		self.assertEqual(res[0], program[1], "It should output the middle number")
	def test_example_2(self):
		program = [1102,34915192,34915192,7,4,7,99,0]
		machine = intcode.Machine(program.copy())
		res = machine.run()
		self.assertEqual(len(str(res[0])), 16, "It should output a 16-digit number")