Beispiel #1
0
def collect_dust(memory, main, functions, continuous_video_feed=False):
    memory[0] = 2

    inputs = main + functions[0] + functions[1] + functions[2] + [121 if continuous_video_feed else 110, 10]
    intcode_computer = IntcodeComputer(memory, initial_inputs=inputs)
    intcode_computer.run()
    return intcode_computer.get_all_outputs()
Beispiel #2
0
def get_linear_system_output(phase_settings, memory):
    last_output = 0
    for phase_setting in phase_settings:
        inputs = [phase_setting, last_output]
        intcode_computer = IntcodeComputer(memory, inputs)
        intcode_computer.run()
        last_output = intcode_computer.pop_last_output()

    return last_output
Beispiel #3
0
def survey(commands):
    inputs = functools.reduce(lambda a, b: a + string_to_ascii_array(b),
                              commands, [])

    intcode_computer = IntcodeComputer(origin_memory, inputs)
    intcode_computer.run()
    output = intcode_computer.get_all_outputs()
    try:
        print(ascii_array_to_string(output))
    except ValueError:
        print(output[-1])
Beispiel #4
0
    def test_day7_1(self):

        for name, memory in [
            ('Day 7 test 1', self.DAY_7_MEMORY_1),
            ('Day 7 test 2', self.DAY_7_MEMORY_2),
            ('Day 7 test 3', self.DAY_7_MEMORY_3),
        ]:

            computer = IntcodeComputer(memory)
            best_phase_settings = None
            best_phase_output = -99999999
            initial_input = 0

            for phase_settings in list(permutations([0, 1, 2, 3, 4])):

                next_input = initial_input
                for phase in phase_settings:
                    computer.reset()
                    next_input = computer.run(phase, next_input)
                if next_input > best_phase_output:
                    best_phase_settings = phase_settings
                    best_phase_output = next_input

            with self.subTest(f'{name}'):
                expected_phase_setting, expected_outcome = self.DAY_7_EXPECTED_OUTCOMES[
                    name]
                self.assertEqual(best_phase_settings, expected_phase_setting)
                self.assertEqual(best_phase_output, expected_outcome)
Beispiel #5
0
 def test_day9_1(self):
     data = {
         "day 9 part 1 - 1": {
             'memory': [
                 109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101,
                 1006, 101, 0, 99
             ],
             'expected_output': [
                 109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101,
                 1006, 101, 0, 99
             ]
         },
         "day 9 part 1 - 2": {
             'memory': [1102, 34915192, 34915192, 7, 4, 7, 99, 0],
             'expected_output': 1219070632396864
         },
         "day 9 part 1 - 3": {
             'memory': [104, 1125899906842624, 99],
             'expected_output': 1125899906842624
         }
     }
     for name in data.keys():
         computer = IntcodeComputer(data[name]['memory'])
         test_output = computer.run()
         expected_output = data[name]['expected_output']
         with self.subTest(name):
             self.assertEqual(expected_output, test_output)
Beispiel #6
0
def paint_hull(program, start_color):
    computer = IntcodeComputer(program)
    # NESW
    direction = 0
    position = (0, 0)
    panel_colors = defaultdict(int)
    panel_colors[position] = start_color
    panels_painted = 1

    def new_direction(turn_right: int):
        if turn_right:
            return (direction + 1) % 4
        else:
            return (direction - 1) % 4

    while not computer.run(panel_colors[position]):
        paint_color = computer.outputs[-2]
        turn_right = computer.outputs[-1]
        panel_colors[position] = paint_color
        direction = new_direction(turn_right)
        position = update_position(position, direction)
        if position not in panel_colors:
            panels_painted += 1

    return panels_painted, panel_colors
def _run_program(computer: IntcodeComputer, output_cb: Callable[[int], None]):
    it = computer.run()

    try:
        while True:
            output = next(it)
            output_cb(output)
    except StopIteration:
        pass
