|
| 1 | +"""Everyone Codes Day N.""" |
| 2 | + |
| 3 | +import collections |
| 4 | +import logging |
| 5 | +import itertools |
| 6 | +import math |
| 7 | +from lib import parsers |
| 8 | + |
| 9 | +log = logging.info |
| 10 | + |
| 11 | +def solve(part: int, data: str) -> int: |
| 12 | + """Solve the parts.""" |
| 13 | + total = 0 |
| 14 | + if part in [1, 2]: |
| 15 | + counts = collections.defaultdict(int) |
| 16 | + for i in data: |
| 17 | + if i.isupper(): |
| 18 | + counts[i.lower()] += 1 |
| 19 | + else: |
| 20 | + if (i == "a" and part == 1) or part == 2: |
| 21 | + total += counts[i] |
| 22 | + return total |
| 23 | + |
| 24 | + once = math.ceil(1000 / len(data)) |
| 25 | + twice = once * 2 |
| 26 | + three = once * 3 |
| 27 | + |
| 28 | + mentors = collections.defaultdict(set) |
| 29 | + for idx, m in enumerate(j for i in range(twice) for j in data): |
| 30 | + if m.isupper(): |
| 31 | + mentors[m.lower()].add(idx) |
| 32 | + print("Done counting mentors") |
| 33 | + |
| 34 | + edges = 0 |
| 35 | + for idx, m in enumerate(j for i in range(twice) for j in data): |
| 36 | + if m.islower(): |
| 37 | + for i in mentors[m.lower()]: |
| 38 | + if abs(idx - i) <= 1000: |
| 39 | + edges += 1 |
| 40 | + print("edges", edges) |
| 41 | + |
| 42 | + mentors = collections.defaultdict(set) |
| 43 | + for idx, m in enumerate(j for i in range(three) for j in data): |
| 44 | + if m.isupper(): |
| 45 | + mentors[m.lower()].add(idx) |
| 46 | + print("Done counting mentors") |
| 47 | + |
| 48 | + middle = 0 |
| 49 | + count = 1000 - twice |
| 50 | + for idx, m in enumerate(data): |
| 51 | + if m.islower(): |
| 52 | + for i in mentors[m.lower()]: |
| 53 | + if abs(idx + once * len(data) - i) <= 1000: |
| 54 | + middle += 1 |
| 55 | + print(f"{len(data)=}, {edges=}, {middle=}, {once=}, {twice=}, {three=}, {count=}, {count + twice=}") |
| 56 | + |
| 57 | + return edges + middle * count |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | +TESTS = [ |
| 62 | + (1, "ABabACacBCbca", 5), |
| 63 | + (2, "ABabACacBCbca", 11), |
| 64 | + (3, "AABCBABCABCabcabcABCCBAACBCa", 3442321), |
| 65 | +] |
| 66 | + |
| 67 | +if __name__ == "__main__": |
| 68 | + for _part, _data, expected in TESTS: |
| 69 | + assert solve(_part, (_data)) == expected |
| 70 | + print("Tests pass.") |
| 71 | + day = __file__.split("_", maxsplit=-1)[-1].split(".")[0] |
| 72 | + for _part in range(1, 4): |
| 73 | + with open(f"inputs/{day}.{_part}.txt", encoding="utf-8") as f: |
| 74 | + print(_part, solve(_part, (f.read()))) |
0 commit comments