import sys from typing import Dict, Tuple from shared import get_exact_rows, parse_particle if __name__ == '__main__': lines = get_exact_rows(sys.argv[1]) particles_dict = dict((i, parse_particle(l)) for i, l in enumerate(lines)) for _ in range(10000): position_to_particle_id = {} # type: Dict[Tuple[int, int, int], int] deletion_set = set() # if _ % 1000 == 0: # print(_) for p_id, particle in particles_dict.items(): particle.step() if particle.position in position_to_particle_id: deletion_set.add(p_id) deletion_set.add(position_to_particle_id[particle.position]) else: position_to_particle_id[particle.position] = p_id for deletion_candidate in deletion_set: del particles_dict[deletion_candidate] print(len(particles_dict))
import sys import shared if __name__ == '__main__': x = shared.get_exact_rows(sys.argv[1]) parsed_args = [shared.parse_disctree_row(l) for l in x] bottom = shared.find_bottom(parsed_args) d = shared.build_disctree_arguments_dict(parsed_args) tree = shared.DiscNode(bottom, d) different_child, expected_weight = tree.find_different_child() different_child_candidate = different_child while different_child_candidate: different_child_candidate, different_child_candidate_weight = different_child.find_different_child( ) if different_child_candidate: different_child, expected_weight = different_child_candidate, different_child_candidate_weight print(expected_weight - (different_child.combined_weight() - different_child.weight))
import sys from shared import get_exact_rows, parse_duet_instruction, DuetMachine if __name__ == '__main__': instructions = [ parse_duet_instruction(l) for l in get_exact_rows(sys.argv[1]) ] dm0 = DuetMachine(0, instructions) dm1 = DuetMachine(1, instructions) dm0.recipient = dm1 dm1.recipient = dm0 keep_running = True turn = 0 while keep_running: dm0.run_instructions() dm1.run_instructions() messages_remain = bool(len(dm0.message_queue) + len(dm1.message_queue)) neither_terminated = not (dm0.terminated or dm1.terminated) keep_running = messages_remain and neither_terminated turn += 1 print(dm1.messages_sent)
import sys from typing import List from shared import get_exact_rows if __name__ == '__main__': grid = list(get_exact_rows(sys.argv[1], strip=False)) current_position = [0, grid[0].index('|')] direction = 1, 0 letters_encountered = [] # type: List[str] current_character = None steps = 0 while current_character != ' ': steps += 1 current_position[0] += direction[0] current_position[1] += direction[1] current_character = grid[current_position[0]][current_position[1]] if current_character.isalpha(): letters_encountered += current_character elif current_character == '+': left_turn = direction[1], direction[0] # this is not correct how did this work right_turn = direction[1] * -1, direction[0] * -1 peek_left_character = None peek_right_character = None if 0 <= current_position[0] + left_turn[0] < len(grid) and 0 <= current_position[1] + left_turn[1] < len(grid[1]): peek_left_character = grid[current_position[0] + left_turn[0]][current_position[1] + left_turn[1]] if 0 <= current_position[0] + right_turn[0] < len(grid) and 0 <= current_position[1] + right_turn[1] < len(grid[1]): peek_right_character = grid[current_position[0] + right_turn[0]][current_position[1] + right_turn[1]]
INFECTED = '#' FLAGGED = 'F' from_string = {'.': CLEAN, 'W': WEAKENED, 'F': FLAGGED, '#': INFECTED} next_state = { CLEAN: WEAKENED, WEAKENED: INFECTED, INFECTED: FLAGGED, FLAGGED: CLEAN } if __name__ == '__main__': ITERATIONS = 10000000 rows = list(shared.get_exact_rows(sys.argv[1])) grid_size = len(rows) + 1000 notreallyinfinite_grid = shared.initialize_2d_array( grid_size, grid_size, '.') starting_bound = (grid_size - len(rows)) // 2 for i, r in enumerate(rows): notreallyinfinite_grid[starting_bound + i][starting_bound:starting_bound + len(rows)] = list(r) position = grid_size // 2, grid_size // 2 center = position direction = (-1, 0) infection_events = 0 clean_streak = 0 for _ in range(ITERATIONS):
import sys from shared import get_exact_rows, parse_instruction, RegisterMachine if __name__ == '__main__': instructions = [parse_instruction(i) for i in get_exact_rows(sys.argv[1])] rm = RegisterMachine() for i in instructions: rm.process_instruction(i) print(rm.get_max_register()) print(rm.max_ever)
maneuver0 = Maneuver(write_value, tape_step, next_state) next(lines_iterator) # 1 always comes after 0 write_value = int(next(lines_iterator)[-2]) tape_step = {'right': 1, 'left': -1}[next(lines_iterator).split()[-1][:-1]] next_state = next(lines_iterator)[-2] maneuver1 = Maneuver(write_value, tape_step, next_state) return current_state, (maneuver0, maneuver1) def parse_instructions(lines_iterator: Iterator[str]) -> Instructions: instructions_out = {} try: while True: state_letter, new_instruction = parse_single_instruction( lines_iterator) instructions_out[state_letter] = new_instruction except StopIteration: pass return instructions_out if __name__ == '__main__': raw_lines_iterator = shared.get_exact_rows(sys.argv[1]) starting_state = next(raw_lines_iterator)[-2] checksum_amount = int(next(raw_lines_iterator).split()[-2]) parsed_instructions = parse_instructions(raw_lines_iterator) computer = Computer(parsed_instructions, starting_state) for _ in range(checksum_amount): computer.run_step() print(computer.running_sum)