Beispiel #8
0
 def test_day5(self):
     for name, memory in [
         ('day5 1', self.DAY_5_MEMORY_1),
         ('day5 2', self.DAY_5_MEMORY_2),
     ]:
         for initial_input in [0, 1, 2]:
             with self.subTest(f'{name} input: {initial_input}'):
                 computer = IntcodeComputer(memory)
                 output_value = computer.run(initial_input)
                 if initial_input < 1:
                     self.assertEqual(initial_input, output_value)
                 else:
                     self.assertEqual(1, output_value)
def part1(puzzle_data):
    all_possible_phase_settings = list(permutations([0, 1, 2, 3, 4]))

    test_1 = [3, 15, 3, 16, 1002, 16, 10, 16, 1, 16, 15, 15, 4, 15, 99, 0, 0]
    test_2 = [
        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
    ]
    test_3 = [
        3, 31, 3, 32, 1002, 32, 10, 32, 1001, 31, -2, 31, 1007, 31, 0, 33,
        1002, 33, 7, 33, 1, 33, 31, 31, 1, 32, 31, 31, 4, 31, 99, 0, 0, 0
    ]

    initial_input = 0

    for name, memory in [('test1', test_1), ('test2', test_2),
                         ('test3', test_3), ('puzzle', puzzle_data)]:

        computer = IntcodeComputer(memory)
        best_phase_settings = None
        best_phase_output = -99999999

        for phase_settings in all_possible_phase_settings:

            next_input = initial_input
            for phase in phase_settings:
                computer.reset()
                computer.run(phase, next_input)
                next_input = computer.output_values.pop()
            if next_input > best_phase_output:
                best_phase_settings = phase_settings
                best_phase_output = next_input

        print(name)
        print(best_phase_settings)
        print(best_phase_output)
        print('----')
Beispiel #10
0
from intcode_computer import IntcodeComputer

with open("input.txt") as f:
    program = [int(num) for num in f.read().strip().split(",")]
    computer1 = IntcodeComputer(program)
    computer1.run(1)
    print("Part 1:", computer1.outputs[-1])
    computer2 = IntcodeComputer(program)
    computer2.run(2)
    print("Part 2:", computer2.outputs[-1])
Beispiel #11
0
from intcode_computer import IntcodeComputer
import copy

with open("Day 9/input.txt", "r") as f:
    codes = [int(i) for i in f.readline().split(",")]

