Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
def calculate_trees(x_step, y_step, grid):
    next_point = Point(0, 0)
    tree_count = 0
    new_grid = []

    while next_point.get_y() < max_y:
        new_char = 'X'
        if next_point in tree_points:
            tree_count = tree_count + 1
            new_char = 'O'
        new_grid = replace_char_at_positions(grid, new_char, [next_point])

        next_point = create_next_point(next_point, x_step, y_step)

    print_grid(DEBUG, new_grid)
    return tree_count
Ejemplo n.º 4
0
def part1():
    grid = replace_char_at_positions(create_grid(max_x, max_y, '.'), TREE_CHAR, tree_points)
    return calculate_trees(3, 1, grid)