Esempio n. 1
0
    def run(self):
        while True:
            try:
                status = next(self.stdout)
            except StopIteration:
                print("END")
                break

            self.canvas.paint(self.position, 0)

            if status == 0:
                if self.direction == 1:
                    self.canvas.paint(self.position + Coord(0, 1), 5)
                elif self.direction == 2:
                    self.canvas.paint(self.position + Coord(0, -1), 5)
                elif self.direction == 3:
                    self.canvas.paint(self.position + Coord(-1, 0), 5)
                elif self.direction == 4:
                    self.canvas.paint(self.position + Coord(1, 0), 5)
            elif status == 1 or status == 2:
                self.distance += 1
                if self.direction == 1:
                    self.position += Coord(0, 1)
                elif self.direction == 2:
                    self.position += Coord(0, -1)
                elif self.direction == 3:
                    self.position += Coord(-1, 0)
                elif self.direction == 4:
                    self.position += Coord(1, 0)
                if status == 2:
                    print("-" * 80)
                    print("SUCCESS!!")
                    print("-" * 80)

            self.canvas.paint(self.position, self.direction)
Esempio n. 2
0
    def __init__(self, directions: List[Tuple[str, int]]):
        self.segments = []
        pos = Coord(0, 0)

        for direction, distance in directions:
            current = pos
            if direction == "R":
                pos += Coord(distance, 0)
            elif direction == "L":
                pos += Coord(-distance, 0)
            elif direction == "U":
                pos += Coord(0, distance)
            elif direction == "D":
                pos += Coord(0, -distance)
            self.segments.append(Segment(current, pos))
Esempio n. 3
0
 def __init__(self, maze):
     self.maze = maze
     self.neighbours = [[-1, 0], [1, 0], [0, -1], [0, 1]]
     self.blocked_elements = [1]
     self.all_paths = []
     for i in range(0, maze.shape[0]):
         self.all_paths.append([])
         for j in range(0, maze.shape[1]):
             self.all_paths[i].append(Coord((i, j)))
Esempio n. 4
0
 def Intersect(self, other) -> List[Tuple[Coord, int]]:
     intersections = []
     wireLength = Coord(0, 0)  # x is self, y is other
     for selfSegment in self.segments:
         wireLength.y = 0
         for otherSegment in other.segments:
             intersection = selfSegment.intersects(otherSegment)
             if intersection is not None:
                 totalWireLength = (
                     len(wireLength) +
                     len(intersection - selfSegment.begin
                         )  # additional length on self wire
                     + len(intersection - otherSegment.begin
                           )  # additional length on other wire
                 )
                 intersections.append((intersection, totalWireLength))
             wireLength.y += len(otherSegment)
         wireLength.x += len(selfSegment)
     return intersections
Esempio n. 5
0
 def intersection(horiSegment: Segment,
                  vertSegment: Segment) -> Optional[Coord]:
     if horiSegment.min("x") <= vertSegment.begin.x <= horiSegment.max(
             "x"):
         if vertSegment.min(
                 "y") <= horiSegment.begin.y <= vertSegment.max("y"):
             if vertSegment.begin.x == 0 and horiSegment.begin.y == 0:
                 return None
             return Coord(vertSegment.begin.x, horiSegment.begin.y)
     return None
Esempio n. 6
0
def part2(data):
    robot = Robot(data, False)

    # robot expects to start on a white panel
    robot.hull.paint(Coord(0, 0), WHITE)

    for color, position in robot.Paint():
        pass

    util.Answer(2, None)
    print(robot.hull)
Esempio n. 7
0
 def __init__(self, program):
     self.canvas = Canvas({
         0: " ",
         1: "^",
         2: "V",
         3: "<",
         4: ">",
         5: "#"
     }, True)
     self.position = Coord(0, 0)
     self.direction = 1
     self.stdout = computer.Run(program, self.stdin())
     self.distance = 0
Esempio n. 8
0
def part1(program):
    stdout = computer.Run(program, None)

    canvas = Canvas({0: " ", 1: "#", 2: "X", 3: "T", 4: "O"}, False)
    while True:
        try:
            pos = Coord(next(stdout), next(stdout))
            tile = next(stdout)
        except StopIteration:
            break

        canvas.paint(pos, tile)

    print(canvas)
    util.Answer(1, sum(1 for v in canvas.canvas.values() if v == 2))
