@@ -94,14 +94,27 @@ def __str__(self) -> str:
9494 def load_from_str (self , map_grid : str ):
9595 """
9696 Load the map from a string.
97- Use space for line breaks and # for alive cells.
97+ Use space for end of row, . for dead cells and # for alive cells.
9898 """
9999 rows = map_grid .split (" " )
100+ self .load_from_rows (rows )
101+
102+ def load_from_file (self , file_path : str ):
103+ """
104+ Load the map from a file.
105+ Use line breaks for end of row, . for dead cells and # for alive cells.
106+ """
107+ with open (file_path ) as file :
108+ map_grid = file .readlines ()
109+ self .load_from_rows (map_grid )
110+
111+ def load_from_rows (self , rows : list [str ]):
100112 self .number_of_rows = len (rows )
101- self .number_of_columns = len (rows [0 ])
113+ self .number_of_columns = len (rows [0 ]. strip () )
102114 self .map = [[False for _ in range (self .number_of_columns )] for _ in range (self .number_of_rows )]
103115 for y , line in enumerate (rows ):
104- for x , char in enumerate (line ):
116+ for x , char in enumerate (line .strip ()):
117+ print (x , y , char , line )
105118 if char == "#" :
106119 self .map [y ][x ] = True
107120 elif char != "." :
0 commit comments