comp = IntcodeComputer(optcodes=copy.deepcopy(codes), print_output=True)
comp.set_inputs(2)
comp.run()
              63, 1005, 63, 545, 4, 529, 1001, 64, 1, 64, 1106, 0, 545, 1002, 64, 2, 64, 109, 12, 21102, 43, 1, -8,
              1008, 1010, 43, 63, 1005, 63, 571, 4, 551, 1001, 64, 1, 64, 1106, 0, 571, 1002, 64, 2, 64, 109, -1, 1207,
              -8, 27, 63, 1005, 63, 593, 4, 577, 1001, 64, 1, 64, 1106, 0, 593, 1002, 64, 2, 64, 109, -7, 21101, 44, 0,
              8, 1008, 1018, 42, 63, 1005, 63, 617, 1001, 64, 1, 64, 1105, 1, 619, 4, 599, 1002, 64, 2, 64, 109, -4,
              1208, -1, 39, 63, 1005, 63, 639, 1001, 64, 1, 64, 1105, 1, 641, 4, 625, 1002, 64, 2, 64, 109, 13, 2105, 1,
              5, 4, 647, 1106, 0, 659, 1001, 64, 1, 64, 1002, 64, 2, 64, 109, 4, 1206, -3, 673, 4, 665, 1106, 0, 677,
              1001, 64, 1, 64, 1002, 64, 2, 64, 109, -22, 21108, 45, 45, 10, 1005, 1011, 699, 4, 683, 1001, 64, 1, 64,
              1105, 1, 699, 1002, 64, 2, 64, 109, 29, 2105, 1, -7, 1001, 64, 1, 64, 1105, 1, 717, 4, 705, 1002, 64, 2,
              64, 109, -19, 21107, 46, 47, 5, 1005, 1016, 739, 4, 723, 1001, 64, 1, 64, 1106, 0, 739, 1002, 64, 2, 64,
              109, -8, 2102, 1, 2, 63, 1008, 63, 33, 63, 1005, 63, 763, 1001, 64, 1, 64, 1106, 0, 765, 4, 745, 1002, 64,
              2, 64, 109, 1, 1201, -2, 0, 63, 1008, 63, 25, 63, 1005, 63, 791, 4, 771, 1001, 64, 1, 64, 1105, 1, 791,
              1002, 64, 2, 64, 109, 16, 1205, 0, 803, 1105, 1, 809, 4, 797, 1001, 64, 1, 64, 1002, 64, 2, 64, 109, -8,
              1205, 9, 827, 4, 815, 1001, 64, 1, 64, 1106, 0, 827, 1002, 64, 2, 64, 109, -4, 2102, 1, -3, 63, 1008, 63,
              36, 63, 1005, 63, 853, 4, 833, 1001, 64, 1, 64, 1106, 0, 853, 1002, 64, 2, 64, 109, 17, 21102, 47, 1, -6,
              1008, 1019, 50, 63, 1005, 63, 877, 1001, 64, 1, 64, 1105, 1, 879, 4, 859, 1002, 64, 2, 64, 109, -29, 2107,
              22, 5, 63, 1005, 63, 897, 4, 885, 1106, 0, 901, 1001, 64, 1, 64, 4, 64, 99, 21102, 27, 1, 1, 21101, 0,
              915, 0, 1106, 0, 922, 21201, 1, 25338, 1, 204, 1, 99, 109, 3, 1207, -2, 3, 63, 1005, 63, 964, 21201, -2,
              -1, 1, 21101, 942, 0, 0, 1105, 1, 922, 22102, 1, 1, -1, 21201, -2, -3, 1, 21102, 957, 1, 0, 1106, 0, 922,
              22201, 1, -1, -2, 1105, 1, 968, 21202, -2, 1, -2, 109, -3, 2106, 0, 0]
    test1 = [109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99]
    test2 = [1102, 34915192, 34915192, 7, 4, 7, 99, 0]
    test3 = [104, 1125899906842624, 99]
    test4 = [109, 1, 203, 2, 204, 2, 99]  # outputs the input
    logging.basicConfig(level=logging.INFO)

    # computer = IntcodeComputer([109, 1, 203, 2, 204, 2, 99])
    computer = IntcodeComputer(puzzle)
    logging.info(computer.memory)
    print(computer.run(1))

Beispiel #13
0
from intcode_computer import IntcodeComputer

with open("input.txt") as file:
    program = [int(number) for number in file.read().split(",")]

computer = IntcodeComputer()

program[1] = 12
program[2] = 2
computer.run(program)
print(f"Part 1: {computer.get_output()}")


def solve_noun_and_verb():
    for noun in range(0, 99):
        for verb in range(0, 99):
            program[1] = noun
            program[2] = verb
            computer.run(program)
            if computer.get_output() == 19690720:
                return 100 * noun + verb


print(f"Part 2: {solve_noun_and_verb()}")
Beispiel #14
0
    memory[0] = 2

    inputs = main + functions[0] + functions[1] + functions[2] + [121 if continuous_video_feed else 110, 10]
    intcode_computer = IntcodeComputer(memory, initial_inputs=inputs)
    intcode_computer.run()
    return intcode_computer.get_all_outputs()


with open('input.txt', 'r') as f:
    origin_memory = [
        int(x) for x in f.readline().split(',')
    ]

# Part 1
intcode_computer = IntcodeComputer(origin_memory, [])
intcode_computer.run()
camera_view = parse_camera_view(intcode_computer.get_all_outputs())

print_view(camera_view)
intersections = get_intersections(camera_view)
alignment_parameters = [
    x * y for x, y in intersections
]
print(sum(alignment_parameters))

