def try_replacements(find_str, replace_str): occurrence_to_replace = 1 replaced = True while replaced: file_parser = FileParser('Input.txt') replaced = file_parser.replace(find_str, replace_str, occurrence_to_replace) if replaced: result = file_parser.parse_boot_code() if result[0] == 0: return result[1] occurrence_to_replace += 1 return False
def part2(): def perform_cycle(): new_active = [] min_x = min([point.x for point in current_active]) - 1 max_x = max([point.x for point in current_active]) + 1 min_y = min([point.y for point in current_active]) - 1 max_y = max([point.y for point in current_active]) + 1 min_z = min([point.z for point in current_active]) - 1 max_z = max([point.z for point in current_active]) + 1 min_w = min([point.w for point in current_active]) - 1 max_w = max([point.w for point in current_active]) + 1 for x in range(min_x, max_x + 1): for y in range(min_y, max_y + 1): for z in range(min_z, max_z + 1): for w in range(min_w, max_w + 1): print(w) point_to_check = Point4d(x, y, z, w) neighbours = get_neighbours_4d(point_to_check) active_neighbours = set.intersection(neighbours, current_active) if len(active_neighbours) == 3: new_active.append(point_to_check) elif point_to_check in current_active and len(active_neighbours) == 2: new_active.append(point_to_check) # print_points(new_active) return new_active parser = FileParser("Input.txt") points_2d = parser.read_points("#") grid = create_grid(max([point.x for point in points_2d]) + 1, max([point.y for point in points_2d]) + 1, '.') replace_char_at_positions(grid, '#', points_2d) print("0 cycles") print("==========") print("z = 0:") print_grid(True, grid) print("==========") current_active = [Point4d(point.x, point.y, 0, 0) for point in points_2d] for i in range(0, 6): print("CYCLE: " + str(i)) current_active = perform_cycle() return len(current_active)
def part1(): def get_adjacent(point, other_points): adjacent = [] for x in range(point.x - 1, point.x + 2): for y in range(point.y - 1, point.y + 2): point_to_check = Point(x, y) if point_to_check == point: continue if point_to_check in other_points: adjacent.append(point_to_check) return set(adjacent) seats = set(FileParser("Input.txt").read_points('L')) adjacent_map = {seat: get_adjacent(seat, seats) for seat in seats} return process(seats, adjacent_map, 4)
def part1(): return FileParser('Input.txt').parse_boot_code().pop()
def part2(): def populate_visibles(): def process_point(next_seat, x, y): this_point = Point(x, y) if this_point in seats: if next_seat is None: return this_point else: visible[next_seat].add(this_point) visible[this_point].add(next_seat) return this_point return next_seat def process_vertical(): for y in range(0, xdim): next_seat = None for x in range(0, xdim): next_seat = process_point(next_seat, x, y) def process_horizontal(): for x in range(0, xdim): next_seat = None for y in range(0, ydim): next_seat = process_point(next_seat, x, y) def process_right_diagonal(): start_x = xdim - 1 x = start_x y = 0 next_seat = None while start_x != -ydim: next_seat = process_point(next_seat, x, y) x += 1 y += 1 if x == xdim: next_seat = None start_x -= 1 x = start_x y = 0 def process_left_diagonal(): start_x = 0 x = start_x y = 0 next_seat = None while start_x != xdim + ydim: next_seat = process_point(next_seat, x, y) x -= 1 y += 1 if x == -1: next_seat = None start_x += 1 x = start_x y = 0 process_horizontal() process_vertical() process_left_diagonal() process_right_diagonal() seats = set(FileParser("Input.txt").read_points('L')) xdim = max([seat.x for seat in seats]) + 1 ydim = max([seat.y for seat in seats]) + 1 visible = {seat: set() for seat in seats} populate_visibles() return process(seats, visible, 5)
next_point = create_next_point(next_point, x_step, y_step) print_grid(DEBUG, new_grid) return tree_count def part1(): grid = replace_char_at_positions(create_grid(max_x, max_y, '.'), TREE_CHAR, tree_points) return calculate_trees(3, 1, grid) def part2(): grid = replace_char_at_positions(create_grid(max_x, max_y, '.'), TREE_CHAR, tree_points) slopes = [[1, 1], [3, 1], [5, 1], [7, 1], [1, 2]] results = [] for slope in slopes: results.append(calculate_trees(slope[0], slope[1], grid)) return math.prod(results) TREE_CHAR = '#' DEBUG = False parser = FileParser('Input.txt') tree_points = parser.read_points(TREE_CHAR) max_x = max([point.x for point in tree_points]) + 1 max_y = max([point.y for point in tree_points]) + 1 print(part1()) print(part2())