#!/usr/bin/python from functools import reduce from load_puzzle_data import load_list lines = load_list(3) slopes = [(1,1), (3,1), (5,1), (7,1), (1,2)] tree_counts = [] for right, down in slopes: x = 0 y = 0 tree_count = 0 for line_no in range(0, len(lines), down ): if lines[line_no][x % len(lines[line_no])] == "#": tree_count +=1 x += right tree_counts.append(tree_count) print(reduce(lambda x, y: x*y, tree_counts))
#!/usr/bin/python from load_puzzle_data import load_list import re data = load_list(2) regex = r'(\d{1,2})\-(\d{1,2})\s([a-z])\:\s([a-z]{1,})' count = 0 for line in data: low, high, letter, password = re.findall(regex, line).pop() if password.count(letter) in range(int(low), int(high) + 1): count += 1 print(count)
#!/usr/bin/python from math import ceil, floor from load_puzzle_data import load_list lines = load_list(5) highest_seat_id = 0 for line in lines: low, high = 0, 127 for i in range(0, 6): if line[i] == 'F': high = floor((low + high) / 2) elif line[i] == 'B': low = ceil((low + high) / 2) if line[6] == 'F': row = low else: row = high left, right = 0, 7 for i in range(7, 10): if line[i] == 'R': left = floor((left + right) / 2) elif line[i] == 'L': right = ceil((left + right) / 2) if line[9] == 'L': col = left else: col = right seat_id = (row * 8) + col if highest_seat_id < seat_id:
#!/usr/bin/python from load_puzzle_data import load_list data = load_list(1, True) options = [] for i in data: remainder = 2020 - i if remainder in data: print(remainder * i) break
#!/usr/bin/python import re from load_puzzle_data import load_list lines = load_list(4) passports = [] data = [] item = {} for line in lines: if line == "": data.append(item) item = {} line = line.split(" ") if line[0] != "": for l in line: prop = l.split(":") item[prop[0]] = prop[1] properties = ['byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid'] valid_passports = 0 for item in data: valid = [] s = sum([1 for prop in properties if prop in item.keys()]) if s == len(properties): # birth year if int(item['byr']) in range(1920, 2003): valid.append(1)