def main(): from helpers.data_access import get_data data = get_data(day=1).split("\n") result = expense_check((int(entry) for entry in data)) print(f"Expense check result is {result}") result_b = expense_check((int(entry) for entry in data), num_numbers=3) print(f"Extended xpense check result is {result_b}")
def main(): from helpers.data_access import get_data data = get_data(day=2).split("\n") inputs = (parse_input_line(line) for line in data) inputs_1, inputs_2 = tee(inputs) count = count_valid(inputs_1) print(f"There are {count} valid passwords.") count_v2 = count_valid(inputs_2, checker=is_password_valid_v2) print(f"Actually there's {count_v2} valid passwords.")
seen_lines = [0] current, acc = 0, 0 while True: current, acc = run_instruction(data, current, acc) if current in seen_lines: return current, acc if current == len(data) - 1: if data[current][1] == "acc": acc += data[current][2] return current, acc seen_lines.append(current) if __name__ == "__main__": data = get_data(day=8).split("\n") ### Part 1 t0 = time() data = [x.split() for x in data] data = [[i, x[0], int(x[1])] for i, x in enumerate(data)] current, acc = run_data_loop(data) print(f"first line to repeat itself {current}, value of accumulator {acc}") print(f"runtime {time()-t0:.4f} seconds") ### Part 2 t0 = time() current, acc = 0, 0 for i, line in enumerate(data): if line[1] == "nop": data_mod = deepcopy(data) data_mod[line[0]][1] = "jmp"
def check_pid(p_dict): if len(p_dict["pid"]) == 9: try: int(p_dict["pid"]) return True except ValueError: return False else: return False if __name__ == "__main__": data = get_data(day=4).split("\n\n") ### Part 1 valid_passports = 0 t0 = time() for line in data: a = re.split(" |\n", line) p_dict = {x.split(":")[0]: x.split(":")[1] for x in a} keys = p_dict.keys() if (len(keys) == 8) or (len(keys) == 7 and "cid" not in keys): valid_passports += 1 print(f"valid passports: {valid_passports}") print(f"runtime {time()-t0:.4f} seconds") ### Part 2 t0 = time() valid_passports = 0
from time import time import numpy as np from collections import Counter def get_cluster_sizes(diff): cluster_size = 0 cluster_sizes = {} for i in diff: if i == 3: cluster_sizes[cluster_size] += 1 if __name__ == "__main__": data = list(map(int, get_data(day=10).split("\n"))) ### Part 1 t0 = time() sorted_data = [0] + sorted(data) + [max(data) + 3] diff = np.diff(sorted_data) counts = Counter(diff) print(f"1-jolt diffs: {counts[1]}, 3-jolt diffs: {counts[3]}," + f" prod = {counts[1]*counts[3]}") print(f"runtime {time()-t0:.4f} seconds") ### Part 2 t0 = time() # based on the size of a cluster of 1s (say 311113 is size 4), number of independent # solutions. Total possible solutions are the product of the number of independent # solutions per cluster sols_per_cluster_size = {2: 2, 3: 4, 4: 7}