def sub1(): foods = get_file_entries('day21/input.txt') allergens_d = {} all_ingredients = [] for food in foods: ingredients, allergens = food.split(' (contains ') allergens = allergens.replace(')', '').split(', ') ingredients = ingredients.split(' ') all_ingredients.extend(ingredients) for allergen in allergens: if allergen in allergens_d.keys(): allergens_d[allergen] &= set(ingredients) else: allergens_d[allergen] = set(ingredients) # reduce while not all([len(x) == 1 for x in allergens_d.values()]): for allergen, ingredients in allergens_d.items(): if len(ingredients) == 1: for other_allergen, other_ingredients in allergens_d.items(): if other_allergen == allergen: continue other_ingredients -= ingredients unclean_ingredients = [list(x)[0] for x in allergens_d.values()] clean_ingredients = list(set(all_ingredients) - set(unclean_ingredients)) counter = 0 for ingredient in all_ingredients: if ingredient in clean_ingredients: counter += 1 print(counter)
def sub2(): cups = [int(x) for x in get_file_entries('day23/input.txt')[0]] lowest = min(cups) cups += [x for x in range(max(cups) + 1, RANGE)] for index, cup in enumerate(cups[:-1]): linked_list[cup] = cups[index + 1] linked_list[cups[-1]] = cups[0] highest = max(linked_list[1:]) index = cups[-1] for move in tqdm(range(MOVES)): current_value = linked_list[index] next_value = linked_list[current_value] temp_value = linked_list[next_value] last_value = linked_list[temp_value] destination = current_value - 1 if destination < lowest: destination = highest while destination in [next_value, temp_value, last_value]: destination -= 1 if destination < lowest: destination = highest linked_list[current_value] = linked_list[last_value] linked_list[last_value] = linked_list[destination] linked_list[destination] = next_value index = current_value oneplus = linked_list[1] oneplusplus = linked_list[oneplus] print(f'{oneplus * oneplusplus}')
def sub2(): foods = get_file_entries('day21/input.txt') allergens_d = {} all_ingredients = [] for food in foods: ingredients, allergens = food.split(' (contains ') allergens = allergens.replace(')', '').split(', ') ingredients = ingredients.split(' ') all_ingredients.extend(ingredients) for allergen in allergens: if allergen in allergens_d.keys(): allergens_d[allergen] &= set(ingredients) else: allergens_d[allergen] = set(ingredients) # reduce while not all([len(x) == 1 for x in allergens_d.values()]): for allergen, ingredients in allergens_d.items(): if len(ingredients) == 1: for other_allergen, other_ingredients in allergens_d.items(): if other_allergen == allergen: continue other_ingredients -= ingredients s = sorted(list(allergens_d.keys())) print(','.join([list(allergens_d[x])[0] for x in s]))
def sub2(): all_answers = get_file_entries('day6/input.txt') total_answers = 0 for group in parse_groups(all_answers): group_set = get_all_yes(group) total_answers += len(group_set) print(total_answers)
def sub2(): tickets = get_file_entries('day5/input.txt') seat_ids = [] for ticket in tickets: seat_ids.append(find_position(ticket)) seat_ids.sort() for i in range(seat_ids[0], seat_ids[-1] + 1): if i not in seat_ids: if i - 1 in seat_ids and i + 1 in seat_ids: print(i) break
def generate_airplane(filename='day11/input.txt'): airplane = [] for line in get_file_entries(filename): airplane.append([Seat.OUTSIDE]) for character in line: airplane[-1].append(Seat(character)) airplane[-1].append(Seat.OUTSIDE) empty_row = [Seat.OUTSIDE for _ in range(len(airplane[0]))] airplane.insert(0, empty_row) airplane.append(empty_row) return airplane
def sub1(): data = get_file_entries('day13/input.txt') starttime = int(data[0]) bustimes = [int(x) for x in data[1].split(',') if x != 'x'] current_time = starttime while True: for x in bustimes: if current_time % x == 0: print(x * (current_time - starttime)) return current_time += 1
def sub2(): lines = get_file_entries('day14/input.txt') current_mask = None memory = defaultdict(int) for command in lines: if (match := mask.match(command)) is not None: current_mask = match.group(1) elif (match := mem.match(command)) is not None: location = int(match.group(1)) value = int(match.group(2)) locations = modify_location(location, current_mask) for location in locations: memory[location] = value
def sub1(): lines = get_file_entries('day16/input.txt') rules, my_ticket, other_tickets = parse_file(lines) invalid = 0 for other_ticket in other_tickets: for number in other_ticket: valid = False for rule in rules.values(): if number in rule: valid = True if not valid: invalid += number print(invalid)
def run(amount): numbers = defaultdict(lambda: deque([], maxlen=2)) starting_numbers = [int(x) for x in get_file_entries('day15/input.txt')[0].split(',')] for index, number in enumerate(starting_numbers): numbers[number].append(index) last_number = starting_numbers[-1] for turn_number in range(len(starting_numbers), amount): if len(numbers[last_number]) < 2: new_last_number = 0 else: new_last_number = numbers[last_number][0] - numbers[last_number][1] numbers[new_last_number].appendleft(turn_number) last_number = new_last_number print(last_number)
def sub2(): lines = get_file_entries('day16/input.txt') rules, my_ticket, other_tickets = parse_file(lines) invalid_tickets = [] for other_ticket in other_tickets: for number in other_ticket: valid = False for rule in rules.values(): if number in rule: valid = True if not valid: invalid_tickets.append(other_ticket) break for invalid_ticket in invalid_tickets: other_tickets.remove(invalid_ticket) rules_locations = {} for rulekey in rules.keys(): rules_locations[rulekey] = -1 matches = [set()] for number in other_tickets[0]: for key, value in rules.items(): if could_match(number, value): matches[-1].add(key) matches.append(set()) del matches[-1] for ticket in other_tickets[1:]: for index, number in enumerate(ticket): not_possible = set() for possible_match in matches[index]: if not could_match(number, rules[possible_match]): not_possible.add(possible_match) matches[index] -= not_possible while not all([len(x) == 1 for x in matches]): for match in matches: if len(match) == 1: for second_match in matches: if second_match != match: second_match -= match matches = [list(x)[0] for x in matches] mul = 1 for index, match in enumerate(matches): if match.startswith('departure'): mul *= my_ticket[index] print(mul)
def sub1(): global already_generated_nodes already_generated_nodes = {} rules = get_file_entries('day7/input.txt') rules_parsed = defaultdict(list) for rule in rules: parent, children = parse_rule(rule) for child in children: rules_parsed[child[1]].append(parent) root = Node('shiny gold') build_tree(root, rules_parsed) dot = Digraph(comment='BAGGAGE 1') root.generate_dot(dot) dot.render('day7/out/1.graph', format='png') print(len(set(root.get_all_subnodes())))
def sub2(): global already_generated_nodes already_generated_nodes = {} rules = get_file_entries('day7/input.txt') rules_parsed = defaultdict(list) for rule in rules: parent, children = parse_rule(rule) for child in children: rules_parsed[parent].append((child[0], child[1])) root = Node('shiny gold') build_tree_with_weights(root, rules_parsed) dot = Digraph(comment='BAGGAGE 2') root.generate_dot(dot) dot.render('day7/out/2.graph', format='png') print(root.amounts() - 1)
def sub1(): lines = get_file_entries('day14/input.txt') replacements = {} memory = defaultdict(int) for command in lines: if (match := mask.match(command)) is not None: replacements = new_mask(match.group(1)) elif (match := mem.match(command)) is not None: location = int(match.group(1)) value = int(match.group(2)) for rkey, rvalue in replacements.items(): if rvalue == 0: value = clear_bit(value, rkey) elif rvalue == 1: value = set_bit(value, rkey) memory[location] = value
def get_initial_tiles(filename='day24/input.txt'): points_visited = set() parsed = '' for line in get_file_entries(filename): current_point = hex_point(0, 0, 0) for character in line: full_direction = parsed + character if full_direction in directions.keys(): parsed = '' vector = directions[full_direction] x = current_point.x + vector.x y = current_point.y + vector.y z = current_point.z + vector.z current_point = hex_point(x, y, z) assert (x + y + z == 0) else: parsed = full_direction if current_point in points_visited: points_visited.remove(current_point) else: points_visited.add(current_point) return points_visited
def sub1(): cups = deque([int(x) for x in get_file_entries('day23/input.txt')[0]]) indices = sorted(list(cups)) lowest, highest = indices[0], indices[-1] moves = 100 for move in range(moves): current_value = cups[0] cups.rotate(-1) afterwards = [cups.popleft(), cups.popleft(), cups.popleft()] destination = current_value - 1 if destination < lowest: destination = highest while destination in afterwards: destination -= 1 if destination < lowest: destination = highest destination = cups.index(destination) for cup in afterwards[::-1]: cups.insert(destination + 1, cup) one = cups.index(1) + 1 cups.rotate(-one) result = ''.join([str(x) for x in cups if x != 1]) print(result)
def sub2(): commands = get_file_entries('day12/input.txt') ship_location = [0, 0] waypoint_offset = [-1, 10] for command in commands: c, a = command[0], int(command[1:]) if c == 'N': waypoint_offset[0] -= a elif c == 'S': waypoint_offset[0] += a elif c == 'W': waypoint_offset[1] -= a elif c == 'E': waypoint_offset[1] += a elif c == 'F': ship_location[0] += waypoint_offset[0] * a ship_location[1] += waypoint_offset[1] * a elif c == 'R': waypoint_offset = rotate_waypoint(waypoint_offset, a) elif c == 'L': waypoint_offset = rotate_waypoint(waypoint_offset, 360 - a) else: print('Boguscommand', c) print(abs(ship_location[0]) + abs(ship_location[1]))
def sub2(): lines = get_file_entries('./day2/input.txt') amount = 0 for line in lines: amount += is_valid_sub2_password(line) print(amount)
def sub1(): print('Attention! The functions of today evaluate any input brought in by input.txt - NEVER run this with arbitrary files! It may cause your device to be compromised!') print(sum([calculate(line.replace('*', '-')) for line in get_file_entries('day18/input.txt')]))
def sub1(): commands = get_file_entries('day12/input.txt') location, facing = walk(commands) print(abs(location[0]) + abs(location[1]))
def sub2(): print(sum([calculate(line.replace('*', '-').replace('+', '/')) for line in get_file_entries('day18/input.txt')]))
def sub1(): tickets = get_file_entries('day5/input.txt') highest_seat_id = -1 for ticket in tickets: highest_seat_id = max(highest_seat_id, find_position(ticket)) print(highest_seat_id)