Advertisement
Lewi7tan

ChessFunctional

Mar 19th, 2023 (edited)
753
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.60 KB | Source Code | 0 0
  1. import numpy as np
  2.  
  3.  
  4. # --- read ---
  5. def read_file(filename: str) -> list[str]:
  6.     chessboards = ''
  7.     with open(filename, 'r') as file:
  8.         for line in file:
  9.             chessboards += line[:-1] if len(line) > 1 else ' '
  10.  
  11.     return chessboards[:-1].split(' ')
  12.  
  13.  
  14. def as_matrix(chessboard: str) -> np.array:
  15.     rows = []
  16.     while len(chessboard):
  17.         rows.append(list(chessboard[:8]))
  18.         chessboard = chessboard[8:]
  19.  
  20.     return np.array(rows)
  21.  
  22.  
  23. # --- 1 ---
  24. def empty_column_count(chessboard: np.array) -> int:
  25.     counter = 0
  26.     for column in chessboard.transpose():
  27.         counter += 1 if len(set(column)) == 1 else 0
  28.  
  29.     return counter
  30.  
  31.  
  32. # --- 2 ---
  33. def check_chessboard_balance(chessboard: str) -> int:
  34.     white = sorted(list(filter(lambda chessman: chessman.isupper(), chessboard)))
  35.     black = sorted(list(filter(lambda chessman: chessman.islower(), chessboard)))
  36.     black_as_capitalized = [chessman.upper() for chessman in black]
  37.  
  38.     return len(white * 2) if white == black_as_capitalized else 0
  39.  
  40.  
  41. # --- 3 ---
  42. def check(key):
  43.     def checked(chessboard: np.array):
  44.         return check_side(chessboard, key=key)
  45.  
  46.     return checked
  47.  
  48.  
  49. def check_side(chessboard: np.array, key) -> bool:
  50.     for horizontal in chessboard:
  51.         if check_row(horizontal.tolist(), key):
  52.             return True
  53.  
  54.     for vertical in chessboard.transpose():
  55.         if check_row(vertical.tolist(), key):
  56.             return True
  57.  
  58.     return False
  59.  
  60.  
  61. def check_row(row, key) -> bool:
  62.     king = 'K' if key == 'white' else 'k'
  63.     rook = 'w' if key == 'white' else 'W'
  64.     if king not in row:
  65.         return False
  66.  
  67.     row_chessmen = [chessman for chessman in row if chessman.isalpha()]
  68.  
  69.     king_position = row_chessmen.index(king)
  70.  
  71.     if rook in row_chessmen[king_position - 1:king_position + 1]:
  72.         return True
  73.  
  74.  
  75. # --- read ---
  76. data = read_file('szachy.txt')
  77. chessboards = list(map(as_matrix, data))
  78.  
  79. # --- 1 ---
  80. empty_column_chessboards = list(filter(lambda count: count, map(empty_column_count, chessboards)))
  81.  
  82. print('---1---')
  83. print(len(empty_column_chessboards))
  84. print(max(empty_column_chessboards))
  85.  
  86. # --- 2 ---
  87. balanced_chessboards = list(filter(lambda count: count, map(check_chessboard_balance, data)))
  88.  
  89. print('---2---')
  90. print(len(balanced_chessboards))
  91. print(min(balanced_chessboards))
  92.  
  93. # --- 3 ---
  94. white_checked = check(key="white")
  95. black_checked = check(key="black")
  96.  
  97. black_points = sum(list(map(white_checked, chessboards)))
  98. white_points = sum(list(map(black_checked, chessboards)))
  99.  
  100. print('---3---')
  101. print(black_points)
  102. print(white_points)
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement