def __main(): input = read_path_lines(f'{DATA_DIR}input') with localtimer(): print('Part 1') Program(input, dict(a=0, b=0, c=0, d=0)).run() with localtimer(): print('Part 2') Program(input, dict(a=0, b=0, c=1, d=0)).run()
def do_moves(self, num_moves: int): self._init_min_max() with U.localtimer(): for i in range(0, num_moves): self.play_move() self.log('-- final --') self.log_current()
def run_world_cycles(start: List[List[str]], world_type: Type[Union[World, HyperWorld]]): world = world_type.from_str_list(start) with U.localtimer(): for i in range(0, 6): # world.print() world = world.run_cycle() print(world.count_all_active())
def part2(start: List[List[str]]): world = HyperWorld.from_str_list(start) with U.localtimer(): for i in range(0, 6): # world.print() world = world.run_cycle() print(world.count_all_active())
def part2(data: List[List[str]]): sa = SeatArrangement(data) with U.localtimer(): while True: has_changed, seats_occupied = sa.apply_rules(check_visible=True, tolerance=5) if not has_changed: break print(seats_occupied)
def part1(data: List[List[str]]): sa = SeatArrangement(data) with U.localtimer(): while True: has_changed, seats_occupied = sa.apply_rules() if not has_changed: break print(seats_occupied)
def part2(g: CupsGame): start_max = max(g.item_map) + 1 with U.localtimer(): for i in range(start_max, 1000000 + 1): g.insert(i) g.do_moves(10000000) one = g.item_map[1] print(one.next.val, one.next.next.val) print(one.next.val * one.next.next.val)
def part1(lines: List[str]) -> Dict[Point, int]: points_found = {} with U.localtimer(): for line in lines: end_point = navigate_line(line) if points_found.get(end_point) is not None: points_found[end_point] = points_found[end_point] ^ 1 else: points_found[end_point] = 0 print(sum(1 for v in points_found.values() if v == 0)) return points_found
def part1(in_file: List[str]) -> TileGrid: tiles = [i.split('\n') for i in in_file] tile_map = {} for tile in tiles: _, tile_id = tile[0].split(' ') tile_map[int(tile_id[:-1])] = Tile(tile[1:]) tg = TileGrid(tile_map) with U.localtimer(): tg.fill_all() grid = tg.filled_grid tl, tr, bl, br = tg.get_corners() print(grid[tl] * grid[tr] * grid[bl] * grid[br]) return tg
def speaking_game(nums: List[int], game_end: int): with U.localtimer(): num_to_turn = {n: i + 1 for i, n in enumerate(nums)} prev_num = 0 prev_turn = len(nums) + 1 while prev_turn < game_end: num_was_spoken = num_to_turn.get(prev_num, 0) != 0 if num_was_spoken: spoken_num = prev_turn - num_to_turn[prev_num] else: spoken_num = 0 num_to_turn[prev_num] = prev_turn prev_turn += 1 prev_num = spoken_num print(prev_num)
def part2(tile_dict: Dict[Point, int], days: int): with U.localtimer(): for day in range(1, days + 1): new_dict = {} seen_today = set() for point in tile_dict: neighbors = point.get_neighbors() for n in [*neighbors, point]: if n in seen_today: continue curr_color = 1 if tile_dict.get(n) is None else tile_dict[n] live_neighbors = sum(tile_dict.get(neighbor) == 0 for neighbor in n.get_neighbors()) if (curr_color == 1 and live_neighbors == 2) or (curr_color == 0 and (0 < live_neighbors < 3)): new_dict[n] = 0 seen_today.add(n) tile_dict = new_dict print(f'Day {day}: {sum(1 for v in tile_dict.values() if v == 0)}')
def do_problems(problems: List[str], math: Callable): with U.localtimer(): res = [math(i) for i in problems] # print(res) print(sum(res))
def part2(num_list: List[int], goal_value: int): with localtimer(): a, b, c = get_summands(num_list, goal_value, 3) print(f'3 summands are {a}, {b}, {c}') print(a * b * c)
def part2(rows: List[str]): with localtimer(): total = count_valid(rows, is_valid_by_pos) print(f'Number of valid rows: {total}')
def part1(sm: SlopeMap): with localtimer(): trees = sm.trees_on_slope(3, 1) print(trees)
def part1(num_list: List[int], goal_value: int): with localtimer(): a, b = get_summands(num_list, goal_value) print(f'2 summands are {a}, {b}') print(a * b)
def part2(sm: SlopeMap): trees = (sm.trees_on_slope(h, v) for h, v in [(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)]) with localtimer(): res = reduce(operator.mul, trees) print(res)
def part2(passports: List[str]): passports = Passports.from_str_list(passports) with localtimer(): res = passports.valid_count(Passports.validate) print(f'Number of valid passports: {res}')
def part1(card_key: int, door_key: int): with U.localtimer(): loop_size, device_type = find_loop_size(card_key, door_key) key = door_key if device_type == 0 else card_key private_key = get_encryption_key(key, loop_size) print(private_key)