# Part 2

# Uncompressed sequence
# L,10,R,8,L,6,R,6,L,8,L,8,R,8,L,10,R,8,L,6,R,6,R,8,L,6,L,10,L,10,L,10,R,8,L,6,R,6,L,8,L,8,R,8,R,8,L6,L,10,L,10,L8,L,8,R,8,R,8,L,6,L,10,L,10,L,8,L,8,R,8

# Main sequence
Beispiel #15
0
class RescueBot:
	def __init__(self, file_str):
		self.computer = IntcodeComputer(file_str, echo=False)
		self.running = False

		self.rooms = {}		# name(str): room(Room)
		self.items = {}		# name(str): room(Room)
		self.directions = []		# e.g. ['north', 'west', 'west', 'south']

		self.cur_room = None
		self.last_room = None
		self.last_dir = None

	def run(self):
		self.running = True
		self.computer.run()
		while self.running:
			output = self.computer.get_output()
			output = ''.join([chr(char) for char in output])
			# print(output)

			# Find/Create Room
			self.last_room = self.cur_room
			room_name = self.get_name_from_output(output)
			if re.search('You can\'t go that way.', output):
				print(output)
			elif room_name in self.rooms:
				self.cur_room = self.rooms[room_name]
			else:
				self.cur_room = self.get_room_from_output(output)
				self.rooms[room_name] = self.cur_room

				print('New Room:')
				self.cur_room.print()
				# print(self.cur_room.text)

			# Link the rooms if they are not already
			if self.last_room and self.last_room is not self.cur_room:
				print('Linking: %s and %s' % (self.cur_room.name, self.last_room.name))
				if self.last_room.edges[self.last_dir] is None:
					self.last_room.edges[self.last_dir] = self.cur_room.name

				if self.last_dir == 'north':
					inv_last_dir = 'south'
				elif self.last_dir == 'south':
					inv_last_dir = 'north'
				elif self.last_dir == 'east':
					inv_last_dir = 'west'
				elif self.last_dir == 'west':
					inv_last_dir = 'east'
				if inv_last_dir in self.cur_room.edges and self.cur_room.edges[inv_last_dir] is None:
					self.cur_room.edges[inv_last_dir] = self.last_room.name

				# self.cur_room.print()
				# self.last_room.print()

			print(output)
			command = self.get_command()
			print('####################')
			print('Current Room: %s' % self.cur_room.name)
			print('Command: %s' % command)
			print('####################')

			if command in ('north', 'south', 'east', 'west'):
				self.last_dir = command

			if command is None:
				self.running = False
				break

			ascii_command = [ord(char) for char in command]
			ascii_command.append(ord('\n'))
			self.computer.read_input(ascii_command)

	def get_command(self):
		if self.directions:
			return self.directions.pop(0)
		elif self.cur_room.has_unknown_edge():
			for dir, name in self.cur_room.edges.items():
				if name is None:
					return dir
			print('Unknown Edge not found')
		else:
			dest = self.find_nearest_unexplored()
			self.directions = self.get_directions_to_room(self.cur_room.name, dest)

			if self.directions is not None:
				return self.directions.pop(0)
			else:
				print('Directions not found')
				self.running = False
				return None

	def find_nearest_unexplored(self):
		for name, room in self.rooms.items():
			print(name)
			for dir, edge in room.edges.items():
				if edge is None:
					return name
		return None

	def get_directions_to_room(self, start_room, stop_room):
		if start_room not in self.rooms:
			print('Error: "%s" not in rooms to path.' % start_room)
			return None
		if stop_room not in self.rooms:
			print('Error: "%s" not in rooms to path.' % stop_room)
			return None

		print('Finding directions from %s to %s' % (start_room, stop_room))

		parents = {start_room: None}
		distances = {start_room: 0}
		node_list = [start_room]

		while len(node_list) > 0:
			node_name = node_list.pop(0)
			if node_name == stop_room:
				break

			node_room = self.rooms[node_name]
			node_edges = node_room.edges
			node_dist = distances[node_name]

			for dir, edge in node_edges.items():
				if edge is not None and edge not in parents:
					parents[edge] = (node_name, dir)
					distances[edge] = node_dist + 1
					node_list.append(edge)

		directions = []
		path_par = parents[stop_room]
		while path_par is not None:
			directions.insert(0, path_par[1])
			path_par = parents[path_par[0]]

		return directions

	def get_name_from_output(self, output):
		# Room Name
		name_match = re.search('== ([a-zA-Z ]+) ==', output)
		if name_match:
			name = name_match.group(1)
			# print('Room Name: %s' % name)
			return name
		else:
			# print('Error: Name not found')
			# print(output)
			return None

	def get_room_from_output(self, output):
		new_room = Room()
		new_room.text = output

		# Room Name
		name_match = re.search('== ([a-zA-Z ]+) ==', output)
		if name_match:
			name = name_match.group(1)
			new_room.name = name
			# print('Room Name: %s' % name)

		# Adding edge rooms
		dir_match = re.search('Doors here lead:\n(- north)?\n?(- east)?\n?(- south)?\n?(- west)?', output)
		if dir_match:
			if dir_match.group(1):
				# print('north found')
				new_room.edges['north'] = None

			if dir_match.group(3):
				# print('south found')
				new_room.edges['south'] = None

			if dir_match.group(2):
				# print('east found')
				new_room.edges['east'] = None

			if dir_match.group(4):
				# print('west found')
				new_room.edges['west'] = None

		return new_room

		# Handling Items
		# item_str_match = re.search('Items here:\n', output)
		# if item_str_match:
		# 	before, keyword, items = output.partition('Items here:\n')
		# 	items_match = re.findall('- [a-z ]+', items)
		# 	# print(items_match)
		# 	new_room.items = items_match

		# 	for item in items_match:
		# 		self.items[item[2:]] = pos

	def find_room_by_name(self, name):
		for room in self.rooms:
			if room.name == name:
				return room

		return None

	def find_closest_unexplored_room(self):
		min_dist = -1
		closest = None

		for pos in self.unexplored:
			pos_x, pos_y = pos
			dist = abs(self.x - pos_x) + abs(self.y - pos_y)

			if not closest or dist < min_dist:
				closest = pos
				min_dist = dist

		return closest
