#!/usr/bin/python3 from shared import read_from_file bags = read_from_file('inputs/day7.txt') def parse_bags(bags): parsed_bags = {} for baglist in bags: blist = [baglist.split(" bags contain ")] for bag, contains in blist: if bag not in parsed_bags: parsed_bags[bag] = {} for contents in contains.replace(".", "").replace(" bags", "").replace( " bag", "").split(", "): if "no other" in contents: continue parsed_bags[bag][" ".join(contents.split(" ")[1:])] = int( contents.split(" ")[0]) return parsed_bags def traverse_gold(bags, bag, total=0): if not bags[bag]: return 0 if "shiny gold" in bags[bag]: return 1 mapped = map(lambda x: traverse_gold(bags, x, total), bags[bag]) total += any(list(mapped)) return total def find_gold_bags(bags):
#!/usr/bin/python3 from shared import read_from_file import re passport_inputs = read_from_file('inputs/day4.txt') def process_passports(passports): all_passports = [] p_i = {} for passport_input in passports: if passport_input == "": all_passports.append(p_i) p_i = {} continue items = passport_input.split(" ") for item in items: k, v = item.split(":") p_i[k] = v all_passports.append(p_i) return all_passports def filter_invalids(passport): required_fields = ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"] return True if all([field in passport for field in required_fields]) else False def filter_byr(passport):
#!/usr/bin/python3 from shared import read_from_file xmas = list(map(lambda x: int(x), read_from_file('inputs/day9.txt'))) preamble = 25 def can_sum_to(numbers, sums_to): for x in numbers: for y in numbers: if x + y == sums_to: return True return False def check_first_incorrect(numbers, preamble): for i in range(preamble,len(numbers) - 1): if not can_sum_to(numbers[i - preamble + 1: i + 1], numbers[i + 1]): return numbers[i + 1] return 0 def check_incorrect_set(numbers, target): numbers = [number for number in numbers if number < target] size = 2 while size < len(numbers): for number in range(0, len(numbers) - size): if sum(numbers[number:number + size]) == target: return min(numbers[number:number + size]) + max(numbers[number:number + size]) size += 1 return 0 first_incorrect = check_first_incorrect(xmas, preamble)
#!/usr/bin/python3 from shared import read_from_file program = read_from_file('inputs/day8.txt') def run_program(program): cur_index = 0 visited_indexes = set() accum = 0 while True: if cur_index in visited_indexes: return accum visited_indexes.add(cur_index) operator, amount = program[cur_index].split(" ") if operator == "acc": accum += int(amount) if operator == "jmp": cur_index += int(amount) else: cur_index += 1 def fix_program(program, tested_nodes=set()): cur_index = 0 visited_indexes = set() accum = 0 has_changed = False
#!/usr/bin/python3 from shared import read_from_file boardingpasses = read_from_file('inputs/day5.txt') def parse_passes(boardingpasses): boarding_ids = [1] for bpass in boardingpasses: row = int( "".join(list(map(lambda x: "1" if x == "B" else "0", bpass[0:7]))), 2) column = int( "".join(list(map(lambda x: "1" if x == "R" else "0", bpass[7:]))), 2) boarding_ids.append((row * 8) + column) return boarding_ids def find_seat(boarding_ids): for seat in range(sorted(boarding_ids)[0], sorted(boarding_ids)[-1] + 1): if seat not in boarding_ids and int(seat) > 10: return seat return 0 boarding_ids = parse_passes(boardingpasses) print("Part 1:", max(boarding_ids)) print("Part 2:", find_seat(boarding_ids))
#!/usr/bin/python3 from shared import read_from_file tree_map = read_from_file('inputs/day3.txt') def count_trees(tree_map, directions): x_pos = 0 y_pos = 0 move_right, move_down = directions tree_count = 0 while y_pos < len(tree_map): if tree_map[y_pos][x_pos % len(tree_map[y_pos])] == "#": tree_count += 1 y_pos += move_down x_pos += move_right return tree_count count_multiple = [[1, 1], [3, 1], [5, 1], [7, 1], [1, 2]] res = 1 for count in count_multiple: res *= count_trees(tree_map, count) print("Part 1", count_trees(tree_map, [3, 1])) print("Part 2", res)
#!/usr/bin/python3 from shared import read_from_file expenses = list(map(lambda x: int(x), read_from_file('inputs/day1.txt'))) def getExpensesTwo(expenses): # return [x * y for x in expenses for y in expenses if x + y == 2020][0] # Not sure if pretty for x in expenses: for y in expenses: if x + y == 2020: return x * y def getExpensesThree(expenses): for x in expenses: for y in expenses: for z in expenses: if x + y + z == 2020: return x * y * z print("Part 1", getExpensesTwo(expenses)) print("Part 2", getExpensesThree(expenses))
#!/usr/bin/python3 from shared import read_from_file import re passwords = read_from_file('inputs/day2.txt') def is_valid_password_count(passwords): valid_count = 0 for password in passwords: first_num, second_num, character, pwd = list( filter(None, re.split('-|:|\\s', password))) first_num, second_num = [int(x) for x in [first_num, second_num]] if pwd.count(character) >= first_num and pwd.count( character) <= second_num: valid_count += 1 return valid_count def is_valid_password_char(passwords): valid_count = 0 for password in passwords: first_num, second_num, character, pwd = list( filter(None, re.split('-|:|\\s', password))) first_num, second_num = [int(x) for x in [first_num, second_num]] if pwd[first_num - 1] == pwd[second_num - 1]: continue if pwd[first_num - 1] == character or pwd[second_num - 1] == character: valid_count += 1 return valid_count