import aoc from itertools import product data = aoc.strgroups(19) rules, message = data d = {} while rules: i = rules.pop(0) num, match = i.split(": ") num = int(num) if any(j.isnumeric() and int(j) not in d for j in match.split(" ")): rules.append(i) continue if "\"" in match: d[num] = set([match.strip("\"")]) continue d[num] = set() for j in match.split(" | "): d[num] |= set("".join(k) for k in product(*(map(d.get, map(int, j.split(" ")))))) n = 0 m = 0 for i in message: n += int(i in d[0]) if not any(i.startswith(j) for j in d[42]) or not any(i.endswith(j) for j in d[31]): continue while any(i.startswith(j) for j in d[42]) and any(i.endswith(j) for j in d[31]): i = i[8:-8]
import aoc data = aoc.strgroups(4) n = 0 m = 0 for i in data: f = {} for j in " ".join(i).split(" "): k, v = j.split(":") if k == "cid": continue f[k] = v if set(f.keys()) == {"byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"}: n += 1 m += all([ 1920 <= int(f["byr"]) <= 2002, 2010 <= int(f["iyr"]) <= 2020, 2020 <= int(f["eyr"]) <= 2030, any([ f["hgt"][-2:] == "cm" and 150 <= int(f["hgt"][:-2]) <= 193, f["hgt"][-2:] == "in" and 59 <= int(f["hgt"][:-2]) <= 76 ]), len(f["hcl"]) == 7 and f["hcl"][0] == "#" and set(f["hcl"][1:]) <= set("0123456789abcdef"), f["ecl"] in ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"], len(f["pid"]) == 9 and set(f["pid"]) <= set("0123456789") ]) print(n, m) aoc.tock("ms")
import aoc data = aoc.strgroups(22) x = list(map(int, data[0][1:])) y = list(map(int, data[1][1:])) while x and y: i = x.pop(0) j = y.pop(0) if i > j: x.append(i) x.append(j) else: y.append(j) y.append(i) n = sum(i * j for i, j in enumerate(reversed(x + y), 1)) u = {0: list(map(int, data[0][1:]))} v = {0: list(map(int, data[1][1:]))} p = {0: set()} q = {0: set()} t = 0 b = None while t >= 0: while u[t] and v[t]: if tuple(u[t]) in p[t] or tuple(v[t]) in q[t]: t -= 1 b = True break p[t].add(tuple(u[t]))