Beispiel #16
0
def is_being_pulled(x, y):
    intcode_computer = IntcodeComputer(origin_memory, [x, y])
    intcode_computer.run()
    return intcode_computer.pop_last_output() == 1
Beispiel #17
0
def run_test(memory, input):
    computer = IntcodeComputer(memory, input)
    computer.run()
    print('Input: {}, Output: {}'.format(input, computer.pop_last_output()))
Beispiel #18
0
from intcode_computer import IntcodeComputer

with open('input.txt', 'r') as f:
    origin_memory = [int(x) for x in f.readline().split(',')]

# Part 1
computer = IntcodeComputer(origin_memory, [1])
computer.run()
print(computer.pop_last_output())

# Part 2
computer = IntcodeComputer(origin_memory, [2])
computer.run()
print(computer.pop_last_output())
Beispiel #19
0
class ScaffoldInterface:
	def __init__(self, file_str=None):
		self.computer = IntcodeComputer(file_str, echo=False)

	def run(self):
		self.computer.run()
		output = self.computer.get_output_values()

		self.scaffolding = [[]]
		for tile in output:
			if tile == 10:
				self.scaffolding.append([])
				# print('\n', end='')
			else:
				self.scaffolding[-1].append(tile)
				# print(chr(tile), end='')
		self.scaffolding = self.scaffolding[0:-2]

		self.print_scaffolding()
		self.count_intersections()

	def print_scaffolding(self):
		height = len(self.scaffolding)
		width = len(self.scaffolding[0])
		# print('%d, %d' % (width, height))

		for y in range(height):
			for x in range(width):
				tile = self.scaffolding[y][x]
				print(chr(tile), end='')
			print('\n', end='')

	def count_intersections(self):
		height = len(self.scaffolding)
		width = len(self.scaffolding[0])

		intersections = []
		for y in range(height):
			for x in range(width):
				tile = self.scaffolding[y][x]

				if chr(tile) is not '#':
					print('%s is not #' % tile)
					continue

				if y > 0 and chr(self.scaffolding[y-1][x]) is not '#':
					continue
				if y < height-1 and chr(self.scaffolding[y+1][x]) is not '#':
					continue
				if x > 0 and chr(self.scaffolding[y][x-1]) is not '#':
					continue
				if x < width-1 and chr(self.scaffolding[y][x+1]) is not '#':
					continue

				intersections.append((x, y))

		intersection_alignment_sum = 0
		for intersection in intersections:
			intersection_alignment_sum += intersection[0] * intersection[1]

		print(intersection_alignment_sum)
