Beispiel #1
0
 def test_pad(self, x, y, expected_corners):
     board = Board.from_string("abcd\nefgh\nijkl\nmnop\n", fill_value=".")
     view = board.adjacent_view(x, y)
     padded = pad(view, x, y, board.grid)
     assert padded.shape == (3, 3)
     assert padded[0, 0] == expected_corners[0]
     assert padded[-1, 0] == expected_corners[1]
     assert padded[0, -1] == expected_corners[2]
     assert padded[-1, -1] == expected_corners[3]
Beispiel #2
0
def part1():
    with open(get_input_name(24, 2019)) as fobj:
        char_board = Board.from_string(fobj.read().strip())
        board = char_board.grid == "#"
        gol = GoL(board)
        while not gol.duplicate():
            gol.step()
        bd_points = (2 ** np.arange(25))
        points = bd_points * gol._board.flatten()
        print("This board has a biodiversity of {}".format(np.sum(points)))
Beispiel #3
0
def load2(fobj):
    board = Board.from_string(fobj.read(),
                              fill_value=0,
                              dtype=int,
                              growable=False)
    g = board.grid
    g = np.tile(g, (5, 5))
    overlay = np.repeat(np.arange(5), board.size_x)
    overlay = overlay + overlay.reshape((overlay.shape[0], 1))
    m = np.ma.asarray(g + overlay)
    m.mask = m <= 9
    m = m - 9
    board.grid = m.data
    return make_graph(board)
Beispiel #4
0
def load():
    tiles = []
    with open(get_input_name(20, 2020)) as fobj:
        lines = deque(fobj.read().splitlines(keepends=False))
        while lines:
            title = lines.popleft()
            input = []
            for _ in range(10):
                input.append(lines.popleft())
            board = Board.from_string("\n".join(input))
            grid = board.grid
            edges, grid = collect_edges(grid)
            grid = np.flipud(grid)
            flipped, grid = collect_edges(grid)
            tiles.append(Tile(title, edges, flipped))
            lines.popleft()
    return tiles
Beispiel #5
0
def load(fobj):
    return Board.from_string(fobj.read(),
                             fill_value=0,
                             dtype=int,
                             growable=False)
Beispiel #6
0
def capture_camera(intcode):
    chars = []
    intcode.execute(output_func=chars.append)
    board = Board.from_string("".join(chr(c) for c in chars))
    return board
Beispiel #7
0
9,0

fold along y=7
fold along x=5
""")

INITIAL_BOARD = Board.from_string("""\
00010010010
00001000000
00000000000
10000000000
00010000101
00000000000
00000000000
00000000000
00000000000
00000000000
01000010110
00001000000
00000010001
10000000000
10100000000
""",
                                  fill_value=0,
                                  dtype=int,
                                  growable=False)

PART1_RESULT = 17


class TestDec13():
    @pytest.fixture
Beispiel #8
0
def make_board(string):
    char_board = Board.from_string(string)
    return char_board.grid == "#"
Beispiel #9
0
def load1(fobj):
    board = Board.from_string(fobj.read(),
                              fill_value=0,
                              dtype=int,
                              growable=False)
    return make_graph(board)
Beispiel #10
0
def load():
    with open(get_input_name(3, 2020)) as fobj:
        return Board.from_string(fobj.read())
Beispiel #11
0
import pytest

from ibidem.advent_of_code.board import Board
from ..dec17 import get_intersections, calculate_alignments

SMALL_MAP = textwrap.dedent("""\
    ..#..........
    ..#..........
    #######...###
    #.#...#...#.#
    #############
    ..#...#...#..
    ..#####...^..    
    """)


@pytest.mark.parametrize("board, intersections",
                         ((Board.from_string(SMALL_MAP), {(2, 2), (2, 4),
                                                          (6, 4), (10, 4)}), ))
def test_get_intersections(board, intersections):
    actual = set(get_intersections(board))
    assert actual == intersections


@pytest.mark.parametrize("intersections, result", (({(2, 2), (2, 4), (6, 4),
                                                     (10, 4)}, 76), ))
def test_aligments(intersections, result):
    actual = calculate_alignments(intersections)
    assert actual == result
Beispiel #12
0
def load(fobj):
    algo_str = fobj.readline().strip()
    fobj.readline()
    board = Board.from_string(fobj.read(), fill_value=".")
    board.grid = np.pad(board.grid, ((9, 9), (9, 9)), mode="constant", constant_values=".")
    return algo_str, board
Beispiel #13
0
def load():
    with open(get_input_name(3, 2021)) as fobj:
        return Board.from_string(fobj.read(), fill_value=0, dtype=np.int_)