def part_1(): start = "shiny gold" bags = {} raw_data = read_input(7, 1) for l in raw_data: parent, children = parse_row(l) bags[parent] = children nodes = bags.keys() connected = [] friends = [] for k, v in bags.items(): if k != start: if start in v: connected.append(k) friends.append(k) while connected: node = connected.pop() for k, v in bags.items(): if k != node: if node in v: connected.append(k) friends.append(k) print(len(set(friends)))
def part_2(): start = "shiny gold" bags = {} raw_data = read_input(7, 1) for l in raw_data: parent, children = parse_row(l, with_counts=True) bags[parent] = children # children = [] # to_visit = [] # for child in bags[start]: # count, node = child.split(" ", maxsplit=1) # to_visit.append((node, int(count))) # children.append((node, int(count))) # while to_visit: # current = to_visit.pop() # for child in bags[current[0]]: # count, node = child.split(" ", maxsplit=1) # to_visit.append((node, int(count))) # children.append((node, int(count))) # print(children) total = traverse(bags, start) print(total)
def part_2(): raw_input = read_input(6, 1) grouped = group_partition(raw_input) total = 0 for g in grouped: total += len(g) print(total)
def part_1(): raw_input = read_input(6, 1) partitioned = partition_input(raw_input) count = 0 for partition in partitioned: p_set = set(partition) count += len(p_set) print(count)
def part_2(): total = 57195069 raw_input = read_input(9, 1) conv_input = [int(e) for e in raw_input] for window_size in range(2, len(conv_input)): for w in window(conv_input, window_size): if sum(w) == total: print(min(w) + max(w))
def part_1(): passwords = read_input(2, 1) valid = [] for password in passwords: minimum, maximum, character, p = parse_password_row(password) letter_counts = Counter(p) if int(minimum) <= letter_counts[character] <= int(maximum): valid.append(password) print(len(valid))
def part_2(): """ Note: For this task, pos1 and pos2 are not 0-based position in the string, that's why the -1 """ passwords = read_input(2, 1) valid = [] for password in passwords: pos1, pos2, character, p = parse_password_row(password) # pos1 and pos2 are not allowed to be identical according to password policy if (p[pos1 - 1] != p[pos2 - 1]) and ( # either pos1 or pos2 needs to be the character p[pos1 - 1] == character or p[pos2 - 1] == character ): valid.append(password) print(len(valid))
def part_2(): raw_input = read_input(8, 1) instructions = get_instructions(raw_input) for i in range(0, len(instructions)): current = instructions[i] if current[0] == "jmp": tmp = ("nop", current[1]) elif current[0] == "nop": tmp = ("jmp", current[1]) else: continue instructions[i] = tmp total, finished = parse_instructions(instructions) if finished: print(total) exit(0) instructions[i] = current
def part_1() -> List[Dict]: passports = [] puzzle_input_raw = read_input(4, 1) current = dict() for l in puzzle_input_raw: if l: entries = l.split(" ") for e in entries: k, v = e.split(":") current[k] = v else: passports.append(current) current = dict() passports.append(current) valid = [p for p in passports if len(p.keys()) == 8] missing_only_one = [p for p in passports if len(p.keys()) == 7] missing_only_cid = [p for p in missing_only_one if "cid" not in p] return valid + missing_only_cid
def part_1(): puzzle_input_raw = read_input(5, 1) highest = 0 pass_str = "" seat_ids = [] for boarding_pass in puzzle_input_raw: row, column = boarding_pass[:-3], boarding_pass[-3:] row_number = search(row, 0, 127) column_number = search(column, 0, 7) seat_id = row_number * 8 + column_number seat_ids.append(seat_id) # print( # f"{boarding_pass}: row {row_number}, column {column_number}, seat id {seat_id}" # ) if seat_id > highest: highest = seat_id pass_str = boarding_pass print(f"{pass_str}: {highest}") return sorted(seat_ids)
def part_1(step_x=3, step_y=1): raw_input = read_input(3, 1) x = 0 y = 0 pos_x = 0 pos_y = 0 trees = 0 mod = len(raw_input[0]) while pos_y < len(raw_input): row = raw_input[pos_y] field = row[pos_x % len(row)] if field == "#": trees += 1 pos_x += step_x pos_y += step_y return trees
from common.read import read_input if __name__ == "__main__": puzzle_input_raw = read_input(1, 1) puzzle_input = [int(e) for e in puzzle_input_raw] # Puzzle 1 - The sum is supposed to be 2020, which means one of the numbers needs to be >= 1010 upper = set([e for e in puzzle_input if e >= 1010]) counter_part = [(2020 - e) for e in puzzle_input if e <= 1010] for e in counter_part: if e in upper: print(f"{e} + {2020-e} = 2020 - {e} * {2020-e} = {e * (2020-e)}") # Puzzle 2 - naive approach year = 2020 parts = 3 # at least one number needs to bee larger than 2020/3 = 673,3 = 674 (when talking about integers) first_number = set([e for e in puzzle_input if e >= int(year / 3)]) # remainders contain the sum of the two missing numbers remainders = set([(year - e) for e in first_number]) # for each of the remainders repeat the same procedure and try to find two numbers from # puzzle input that give the sum. Basically reiterate the approach from part 1 for r in remainders: second_number = set([e for e in puzzle_input if e >= int(r / 2)]) counter_part_3 = set([r - e for e in second_number])
def part_1(): raw_input = read_input(8, 1) instructions = get_instructions(raw_input) total, finished = parse_instructions(instructions) print(f"Finished: {finished}. Acc: {total}")
def part_1(): raw_input = read_input(9, 1) conv_input = [int(e) for e in raw_input] for sub in window(conv_input, 26): check(sub[:-1], sub[-1])