Esempio n. 9
0
    def play(self):
        while True:
            try:
                pos = Coord(next(self.stdout), next(self.stdout))
                value = next(self.stdout)
            except StopIteration:
                return

            if pos.x == -1:
                self.segment = value
            else:
                if value == 3:
                    self.paddle = pos
                elif value == 4:
                    self.ball = pos
                self.canvas.paint(pos, value)
Esempio n. 10
0
def part1(data):
    coords = []
    effects = []
    canvas = Canvas({0: " ", 1: "#"}, inverted=False)
    for x in range(50):
        for y in range(50):
            coord = Coord(x, y)
            stdout = computer.Run(data[:], stdin(coord))
            coords.append(coord)
            effects.append(next(stdout))

            canvas.paint(coord, effects[-1])
        print(x)
    zip(coords, effects)

    util.Answer(1, sum(effects))

    print(canvas)
Esempio n. 11
0
    def shortest_path(self, ini, end, max_distance=np.inf):
        min_dist = np.full(self.maze.shape, np.inf)

        ini = Coord(ini)
        list_to_explore = [ini]
        min_dist[ini.y, ini.x] = 0

        while list_to_explore:
            current_coord = list_to_explore.pop(0)

            for neighbour in self.neighbours:
                next_coord = current_coord + neighbour
                if next_coord.x < 0 \
                        or next_coord.x >= self.maze.shape[1] \
                        or next_coord.y < 0 \
                        or next_coord.y >= self.maze.shape[0]:
                    continue

                if self.maze[next_coord.y,
                             next_coord.x] in self.blocked_elements:
                    continue

                new_dist = min_dist[current_coord.y,
                                    current_coord.x] + np.sqrt(
                                        neighbour[0]**2 + neighbour[1]**2)
                if new_dist > max_distance:
                    continue

                if new_dist < min_dist[next_coord.y, next_coord.x]:
                    min_dist[next_coord.y, next_coord.x] = new_dist
                    self.all_paths[next_coord.y][next_coord.x] = current_coord
                    list_to_explore.append(next_coord)

        path = [Coord(end)]
        if self.all_paths[Coord(end).y][Coord(end).x].x is not Coord(end).x or \
                self.all_paths[Coord(end).y][Coord(end).x].y is not Coord(end).y:
            while ini.x is not path[0].x or ini.y is not path[0].y:
                if self.all_paths[path[0].y][path[0].x] == path[0]:
                    break
                path = [self.all_paths[path[0].y][path[0].x]] + path

        return path
Esempio n. 12
0
    def __repr__(self):
        mins = Coord(0, 0)
        maxs = Coord(0, 0)
        for key in self.canvas.keys():
            mins.x = min(key.x, mins.x)
            mins.y = min(key.y, mins.y)
            maxs.x = max(key.x, maxs.x)
            maxs.y = max(key.y, maxs.y)

        canvas = []
        for i in range(maxs.y - mins.y + 1):
            canvas.append([".",] * (maxs.x - mins.x + 1))

        for key, color in self.canvas.items():
            pos = key - mins
            canvas[pos.y][pos.x] = self.colormap[color]

        if self.inverted:
            canvas.reverse()

        return "\n".join("".join(str(v) for v in line) for line in canvas)
Esempio n. 13
0
 def __init__(self, program, verbose=False):
     self.position = Coord(0, 0)
     self.direction = 0
     self.hull = Canvas()
     # pass the camera generator as stdin to the program
     self.stdout = computer.Run(program[:], self.Camera(), verbose)
Esempio n. 14
0
from lib import api, get_approximate_state_vectors, Coord, filter_by_radius

if __name__ == '__main__':
    # Get approximated results
    states = api.get_states().states
    latitude_min_max = (38, 58)
    longitude_min_max = (-8, 12)
    vectors = get_approximate_state_vectors(
        states,
        latitude_min_max=latitude_min_max,
        longitude_min_max=longitude_min_max)
    # Get accurate results
    paris = Coord(latitude=48.864716, longitude=2.349014)
    radius_km = 450
    answer = filter_by_radius(radius_km=radius_km,
                              from_coord=paris,
                              to_coords=vectors)
    print(answer)