def main(): input_txt = utils.get_input(2) lines = input_txt.split("\n") twos = 0 threes = 0 for line in lines: f1 = False f2 = False occs = get_occurences(line) for occ in occs.values(): if occ == 2 and not f1: twos += 1 f1 = True elif occ == 3 and not f2: threes += 1 f2 = True p1 = threes * twos print(f"Part 1: {p1}") for i, line_a in enumerate(lines): for line_b in lines[i + 1 :]: one_time, idx = compare(line_a, line_b) if one_time: solution = list(line_a) del solution[idx] print(f"Part 2: {''.join(solution)}") return
def main(): input_txt = get_input(17) int_code_list = ints(input_txt) code = defaultdict(int, enumerate(int_code_list)) board1 = run_robot(code) print(f'Part 1: {len(board1)}')
def main(): data = utils.get_input(6).split("\n")[:-1] locations = dict() max_x = 0 max_y = 0 for i, d in enumerate(data): x, y = d.split(", ") x = int(x) if x > max_x: max_x = x y = int(y) if y > max_y: max_y = y locations[(int(x), int(y))] = i print("WuuT: ", max_x, max_y) grid_one = create_grid(-400, 800, -400, 800, locations) grid_two = create_grid(-450, 850, -450, 850, locations) counts_one = [0] * len(locations) counts_two = [0] * len(locations) for k, v in grid_one.items(): if v == -1: continue counts_one[v] += 1 for k, v in grid_two.items(): if v == -1: continue counts_two[v] += 1 didnt_change = set(counts_one).intersection(set(counts_two)) print("#" * 10) print(didnt_change) print(f"Part 1: {max(didnt_change)}") print(f"Part 2: {part_two(max_x, max_y, locations)}")
def exec_moves(day: int, letters: list) -> None: # letters = ['a', 'b', 'c', 'd', 'e'] moves = utils.get_input(day) # moves = 's1,x3/4,pe/b' # Look for cycle and as soon as found: return state of cycle at 10 ** 9th iteration done = [] for i in range(10 ** 9): letters_str = ''.join(letters) if letters_str in done: print('Part 2: {0}'.format(done[10 ** 9 % i])) return done.append(letters_str) for move in moves.strip().split(','): if move and move[0] == 's': letters = deque(letters) letters.rotate(int(move[1:])) letters = list(letters) elif move and move[0] == 'x': idcs = move[1:].split('/') letters = swap(letters, int(idcs[0]), int(idcs[1])) elif move and move[0] == 'p': letters = swap(letters, letters.index(move[1]), letters.index(move[-1])) if i == 0: print('Part 1: {0}'.format(''.join(letters)))
def execute_instructions(day: int) -> None: # cy inc -232 if scy > -8 pattern = r'(\w+) (\w+) (-*\d+) \w+ (\w+) ([>=<!]+) (-*\d+).*' regex = re.compile(pattern) max_value = 0 input_str = utils.get_input(day) for line in input_str.split('\n'): match = regex.match(line) if match: mod_register = match.group(1) mod_op = match.group(2) mod_value = int(match.group(3)) check_register = match.group(4) bool_op = match.group(5) check_value = int(match.group(6)) check_current_val = registers.get(check_register, 0) if ops[bool_op](check_current_val, check_value): mod_current_val = registers.get(mod_register, 0) registers[mod_register] = ops[mod_op](mod_current_val, mod_value) if registers[mod_register] > max_value: max_value = registers[mod_register] print('Part 1: ', max(registers.values())) print('Part 2: ', max_value)
def part_one(day: int) -> None: tree = nx.DiGraph() edges = list() node_pattern = r'(\w+) \((\d+)\).*' std_regex = re.compile(node_pattern) input_str = utils.get_input(day) for line in input_str.split('\n'): splitted_line = line.split('->') match = std_regex.match(splitted_line[0]) if match: node, weight = match.group(1), match.group(2) tree.add_node(node, weight=int(weight)) if len(splitted_line) > 1: children = [s.rstrip(',') for s in splitted_line[1].split()] for child in children: edges.append((node, child)) tree.add_edges_from(edges) root = list(nx.topological_sort(tree))[0] print('Root:', root) my_tree = Tree(root, tree) my_tree.find_unbalanced_node()
def main(): data = utils.get_input(13).split("\n") carts = [] map_size_y = len(data) map_size_x = len(data[0]) trackmap = [["" for y in range(map_size_y)] for x in range(map_size_x)] cart_to_track = {"^": "|", "v": "|", "<": "|", ">": "|"} for y, line in enumerate(data): for x, c in enumerate(line): if c in ["^", "v", ">", "<"]: carts.append(Cart(x, y, c)) trackmap[x][y] = cart_to_track[c] else: trackmap[x][y] = c last_cart = None while True: carts = [c for c in carts if not c.crashed] # print(len(carts)) if len(carts) == 1: last_cart = carts[0] break if not carts: print("ERROR: All carts crashed??") carts = tick(trackmap, carts) print(f"Part 1: {FIRST_CRASH}") print(f"Part 2: {last_cart.x},{last_cart.y}")
def main(): input_txt = utils.get_input(8).strip() p1 = part1(input_txt) print(f"Part 1: {p1}") print("Part 2") part2(input_txt)
def walk(day: int) -> None: x = 0 y = 0 z = 0 input_str = utils.get_input(11) # input_str = 'ne,ne,ne' # input_str = 'ne,ne,sw,sw' # input_str = 'ne,ne,s,s' # input_str = 'se,sw,se,sw,sw' distances = [] for d in input_str.rstrip().split(','): if d == 'n': y += 1 z -= 1 elif d == 'ne': x += 1 z -= 1 elif d == 'se': x += 1 y -= 1 elif d == 's': y -= 1 z += 1 elif d == 'sw': x -= 1 z += 1 elif d == 'nw': x -= 1 y += 1 distances.append((abs(x) + abs(y) + abs(z)) // 2) print((abs(x) + abs(y) + abs(z)) // 2) print(max(distances))
def main(): components = [cp for cp in utils.get_input(24).strip().split('\n')] available = set() global all_possibilities all_possibilities = list() for cp in components: port_a, port_b = [int(x) for x in cp.strip().split('/')] available.add((port_a, port_b)) for bridge in available: if bridge[0] == 0: tmp = copy(available) tmp.remove(bridge) get_max([bridge], bridge[-1], tmp) elif bridge[-1] == 0: tmp = copy(available) tmp.remove(bridge) get_max([bridge], bridge[0], tmp) maximum = max( [sum([t[0] + t[-1] for t in xs]) for xs in all_possibilities]) print('Part 1: ', maximum) longest = max(all_possibilities, key=lambda x: len(x)) print('Part 2: ', sum([x[0] + x[-1] for x in longest]))
def read_firewall(day: int) -> dict: firewall = dict() input_str = utils.get_input(day) for line in input_str.rstrip().split('\n'): depth, scanner_range = line.split(': ') firewall[int(depth)] = int(scanner_range) return firewall
def main(): input_txt = utils.get_input(4) start, end = [int(x) for x in input_txt.split('-')] p1 = part1(start, end) p2 = part2(start, end) print(f"Part 1: {p1}") print(f"Part 2: {p2}")
def main(): input_txt = utils.get_input(98) lines = input_txt.splitlines() # TODO: change to 20 only_fifty = lines[:10] print(f"Part 1: {part1(only_fifty)}") print(f"Part 2: {part2(lines)}")
def main(): input_txt = utils.get_input(16).rstrip() ranges, my_ticket, nearby = input_txt.split("\n\n") valid_ranges = read_ranges(ranges) sum_not_valid, valid_nearby_lines = part1(valid_ranges, nearby) print(sum_not_valid) print(part2(my_ticket, valid_ranges, valid_nearby_lines))
def main(): input_txt = utils.get_input(5) lines = [] for line_def in input_txt.strip().split("\n"): x1, y1, x2, y2 = utils.ints(line_def) lines.append(((x1, y1), (x2, y2))) print(f"Part 1: {part1(lines)}") print(f"Part 2: {part2(lines)}")
def main(): lines = [x for x in utils.get_input(19).split('\n')] start = (0, 0) for x, char in enumerate(lines[0]): if char == '|': start = (x, 0) print(start) run_package(lines, start)
def main(): functions = { 'addr': addr, 'addi': addi, 'mulr': mulr, 'muli': muli, 'banr': banr, 'bani': bani, 'borr': borr, 'bori': bori, 'setr': setr, 'seti': seti, 'gtir': gtir, 'gtri': gtri, 'gtrr': gtrr, 'eqir': eqir, 'eqri': eqri, 'eqrr': eqrr, } lines = utils.get_input(21).split("\n") # Read in instruction pointer binding ip = int(lines[0].split()[-1]) # Read in operations operations = [None] * (len(lines) - 1) for i, line in enumerate(lines[1:]): if line: op, a, b, c = line.split() operations[i] = (op, int(a), int(b), int(c)) print(operations) print(len(operations)) # Set initial register values # Part 1 # registers = [0] * 6 # Part 2 minimal = 0 for x in range(10000): registers = [x, 0, 0, 0, 0, 0] iterations = 0 while iterations < 500: if registers[ip] < 0 or registers[ip] > len(operations): minimal = x print(minimal) return #print('before', registers, end=' ') next_op, a, b, c = operations[registers[ip]] registers = functions[next_op](a, b, c, registers) # Default increment instruction pointer by 1 registers[ip] += 1 #print('after', registers) iterations += 1 print(registers[0])
def part_one(day: int) -> None: valid = 0 input_str = utils.get_input(day) for line in input_str.split('\n'): words = line.split() if words and len(set(words)) == len(words): valid += 1 print(valid)
def main(): input_txt = get_input(19) int_code_list = ints(input_txt) code = defaultdict(int, enumerate(int_code_list)) p1 = part1(code) print(f"Part 1: {p1}") p2 = part2(code) print(f"Part 2: {p2}")
def main(): input_txt = utils.get_input(23).rstrip() numbers = [int(x) for x in list(input_txt)] numbers2 = numbers.copy() print(f"Do sorting by hand - clockwise numbers after 1: {part1(numbers)}") numbers2.extend(list(range(10, 10**6 + 1))) print(part2(numbers2))
def main(): test = '12131415' test_sum = calc_sum(test, find_halfway_matching_pairs) assert test_sum == 4 input_txt = utils.get_input(1) input_sum_one = calc_sum(input_txt, find_direct_matching_pairs) input_sum_two = calc_sum(input_txt, find_halfway_matching_pairs) print('Sum Part 1: {}'.format(input_sum_one)) print('Sum Part 2: {}'.format(input_sum_two))
def main(): operations = [] functions = [ addr, addi, mulr, muli, banr, bani, borr, bori, setr, seti, gtir, gtri, gtrr, eqir, eqri, eqrr, ] op_assignments = dict() data = utils.get_input(16).split("\n\n\n") lines_p1 = data[0].split("\n") for i, line in enumerate(lines_p1): if "Before" in line: op_lines = lines_p1[i:i + 3] reg_before = parse(op_lines[0]) op = parse(op_lines[1]) reg_after = parse(op_lines[2]) this_op = Operation(reg_before, op, reg_after) operations.append(this_op) for op in operations: for f in functions: if f(op.op[1], op.op[2], op.op[3], op.before) == op.after: op.possible_ops.append(f) three_and_above = len( [op for op in operations if len(op.possible_ops) >= 3]) print(f"Part 1 {three_and_above}") op_assignments = find_op_codes(operations, op_assignments) final_registers = [0] * 4 lines_p2 = data[1].split("\n") for line in lines_p2: line = line.strip() if not line: continue op, a, b, c = list(map(int, line.split())) f = op_assignments[op] final_registers = f(a, b, c, final_registers) print(f"Part 2: {final_registers[0]}")
def main(): input_txt = utils.get_input(8).rstrip() operations = {} for idx, line in enumerate(input_txt.split("\n")): operation, number = line.split() operations[idx] = (operation, int(number)) print(part1(operations)) print(part2(operations))
def main(): input_txt = utils.get_input(6) p1 = part1(input_txt) x = 'YOU' y = 'SAN' p2 = part2(input_txt, x, y) print(f"Part 1: {p1}") print(f"Part 2: {p2}")
def part_two(day: int) -> None: valid = 0 input_str = utils.get_input(day) for line in input_str.split('\n'): words = line.split() if words and is_valid(words): valid += 1 print(valid)
def main(): data = utils.get_input(7).split("\n") G, starting_node = create_graph(data) # Very short and via networkX algorithm only: # ''.join(nx.lexicographical_topological_sort(G))) order = find_order(G, starting_node) order_str = "".join(order) print(f"Part 1: {order_str}") print(f"Part 2: {part_two(G)}")
def main(): input_txt = utils.get_input(4) sections = input_txt.split("\n\n") drawn_numbers = utils.ints(sections[0]) boards_input = sections[1:] boards = [Board(board_input) for board_input in boards_input] boards2 = deepcopy(boards) print(f"Part 1: {part1(drawn_numbers, boards)}") print(f"Part 2: {part2(drawn_numbers, boards2)}")
def main(): input_txt = utils.get_input(14) start, rule_lines = input_txt.split("\n\n") rules = defaultdict(str) for rule in rule_lines.splitlines(): pair, result = rule.split(" -> ") rules[pair] = result print(f"Part 1: {part1(list(start), rules)}") print(f"Part 2: {part2(list(start), rules)}")
def main(): grid_lines = utils.get_input(10).strip().split('\n') asteroids = set() for y, s in enumerate(grid_lines): for x, c in enumerate(s): if c == '#': asteroids.add((x, y)) best_asteroid, detected = part1(asteroids) print(f"Part 1: {detected}") print(best_asteroid)
def main(): input_txt = utils.get_input(11) grid = {} for y, line in enumerate(input_txt.strip().split("\n")): for x, value in enumerate(line): grid[x, y] = int(value) grid2 = deepcopy(grid) print(f"Part 1: {part1(grid)}") print(f"Part 2: {part2(grid2)}")