def _read_tiles(file_name): def _get_id_and_tile(block): _id, *tile = block.split("\n") return int(_id[len("Tile "):-1]), np.asarray([[x for x in row] for row in tile]) return list(map(_get_id_and_tile, read(file_name).split("\n\n")))
def shuffle(file_name, iter, total=None): cups = list(map(int, read(file_name))) if total is not None: cups = cups + list(range(len(cups) + 1, total + 1)) cups_dict = {} n = len(cups) for i, c1 in enumerate(cups): c2 = cups[(i + 1) % n] cups_dict.setdefault(c1, Node(c1)) cups_dict.setdefault(c2, Node(c2)) cups_dict[c1].set_next(cups_dict[c2]) current_cup = cups_dict[cups[0]] for i in range(iter): cut_nums = [] cut_end = current_cup for _ in range(3): cut_end = cut_end.next cut_nums.append(cut_end.num) cut_start = current_cup.next dest_cup_num = current_cup.num - 1 while dest_cup_num < 1 or dest_cup_num in cut_nums: dest_cup_num = (dest_cup_num - 1) % (n + 1) dest_cup = cups_dict[dest_cup_num] current_cup.set_next(cut_end.next) cut_end.set_next(dest_cup.next) dest_cup.set_next(cut_start) current_cup = current_cup.next return cups_dict
def solve_part1(file_name): cards = read(file_name).split("\n\n") player1_cards = list(map(int, cards[0].splitlines()[1:])) player2_cards = list(map(int, cards[1].splitlines()[1:])) while len(player1_cards) and len(player2_cards): if (c1 := player1_cards.pop(0)) > (c2 := player2_cards.pop(0)):
def _read_and_parse_data(file_name): data = read(file_name).split("\n\n") your_ticket = _parse_ticket_str(data[1].split("\n")[1]) nearby_tickets = list( map(_parse_ticket_str, data[2].split("\n", maxsplit=1)[1].splitlines())) rules = pattern_extract_all("([a-z ]+): (\d+)\-(\d+) or (\d+)\-(\d+)", data[0], str, int, int, int, int) return your_ticket, nearby_tickets, rules
def solve_part2(file_name): total_all_in_group_answered_yes = 0 for answers in read(file_name).split("\n\n"): questions_answered_yes = Counter() for answers_from_person in (answers_per_person := answers.split("\n")): questions_answered_yes.update(answers_from_person) for v in questions_answered_yes.values(): total_all_in_group_answered_yes += v == len(answers_per_person)
def _count_words_valid_according_to_grammar(file_name, replace=None): grammar_rules, words = map(lambda x: x.splitlines(), read(file_name).split("\n\n")) replace = dict(map(lambda r: r.split(": "), replace)) if replace is not None else {} grammar = Grammar() for line in grammar_rules: lhs, rhss = line.split(": ") rhss = replace.get(lhs, rhss).replace('"', "") lhs = "S" if lhs == "0" else lhs for rhs in rhss.split(" | "): grammar.add(Rule(lhs, rhs.split())) def run_parse(sentence): parse = EarleyParse(" ".join(x for x in sentence), grammar) parse.parse() return parse.get() is not None return sum(map(run_parse, words))
def solve_part2(file_name): passports = read(file_name).split("\n\n") return quantify(map(_parse_passport, passports), pred=partial(_validate_with_model, PassportB))
def solve_part2(file_name): cards = read(file_name).split("\n\n") player1_cards = list(map(int, cards[0].splitlines()[1:])) player2_cards = list(map(int, cards[1].splitlines()[1:])) winner, cards = _recursive_game(player1_cards, player2_cards) return _score(cards)
def solve_part1(file_name): total = 0 for answers in read(file_name).split("\n\n"): total += len(set(answers.replace("\n", ""))) return total
last_spoken[x] = (None, i) i += 1 while i <= n_iterations: if x in last_spoken: a, b = last_spoken[x] x = 0 if a is None else b - a else: x = 0 b = None if x not in last_spoken else last_spoken[x][1] last_spoken[x] = (b, i) i += 1 return x if __name__ == "__main__": fetch_input_data_if_not_exists() part = "a" assert solve("0,3,6", 2020) == 436 assert solve("1,3,2", 2020) == 1 assert solve("2,1,3", 2020) == 10 assert solve("2,3,1", 2020) == 78 answer = solve(read(DataName.PUZZLE), 2020) submit(answer, part) part = "b" answer = solve(read(DataName.PUZZLE), 30000000) submit(answer, part)