import aocread targets_str = aocread.read_file('input06') targets = [] threshold = 10000 max_x = 0 max_y = 0 for target_str in targets_str: x = int(target_str.split(',')[0].lstrip().rstrip()) y = int(target_str.split(',')[1].lstrip().rstrip()) targets.append((x, y)) max_x = max(max_x, x) max_y = max(max_y, y) max_x += 1 max_y += 1 board = [[0 for _ in xrange(max_x)] for _ in xrange(max_y)] border_targets = set() for y in xrange(max_y): for x in xrange(max_x): net_distance = 0 for target_id in xrange(len(targets)): # Manhattan Distance target_x = targets[target_id][0] target_y = targets[target_id][1] dist = abs(target_y - y) + abs(target_x - x)
import aocread class Node: def __init__(self): self.meta = [] self.children = [] self.val = 0 tree_string = aocread.read_file('input08')[0] tree_string = tree_string.rstrip('\n') tree_tokens = tree_string.split(' ') def build_tree(): global tree_tokens node = Node() num_children = int(tree_tokens[0]) num_meta_data = int(tree_tokens[1]) tree_tokens = tree_tokens[2:] for child in xrange(num_children): child_node = build_tree() node.children.append(child_node) for meta_data_id in xrange(num_meta_data): meta_data = int(tree_tokens[meta_data_id]) node.meta.append(meta_data)
import aocread game_config = aocread.read_file('input09')[0] num_players = int(game_config.split(' ')[0]) last_marble = int(game_config.split(' ')[-2]) player_scores = [0] * num_players class Marble: def __init__(self, points): self.points = points self.right = None self.left = None marble_circle = Marble(0) marble_circle.left = marble_circle marble_circle.right = marble_circle current_marble = marble_circle player = 0 def get_marble(starting_pos, num_pos, clockwise=True): if num_pos == 0: return starting_pos else: return get_marble(starting_pos=starting_pos.right if clockwise else starting_pos.left, num_pos=num_pos - 1,
import aocread ids = aocread.read_file('input02') num_ids = len(ids) common = '' found_correct_box = False def get_diff(ida, idb): common = '' strlen = len(ida) for i in xrange(strlen): if ida[i] == idb[i]: common += ida[i] return common box_found = False for boxa in xrange(num_ids): for boxb in xrange(boxa+1, num_ids): common = get_diff(ids[boxa], ids[boxb]) if len(common) == len(ids[boxa]) - 1: print(common) box_found = True break if box_found:
import aocread frequency_input = aocread.read_file('input01') num_changes = len(frequency_input) current_change = 0 change_set = set() net = 0 while net not in change_set: change_set.add(net) change = int(frequency_input[current_change % num_changes]) net += change current_change += 1 print(net)
class Guard: def __init__(self): self.schedule = [0] * 60 self.time_slept = 0 def stamp_sleep_time(self, start, end): for minute in xrange(start, end): self.schedule[minute] += 1 self.time_slept += (end - start) def __gt__(self, other): return self.time_slept > other.time_slept time_logs = aocread.read_file('input04') parsed_logs = [] guards = dict() for time_log in time_logs: parsed_log = Shift() parsed_log.stamp(time_log) if parsed_log.guardid != 0 and parsed_log.guardid not in guards: guards[parsed_log.guardid] = Guard() parsed_logs.append(parsed_log) parsed_logs.sort() num_logs = len(parsed_logs)
import aocread import numpy as np box_strings = aocread.read_file('input03') class Box: def __init__(self, id, left, top, width, height): self.id = int(id) - 1 self.left = int(left) self.top = int(top) self.width = int(width) self.height = int(height) self.right = self.left + self.width - 1 self.bottom = self.top + self.height - 1 def parse(box_string): id = box_string.split('@')[0].strip().lstrip('#') origin = box_string.split('@')[1].split(':')[0].strip() dimension = box_string.split('@')[1].split(':')[1].strip() left = origin.split(',')[0] top = origin.split(',')[1] width = dimension.split('x')[0] height = dimension.split('x')[1] return Box(id, left, top, width, height) boxes = []
import aocread input_string = aocread.read_file('input05')[0] def process_input(input_string): idx = 0 while idx < len(input_string) - 1: cur_char = input_string[idx] next_char = input_string[idx + 1] # If the units are different if cur_char.lower() == next_char.lower() and cur_char != next_char: input_string = input_string[:idx] + input_string[idx + 2:] idx = max(idx - 1, 0) else: idx += 1 return len(input_string) input_string = input_string.replace('\n', '') final_len = process_input(input_string) print(final_len) min = len(input_string) for unit in xrange(97, 97 + 26): lowerchar = str(chr(unit)) upperchar = lowerchar.upper()
import aocread layout_string = aocread.read_file('input13') TRACK_TYPE_VOID = -1 TRACK_TYPE_INTERSECTION = '+' TRACK_TYPE_HORIZONTAL = '-' TRACK_TYPE_VERTICAL = '|' TRACK_TYPE_CURVE_A = '/' TRACK_TYPE_CURVE_B = '\\' class Cart: def __init__(self, x, y, facing_x, facing_y): self.x = x self.y = y self.facing_x = facing_x self.facing_y = facing_y self.intersection_choice = 0 self.crashed = False self.crash_location = None def turn_right(self): self.facing_x, self.facing_y = -self.facing_y, self.facing_x def turn_left(self): self.facing_x, self.facing_y = self.facing_y, -self.facing_x def update(self, track_type): if self.crashed: return
def get_point_from_string(point_string): global pattern metrics = pattern.findall(point_string) position_string = metrics[0].lstrip('<').rstrip('>') velocity_string = metrics[1].lstrip('<').rstrip('>') x = int(position_string.split(',')[0]) y = int(position_string.split(',')[1]) velx = int(velocity_string.split(',')[0]) vely = int(velocity_string.split(',')[1]) return x, y, velx, vely points_string = aocread.read_file('input10') for point_string in points_string: x, y, velx, vely = get_point_from_string(point_string) light_points.append((x, y)) light_vels.append((velx, vely)) def get_sorted_points_and_bounds(): global light_points sorted_points = sorted(light_points, key=lambda x: x[0]) pos_xs, pos_ys = zip(*sorted_points) min_x = min(pos_xs) max_x = max(pos_xs) min_y = min(pos_ys)
import aocread data = aocread.read_file('input12') initial_state = data[0].split(':')[1].lstrip().rstrip('\n') initial_state = initial_state.replace('.', '0').replace('#', '1') config = dict() for config_str in data[2:]: config_str = config_str.rstrip('\n').replace('.', '0').replace('#', '1') config_key = int(config_str.split(' ')[0], 2) config_result = int(config_str.split(' ')[2], 2) config[config_key] = config_result num_generations = 0 pattern_length = 5 cur_state = initial_state origin_loc = 0 num_stable_generations = 0 prev_num_plants = 0 threshold = 50000000000 while num_generations < threshold: cur_state = '0' * pattern_length + cur_state + '0' * pattern_length origin_loc += (pattern_length - pattern_length // 2) evolved_state = '' sum_of_plants = 0 num_plants = 0 for start_index in xrange(len(cur_state) - pattern_length + 1): batch_config_str = cur_state[start_index:start_index + pattern_length] batch_config = int(batch_config_str, 2) if batch_config in config:
import aocread instructions = aocread.read_file('input07') class Worker: def __init__(self): self.task = '' self.time = 0 def assign(self, step, time): self.task = step self.time = time def update(self): self.time -= 1 if self.time == 0: return True return False def is_available(self): if self.task == '': return True else: return False NUM_WORKERS = 5 BASE_TIME = 60 dependency_graph_in = dict()