Beispiel #20
0
class ScaffoldInterface:
	def __init__(self, file_str=None):
		self.computer = IntcodeComputer(file_str, echo=False)
		self.scaffolding = None

	def run(self):
		self.computer.program[0] = 2
		self.computer.run()
		output = self.computer.get_output_values()
		self.print_output(output)
		# self.save_output(output, './map.txt')
		self.save_scaffolding(output)
		self.find_path()

		# Main Routine
		routine_main = [ord(char) for char in 'A,A,B,C,B,C,B,C,B,A\n']
		print('Main Len %d' % len(routine_main))
		# Function A
		routine_A = [ord(char) for char in 'L,10,L,8,R,8,L,8,R,6\n']
		print('A Len %d' % len(routine_A))
		# Function B
		routine_B = [ord(char) for char in 'R,6,R,8,R,8\n']
		print('B Len %d' % len(routine_B))
		# Function C
		routine_C = [ord(char) for char in 'R,6,R,6,L,8,L,10\n']
		print('C Len %d' % len(routine_C))

		# Main:
		self.computer.set_input(routine_main)
		print(routine_main)
		self.computer.run()
		output = self.computer.get_output_values()
		self.print_output(output)

		# Function A:
		self.computer.set_input(routine_A)
		print(routine_A)
		self.computer.run()
		output = self.computer.get_output_values()
		self.print_output(output)

		# Function B:
		self.computer.set_input(routine_B)
		print(routine_B)
		self.computer.run()
		output = self.computer.get_output_values()
		self.print_output(output)

		# Function C:
		self.computer.set_input(routine_C)
		print(routine_C)
		self.computer.run()
		output = self.computer.get_output_values()
		self.print_output(output)


		# Continuous Video Feed?
		in_yes = [ord(char) for char in 'Y,E,S\n']
		in_no = [ord(char) for char in 'N,O\n']

		self.computer.set_input(in_no)
		self.computer.run()
		last_value = self.computer.get_last_value()
		output = self.computer.get_output_values()
		self.print_output(output[:-1])

		print(last_value)

	def save_scaffolding(self, output):
		self.scaffolding = [[]]
		for val in output[:-2]:
			if val == ord('\n'):
				self.scaffolding.append([])
			else:
				self.scaffolding[-1].append(chr(val))
		self.scaffolding = self.scaffolding[:-2]

	def find_path(self):
		height = len(self.scaffolding)
		width = len(self.scaffolding[0])

		for y in range(height):
			for x in range(width):
				tile = self.scaffolding[y][x]
				if tile == '^':
					cur_x, cur_y = x, y

		# print('%d, %d' % (cur_x, cur_y))
		# Directions ^ v < >
		cur_dir = '^'
		path = []

		print('Finding path from (%d, %d)' % (cur_x, cur_y))
		while True:
			# Find direction to trun
			if cur_dir == '^' or cur_dir == 'v':
				if cur_x > 0 and self.scaffolding[cur_y][cur_x-1] == '#':
					if cur_dir == '^':
						# print('L')
						path.append('L')
						cur_dir = '<'
					else:
						# print('R')
						path.append('R')
						cur_dir = '<'
				elif cur_x < width-1 and self.scaffolding[cur_y][cur_x+1] == '#':
					if cur_dir == '^':
						# print('R')
						path.append('R')
						cur_dir = '>'
					else:
						# print('L')
						path.append('L')
						cur_dir = '>'
				else:
					print('Reached the end at (%d, %d) facing %s' % (cur_x, cur_y, cur_dir))
					break
			elif cur_dir == '<' or cur_dir == '>':
				if cur_y > 0 and self.scaffolding[cur_y-1][cur_x] == '#':
					if cur_dir == '<':
						# print('R')
						path.append('R')
						cur_dir = '^'
					else:
						# print('L')
						path.append('L')
						cur_dir = '^'
				elif cur_y < height-1 and self.scaffolding[cur_y+1][cur_x] == '#':
					if cur_dir == '<':
						# print('L')
						path.append('L')
						cur_dir = 'v'
					else:
						# print('R')
						path.append('R')
						cur_dir = 'v'
				else:
					print('Reached the end at (%d, %d) facing %s' % (cur_x, cur_y, cur_dir))
					break
			else:
				print('Error: Invalid direction %s' % cur_dir)

			# Count steps until off scaffolding
			step_x, step_y = 0, 0
			if cur_dir == '^':
				step_y = -1
			elif cur_dir == 'v':
				step_y = 1
			elif cur_dir == '<':
				step_x = -1
			elif cur_dir == '>':
				step_x = 1
			else:
				print('Error: Invalid direction %s' % cur_dir)

			distance = 0
			next_tile = self.scaffolding[cur_y+step_y][cur_x+step_x]
			while next_tile != '.':
				distance += 1
				cur_x += step_x
				cur_y += step_y
				if cur_x + step_x < 0 or cur_x + step_x >= width or cur_y + step_y < 0 or cur_y + step_y >= height:
					next_tile = None
					break

				next_tile = self.scaffolding[cur_y+step_y][cur_x+step_x]
			# print(distance)
			path.append(distance)

		print(path)
		with open('./path.txt', 'w') as file:
			for step in path:
				file.write(str(step))
				file.write(',')

	def print_output(self, output):
		for val in output:
			print(chr(val), end='')

	def save_output(self, output, file_str):
		with open(file_str, 'w') as file:
			for val in output:
				file.write(chr(val))
