def submit_b(res): submit(res, part="b", day=day, year=2020)
board_copy[n] = board_copy[n] board = board_copy board_copy = deepcopy(board) new_board = deepcopy(board) for p, flipped in board.items(): neighs = [ tuple(c1 + c2 for c1, c2 in zip(p, d)) for d in dirs.values() ] neighs = [board_copy[coords] for coords in neighs] num_flipped = sum(1 for n in neighs if n) new_board[p] = flipped if flipped: if num_flipped == 0 or num_flipped > 2: new_board[p] = False else: if num_flipped == 2: new_board[p] = True board = new_board return sum(1 for flipped in board.values() if flipped) aocd.submit(first()[0], part='a', day=DAY, year=YEAR) aocd.submit(second(), part='b', day=DAY, year=YEAR)
import functools from collections import Counter import aocd data: list[int] = aocd.numbers data.append(0) data.append(max(data) + 3) data.sort() diffs = [data[i + 1] - data[i] for i in range(len(data) - 1)] jolts = Counter(diffs) aocd.submit(jolts[1] * jolts[3], part="a") end = max(data) @functools.cache def ways_from(x: int): if x not in data: return 0 elif x == end: return 1 else: return ways_from(x + 1) + ways_from(x + 2) + ways_from(x + 3) aocd.submit(ways_from(0), part="b")
def submita(answer): submit(answer, part="a", day=11, year=2020)
def submit_a(res): submit(res, part="a", day=day, year=2020)
def submitb(answer): submit(answer, part="b", day=11, year=2020)
for num, colour in inner] bag_contains_colours[outer] = set(colour for (_, colour) in inner) # Part A: start from shiny gold bag and look upwards through the tree. done = set() queue = set() queue.add("shiny gold") while queue: col = queue.pop() # col is a colour done.add(col) for k, v in bag_contains_colours.items(): if col in v and k not in done: queue.add(k) aocd.submit(len(done) - 1, part='a') # deduct one because we don't count shiny gold # Part B: Just count the number of bags iteratively. queue = [(1, "shiny gold")] total = -1 while queue: num, col = queue.pop() total += num contains = bag_contains_full[col] if contains: contains = [(n * num, c) for (n, c) in contains] queue.extend(contains) else: continue
import re from aocd import get_data, submit answer = 0 for line in get_data(year=2020, day=2).splitlines(): match = re.match(r"(\d+)-(\d+) (\w): (\w+)", line) lower = int(match.group(1)) upper = int(match.group(2)) char = match.group(3) passwd = match.group(4) count = passwd.count(char) if lower <= count <= upper: answer += 1 submit(answer, part='a', year=2020, day=2)
for new_container in contained_dict[bag]: if new_container not in checked: frontier.add(new_container) return checked def count_contained(bags, starting_bag="shiny gold", n_bags=1): c = 0 for bag in bags[starting_bag]: next_bag = bag["type"] count = bag["count"] c += n_bags * count c += count_contained(bags, next_bag, count) * n_bags return c if __name__ == "__main__": d = get_data() contained_dict = defaultdict(set) container_dict = defaultdict(list) for line in d.split("\n"): parse_line(line, contained_dict, container_dict) answer_a = len(find_containers(contained_dict)) submit(answer_a, part="a", day=7, year=2020) answer_b = count_contained(container_dict) submit(answer_b, part="b", day=7, year=2020)
vcount = 0 for v in "aeiou": vcount += line.count(v) if vcount < 3: continue copy = " " + line bad = True for a, b in zip(copy, line): if a == b: bad = False break if bad: continue bad = False for pair in ["ab", "cd", "pq", "xy"]: if pair in line: bad = True break if bad: continue nice += 1 print(nice) submit(nice, part="a", day=5, year=2015)