def __main(): # return _test() # rules = Rules(parse_data(debug=True, cls=Rule)) # print(len(rules)) # print(part1(rules)) cubes = parse_data(debug=True, cls=Cube) exhaust(print, cubes)
def _display(points): max_x = max(x for x, _ in points) max_y = max(y for _, y in points) board = [[' '] * (max_x + 1) for _ in range(max_y + 1)] for x, y in points: board[y][x] = '\u2589' exhaust(print, map(''.join, board))
def aoc20_b(fname=20): parts = Particle.read_data(fname) parts_by_id = {p.id: p for p in parts} for _ in range(1000): U.exhaust(parts_by_id.pop(d) for d in _find_dupes(parts)) U.exhaust(p.tick() for p in parts) return len(parts_by_id)
def aoc20_a(fname=20, history_len=500): parts = Particle.read_data(fname) key = lambda _p: _p.p.manhattan history = deque(maxlen=history_len) while True: U.exhaust(p.tick() for p in parts) history.append(min(parts, key=key).id) if len(history) == history_len and len(set(history)) == 1: return history[0]
def __init__(self, instructions, mode=0): self._insts = instructions U.exhaust(map(print, enumerate(instructions))) self._regs = dict.fromkeys('abcdefgh', 0) self._regs['a'] = mode self._pc = 0 self._max_pc = 0 self._inst_counts = Counter() self._mul_count = 0
def _update_bot(self, bot_id): ids = l_id, h_id = self._bot_insts[bot_id] try: pot_target = l, h = sorted(self._bots[bot_id]) if self._target == pot_target: raise ResExc(bot_id) except ValueError: return self._bots[bot_id].clear() self._bots[l_id].append(l) self._bots[h_id].append(h) exhaust(map(self._update_bot, ids))
def __main(): scanners = parse_data(debug=True) print(scanners) print(scanners[0][1]) print(Point3d(10, 1, 2) + Point3d(1, 1, 1)) print(len(scanners[-1].points)) print(pairs := _get_coverage_pairs(scanners)) print('=============================') print(_find_possible_orientations(scanners, pairs)) return print(canonical_order()) print(len(canonical_order())) exhaust(print, Point3d(4, 6, 5).canonical) points = {Point3d(5, 6, -4), Point3d(-5, 4, -6), Point3d(4, 6, 5), Point3d(-4, -6, 5), Point3d(-6, -4, -5)} print(len(points)) print(points & set(Point3d(4, 6, 5).canonical) == set(points))
def _traverse( node: str = 'start', seen: frozenset[str] = frozenset(), seen_twice: bool = False, path: tuple[str, ...] = ()) -> None: path += (node, ) if node == 'end': valid_paths.add(path) return if node.islower(): if node in seen: return if node != 'start' and not seen_twice: exhaust( _traverse(n, seen, seen_twice=True, path=path) for n in graph[node]) seen |= {node} exhaust(_traverse(n, seen, seen_twice, path) for n in graph[node])
def part2(init_state, state_map): def cache_state(s): starting = s.index('#') s = s[starting:s.rindex('#') + 1] s_in_cache = s in cache cache[s].append(starting) if s_in_cache: return True offset, state = _add_empties(15000, init_state) cache = defaultdict(list) cache_state(state) for next_state in _simulate(state, state_map): if cache_state(next_state): break final_state = last(cache.keys()) exhaust(print, enumerate(cache.values())) num_gen = 50_000_000_000 first_150 = 80 final_offset = -first_150 - (num_gen - 150) return _score(final_state, final_offset)
def part2(cmds): points = dict.fromkeys(RC(0, 0).to(RC(999, 999)), 0) exhaust(cmd(points) for cmd in cmds) return sum(points.values())
regs = regs.copy() regs[inst.output] = inst.a return regs @register def gt(self, inst: Inst, regs: List[int]): regs[inst.output] = int(inst.a > inst.b) return regs @register def eq(self, inst: Inst, regs: List[int]): regs[inst.output] = int(inst.a == inst.b) return regs exhaust(_register_helper, _registry) def test(): i = Interpreter() regs = [1, 2, 3, 4] assert i.addr(Inst(2, 2, 3, 0), regs)[0] == 7 assert i.addi(Inst(2, 2, 3, 0), regs)[0] == 6 assert i.mulr(Inst(2, 2, 3, 0), regs)[0] == 12 assert i.muli(Inst(2, 2, 3, 0), regs)[0] == 9 assert i.banr(Inst(2, 2, 3, 0), regs)[0] == 0 assert i.bani(Inst(2, 2, 3, 0), regs)[0] == 3 assert i.borr(Inst(2, 2, 3, 0), regs)[0] == 7 assert i.bori(Inst(2, 2, 3, 0), regs)[0] == 3 assert i.setr(Inst(2, 2, 3, 0), regs)[0] == 3 assert i.seti(Inst(2, 2, 3, 0), regs)[0] == 2
def parse_instructions(self, insts: List[str]): exhaust(map(self._parse_inst, insts))
def run(self, program: Program): program.suppress_output = True proc = process(program) exhaust(map(self._update_board, *([proc] * 3))) self.draw()
def process_no_yield(program: Program): U.exhaust(process(program)) return program
def __main(): print(aoc8_a()) print() print() U.exhaust(map(print, aoc8_b()))
def from_comps(cls, comps: Iterable[Comp]): res = cls() U.exhaust(map(res.add, comps)) return res