Ejemplo n.º 1
0
 def __init__(self, file_name):
     self.base_grid = Grid.from_str(Input(file_name).lines())
     biodiversity = 0
     for col in range(5):
         for row in range(5):
             if self.base_grid[col, row] == '#':
                 index = 5 * row + col
                 biodiversity |= 2**index
     self.with_biodiversity = GridWithBiodiversity(self.base_grid, biodiversity)
Ejemplo n.º 2
0
    def __init__(self, file_name):
        lines = Input(file_name).lines()

        self.grid: Grid[str] = Grid.from_str(lines)

        self.asteroids = self.grid.find('#')
        self.monitoring_station = None

        most_asteroids = 0
        for asteroid in self.asteroids:
            asteroid_visible_count = len(self._asteroids_visible_from(asteroid))
            if asteroid_visible_count > most_asteroids:
                most_asteroids = asteroid_visible_count
                self.monitoring_station = asteroid
Ejemplo n.º 3
0
    def part1(self):
        self.computer.reset()
        self.computer.run()
        map_string = self.computer.output_str()
        grid = Grid.from_str(map_string)

        result = 0
        scaffolding = grid.find('#')

        for scaffold in scaffolding:
            intersecting = all(grid[neighbor] == '#'
                               for neighbor in scaffold.neighbors())

            if intersecting:
                result += scaffold.x * scaffold.y

        print("Part 1:", result)
Ejemplo n.º 4
0
    def part2(self):
        self.computer.reset()
        self.computer.ram[0] = 2
        self.computer.run()

        self.computer.output_str()
        self.computer.input_str("B,C,B,C,A,B,C,A,B,A\n")
        self.computer.output_str()
        self.computer.input_str("R,6,L,8,L,10,R,6\n")
        self.computer.output_str()
        self.computer.input_str("R,6,L,6,L,10\n")
        self.computer.output_str()
        self.computer.input_str("L,8,L,6,L,10,L,6\n")
        self.computer.output_str()
        self.computer.input_str("n\n")
        map_string = self.computer.output_str()
        grid = Grid.from_str(map_string)
        result = self.computer.output()

        print("Part 2:", result)
Ejemplo n.º 5
0
    def __init__(self, file_name):
        lines = Input(file_name).lines()
        self.grid: Grid[str] = Grid(50, 6)
        self.grid.fill(' ')

        for line in lines:
            if (matched := re.match(r'rect (\d+)x(\d+)', line)) is not None:
                columns = int(matched.group(1))
                rows = int(matched.group(2))
                for row in range(rows):
                    for col in range(columns):
                        self.grid[col, row] = '#'
            elif (matched := re.match(r'rotate row y=(\d+) by (\d+)',
                                      line)) is not None:
                row_index = int(matched.group(1))
                amount = int(matched.group(2))
                row = []
                for i in range(self.grid.width):
                    row.append(self.grid[i, row_index])

                for i in range(self.grid.width):
                    self.grid[(amount + i) % self.grid.width,
                              row_index] = row[i]
Ejemplo n.º 6
0
 def __init__(self, file_name):
     self.grid = Grid.from_str(Input(file_name).lines())
Ejemplo n.º 7
0
 def __init__(self, file_name):
     groups = Input(file_name).grouped()
     self._enhancement = groups[0][0]
     self._grid = Grid.from_str(groups[1])
Ejemplo n.º 8
0
 def grid(self) -> Grid[str]:
     return Grid.from_str(self.lines())
Ejemplo n.º 9
0
 def __init__(self, file_name):
     lines = Input(file_name).lines()
     self.grid = Grid.from_str(lines)
     self._fill_tunnels()
Ejemplo n.º 10
0
 def __init__(self, file_name):
     self.grid = Grid.from_str(Input(file_name).lines())
     self.portals = self._get_portals()