コード例 #1
0
def day11_2(text):
    # create a numpy array
    picture = np.array([list(line) for line in lines(text)])
    rows, columns = picture.shape
    # get indices from the seat locations
    seat_locations = list(zip(*np.where(picture == "L")))

    # create dictionary neighbours
    dict_neighbours = {c: [] for c in seat_locations}

    dict_neighbours = get_neighbors_part2(locations=seat_locations,
                                          x=rows,
                                          y=columns,
                                          layout=picture,
                                          d=dict_neighbours)

    final_seating_arrangement = occupation_of_seats(layout=picture,
                                                    d=dict_neighbours,
                                                    threshold=4)

    unique, counts = np.unique(final_seating_arrangement, return_counts=True)

    nr_occupied_seats = dict(zip(unique, counts))['#']

    return nr_occupied_seats
コード例 #2
0
def day7_1(text, target='shiny gold'):
    """How many colors of bags can contain the target color bag? """
    rules = lines(text)
    colors = []
    x = [target]
    while x:
        s = x
        x = []
        for rule in rules:
            if any(value in rule.split('contain')[1] for value in s):
                colors.append(rule[:rule.find("bags contain")])
                x.append(rule[:rule.find("bags contain")])

    return len(set(colors))
コード例 #3
0
def day11_1(text):
    # create a numpy array
    picture = np.array([list(line) for line in lines(text)])
    # get indices from the seat locations
    seat_locations = list(zip(*np.where(picture == "L")))

    # create dictionary neigbours
    dict_neighbours = {}
    for s in seat_locations:
        dict_neighbours[s] = list(get_neighbours(cell=s, size=picture.shape))

    final_seating_arrangement = occupation_of_seats(layout=picture,
                                                    d=dict_neighbours,
                                                    threshold=3)

    unique, counts = np.unique(final_seating_arrangement, return_counts=True)

    nr_occupied_seats = dict(zip(unique, counts))['#']

    return nr_occupied_seats
コード例 #4
0
def day12_1(text):
    instructions = lines(text=text)

    ship_location = {"N": 0, "S": 0, "E": 0, "W": 0}

    facing = "E"
    degree = 90
    compass = {0: "N", 90: "E", 180: "S", 270: "W"}

    for instruction in instructions:
        action = instruction[:1]
        value = int(instruction[1:])
        print(f"Action: {action} with value {value}")
        if action in ("R", "L") and value > 360:
            print(f"WARNING")

        if action == "F":
            # Action F means to move forward by the given value in the direction the ship is currently facing.
            ship_location[facing] += value
        elif action in ("N", "S", "E", "W"):
            # Action N means to move north by the given value. etc.
            ship_location[action] += value
        elif action == "R":
            # Action R means to turn right the given number of degrees.
            degree = (degree + value) % 360
            facing = compass[degree]
        elif action == "L":
            # Action L means to turn left the given number of degrees.
            degree = degree - value
            if degree < 0:
                degree = 360 + degree
            facing = compass[degree]

    print(f"Ship location: {ship_location}")

    manhattan_dist = abs(ship_location["E"] -
                         ship_location["W"]) + abs(ship_location["N"] -
                                                   ship_location["S"])

    return manhattan_dist
コード例 #5
0
def day12_2(text):
    instructions = lines(text=text)

    ship_location = {"N": 0, "S": 0, "E": 0, "W": 0}
    waypoint_location = {
        "N": 1,
        "E": 10,
    }

    waypoint_dir = ["N", "E"]
    compass = {0: "N", 90: "E", 180: "S", 270: "W"}
    compass_reverse = {"N": 0, "E": 90, "S": 180, "W": 270}

    for instruction in instructions:
        action = instruction[:1]
        value = int(instruction[1:])
        print(f"Action: {action} with value {value}")
        print(ship_location)
        print(waypoint_location)

        if action == "F":
            # Action F means to move forward to the waypoint a number of times equal to the given value.
            for k, v in waypoint_location.items():
                ship_location[k] += (value * v)
        elif action in waypoint_dir:
            waypoint_location[action] += value
        elif action in {"N", "E", "S", "W"} - set(waypoint_dir):
            # Action N means to move the waypoint north by the given value. etc.
            if action == "N":
                waypoint_location["S"] -= value
            elif action == "E":
                waypoint_location["W"] -= value
            elif action == "S":
                waypoint_location["N"] -= value
            elif action == "W":
                waypoint_location["E"] -= value
        else:
            new_dir = []
            values = []

            if action == "R":
                # Action R means to rotate the waypoint around the ship right (clockwise)
                # the given number of degrees.
                for w in waypoint_dir:
                    deg = (compass_reverse[w] + value) % 360
                    dir = compass[deg]
                    new_dir.append(dir)
                    values.append(waypoint_location[w])
            elif action == "L":
                # Action L means to rotate the waypoint around the ship left (counter-clockwise)
                # the given number of degrees.
                for w in waypoint_dir:
                    deg = compass_reverse[w] - value
                    if deg < 0:
                        deg = 360 + deg
                    dir = compass[deg]
                    new_dir.append(dir)
                    values.append(waypoint_location[w])

            waypoint_location = dict(zip(new_dir, values))

            waypoint_dir = new_dir

    print(ship_location)
    manhattan_dist = abs(ship_location["E"] -
                         ship_location["W"]) + abs(ship_location["N"] -
                                                   ship_location["S"])
    return manhattan_dist