def test_toboggan_run(self): input_file = 'day03-test.txt' matrix = read_matrix(input_file, str) t = Toboggan(matrix) t.run(3, 1) self.assertEqual(len(t.collisions), 7)
def part1(f: str) -> int: seat_matrix = common.read_matrix(f, str) count: int = 0 while True: previous_matrix = copy.deepcopy(seat_matrix) seat_matrix = do_round(seat_matrix) if common.matrix_equal(previous_matrix, seat_matrix): break count += 1 print(f'Rounds: {count}') ret: int = common.count_in_matrix(seat_matrix, SeatType.OCCUPIED) return ret
x = self.cur_pos_x % self.matrix_width return self.matrix[self.cur_pos_y][x] def run(self, slope_x: int, slope_y: int): while self.cur_pos_y < self.matrix_height - 1: self.increment_position(slope_x, slope_y) if self.check_position_value() == '#': self.collisions.append((self.cur_pos_x, self.cur_pos_y)) def count_collisions(self) -> int: return len(self.collisions) if __name__ == "__main__": input_file = 'day03.txt' matrix = read_matrix(input_file, str) # Part 1 t = Toboggan(matrix) t.run(3, 1) print(f'Answer 1: {t.count_collisions()}') # Part 2 slopes = [(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)] num_collisions = [] for slope in slopes: t = Toboggan(matrix) t.run(slope[0], slope[1]) num_collisions.append(t.count_collisions())
return count def do_round(self): new_coordinates = self.coordinates.copy() # overriding the iterator here to try and not have the defaultdict not change the size during run for coordinate, state in tuple(self.coordinates.items()): active_neighbors: int = self.check_neighbors(coordinate) if state == ACTIVE and 2 <= active_neighbors <= 3: # cube remains active continue elif state == ACTIVE and not 2 <= active_neighbors <= 3: new_coordinates[coordinate] = INACTIVE elif state == INACTIVE and active_neighbors == 2: new_coordinates[coordinate] = ACTIVE self.coordinates = new_coordinates if __name__ == '__main__': puzzle_input = common.read_matrix('day17-test.txt', str) cube = ConwayCube(puzzle_input) for turn in range(0, 7): cube.do_round() print('Active: ', cube.count_type(ACTIVE)) print('Inactive: ', cube.count_type(INACTIVE)) print('New: ', cube.count_type(NEWCOOR))