Beispiel #21
0
from intcode_computer import IntcodeComputer

with open('input.txt', 'r') as f:
    origin_memory = [
        int(x) for x in f.readline().split(',')
    ]

# Part 1
inputs = [1]
part1_intcode_computer = IntcodeComputer(origin_memory, inputs)
part1_intcode_computer.run()
print(part1_intcode_computer.pop_last_output())

# Part 2
inputs = [5]
part2_intcode_computer = IntcodeComputer(origin_memory, inputs)
part2_intcode_computer.run()
print(part2_intcode_computer.pop_last_output())
Beispiel #22
0
from itertools import permutations
from intcode_computer import IntcodeComputer

with open("Day 7/input.txt", "r") as f:
    codes = [int(i) for i in f.readline().split(",")]

sequences = list(permutations(range(5)))

max_val = 0
max_sequence = None

for sequence in sequences:
    amp_a = IntcodeComputer(codes)
    amp_a.set_inputs(sequence[0], 0)
    amp_a.run()

    amp_b = IntcodeComputer(codes)
    amp_b.set_inputs(sequence[1], amp_a.outputs[0])
    amp_b.run()

    amp_c = IntcodeComputer(codes)
    amp_c.set_inputs(sequence[2], amp_b.outputs[0])
    amp_c.run()

    amp_d = IntcodeComputer(codes)
    amp_d.set_inputs(sequence[3], amp_c.outputs[0])
    amp_d.run()

    amp_e = IntcodeComputer(codes)
    amp_e.set_inputs(sequence[4], amp_d.outputs[0])
    amp_e.run()