Ejemplo n.º 1
0
def test_best_bases():
    expected = [('data/10-example-1.txt', Point(3, 4), 8),
                ('data/10-example-2.txt', Point(5, 8), 33),
                ('data/10-example-3.txt', Point(1, 2), 35),
                ('data/10-example-4.txt', Point(6, 3), 41),
                ('data/10-example-5.txt', Point(11, 13), 210)]
    for fn, expected_base, expected_seen in expected:
        asteroids = load_points(fn)
        base = best_base(asteroids)
        assert base == expected_base
        seen = seen_count(base, asteroids)
        assert seen == expected_seen
        print(f">>>> passed test_best_bases: {fn}")
Ejemplo n.º 2
0
def test_load_points():
    points = load_points('data/10-example-1.txt')
    assert points == [
        Point(0, 2),
        Point(1, 0),
        Point(1, 2),
        Point(2, 2),
        Point(3, 2),
        Point(3, 4),
        Point(4, 0),
        Point(4, 2),
        Point(4, 3),
        Point(4, 4)
    ]
Ejemplo n.º 3
0
 def tile_at(p: Point) -> Tile:
     if p == self.pos:
         return Tile.ROBOT
     elif p == Point(0, 0):
         return Tile.ORIGIN
     elif p in self.plan:
         return self.plan[p]
     else:
         return Tile.UNKNOWN
Ejemplo n.º 4
0
 def __init__(self,
              sensor: Callable[[Heading], Tile],
              pos: Point = Point(0, 0)):
     self.sensor = sensor
     self.pos = pos
     self.last_heading = Heading.NORTH
     self.plan = {pos: Tile.FLOOR}
     self.distances = {pos: 0}
     self.look_around()
Ejemplo n.º 5
0
def test_gen_seen():
    asteroids = load_points('data/10-example-1.txt')
    assert list(gen_seen(Point(0, 2), asteroids)) == [
        Point(1, 0),
        Point(1, 2),
        Point(3, 4),
        Point(4, 0),
        Point(4, 3),
        Point(4, 4)
    ]
Ejemplo n.º 6
0
def draw_screen(contents: Iterable[tuple[Point, int]]) -> None:
    screen = dict(contents)
    if not screen:
        print("no screen signal")
        return

    min_x, max_x = 0, max(p.x for p in screen.keys())
    min_y, max_y = 0, max(p.y for p in screen.keys())

    for y in range(min_y, max_y+1):
        print(''.join(
            TILES[screen.get(Point(x, y))]
            for x in range(min_x, max_x+1)
        ))

    if SCORE_POINT in screen:
        score = screen[SCORE_POINT]
        print(f"Score: {score}")
Ejemplo n.º 7
0
 def __init__(self, machine: Machine, debug: bool = False):
     self.brain = machine.as_function()
     self.pos = Point(0, 0)
     self.heading = Heading.NORTH
     self.debug = debug
Ejemplo n.º 8
0
def part_2():
    robot = Robot(Machine(load_tape('data/11-program.txt')), debug=False)
    grid2 = defaultdict(lambda: Color.NONE)
    grid2[Point(0, 0)] = Color.WHITE
    robot.paint_on(grid2)
    draw_grid(grid2)
Ejemplo n.º 9
0
def draw_grid(grid: Grid):
    min_x, max_x = minmax(pos.x for pos in grid.keys())
    min_y, max_y = minmax(pos.y for pos in grid.keys())
    c = {Color.BLACK: '.', Color.WHITE: '#', Color.NONE: ' '}
    for y in range(min_y, max_y + 1):
        print(''.join(c[grid[Point(x, y)]] for x in range(min_x, max_x + 1)))
Ejemplo n.º 10
0
def load_points(fn) -> list[Point]:
    with open(fn) as f:
        return sorted(
            Point(x, y) for y, line in enumerate(f)
            for x, c in enumerate(line.strip()) if c != '.')
Ejemplo n.º 11
0
def test_rotate_laser():
    asteroids = load_points('data/10-example-5.txt')
    base = best_base(asteroids)
    assert base == Point(11, 13)

    vaporized = list(rotate_laser(base, asteroids))
    assert len(vaporized) == 299

    assert vaporized[0] == Point(11, 12)
    assert vaporized[1] == Point(12, 1)
    assert vaporized[2] == Point(12, 2)
    assert vaporized[9] == Point(12, 8)
    assert vaporized[19] == Point(16, 0)
    assert vaporized[49] == Point(16, 9)
    assert vaporized[99] == Point(10, 16)
    assert vaporized[198] == Point(9, 6)
    assert vaporized[199] == Point(8, 2)
    assert vaporized[200] == Point(10, 9)
    assert vaporized[-1] == Point(11, 1)
Ejemplo n.º 12
0
def test_can_be_seen():
    assert can_be_seen(Point(0, 0), Point(1, 1), set()) is True
    assert can_be_seen(Point(0, 0), Point(2, 2), {Point(1, 1)}) is False
    assert can_be_seen(Point(0, 0), Point(6, 6), {Point(4, 4)}) is False
    assert can_be_seen(Point(0, 0), Point(2, 3), {Point(1, 1)}) is True
    assert can_be_seen(Point(0, 2), Point(2, 2), {Point(1, 2)}) is False
    assert can_be_seen(
        Point(1, 0), Point(2, 3),
        {Point(1, 0), Point(1, 1), Point(2, 3)}) is True
Ejemplo n.º 13
0
 def char_at(cx: int, cy: int) -> str:
     return tile_at(Point(cx, cy)).char
Ejemplo n.º 14
0
 def load_screen():
     seq = list(m.run_output_only())
     for h in range(0, len(seq), 3):
         x, y, t = seq[h:h+3]
         yield Point(x, y), t
Ejemplo n.º 15
0
from common.math import sgn
from common.xy import Point
from y2019.intcode import load_tape
from y2019.intcode import Machine

TILES = {
    0: '.',
    1: '#',
    2: 'X',
    3: '=',
    4: 'o',
    None: ' '
}

SCORE_POINT = Point(-1, 0)
SCREEN_WIDTH = 40
SCREEN_HEIGHT = 24


def draw_screen(contents: Iterable[tuple[Point, int]]) -> None:
    screen = dict(contents)
    if not screen:
        print("no screen signal")
        return

    min_x, max_x = 0, max(p.x for p in screen.keys())
    min_y, max_y = 0, max(p.y for p in screen.keys())

    for y in range(min_y, max_y+1):
        print(''.join(
Ejemplo n.º 16
0
def stars_from_lines(lines: Iterable[str]) -> Iterable[Star]:
    for line in lines:
        p_x, p_y, v_x, v_y = parse_line(line.strip(),
                                        "position=<$, $> velocity=<$, $>")
        yield Point(int(p_x), int(p_y)), Vector(int(v_x), int(v_y))