Advertisement
simeonshopov

Santa - грешен 7ми тест

Jun 23rd, 2020
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.11 KB | None | 0 0
  1. #!usr/local/bin/python3.8
  2. # -*- coding: utf-8 -*import
  3.  
  4.  
  5. def is_valid(x: int):
  6.     global neighborhood_size
  7.     return 0 <= x < neighborhood_size
  8.  
  9.  
  10. def find_santa(matrix: list):
  11.     for r in range(len(matrix)):
  12.         for c in range(len(matrix[r])):
  13.             if matrix[r][c] == 'S':
  14.                 return r, c
  15.  
  16.  
  17. def up(r: int, c: int):
  18.     if is_valid(r - 1):
  19.         return r - 1, c
  20.  
  21.  
  22. def down(r: int, c: int):
  23.     if is_valid(r + 1):
  24.         return r + 1, c
  25.  
  26.  
  27. def right(r: int, c: int):
  28.     if is_valid(c + 1):
  29.         return r, c + 1
  30.  
  31.  
  32. def left(r: int, c: int):
  33.     if is_valid(c - 1):
  34.         return r, c - 1
  35.  
  36.  
  37. def manage_cookies(r: int, c: int):
  38.     global no_presents, neighborhood, santa_row, santa_col, presents_count
  39.     rows, cols = (-1, 0, 0, +1), (0, -1, +1, 0)
  40.     for x in range(len(rows)):
  41.         n_r, n_c = r + rows[x], c + cols[x]
  42.         if neighborhood[n_r][n_c] in ('X', 'V'):
  43.             neighborhood[n_r][n_c] = '-'
  44.             presents_count -= 1
  45.             if presents_count == 0:
  46.                 no_presents = True
  47.                 return
  48.  
  49.  
  50. def manage_cell(r: int, c: int):
  51.     global neighborhood, santa_row, santa_col, presents_count, no_presents
  52.  
  53.     if not is_valid(r) or not is_valid(c):
  54.         no_presents = True
  55.         return
  56.     cell = neighborhood[r][c]
  57.     if cell == '-':
  58.         neighborhood[santa_row][santa_col] = '-'
  59.         santa_row, santa_col = r, c
  60.         neighborhood[r][c] = 'S'
  61.     elif cell == 'X':
  62.         neighborhood[santa_row][santa_col] = '-'
  63.         santa_row, santa_col = r, c
  64.         neighborhood[r][c] = 'S'
  65.     elif cell == 'V':
  66.         presents_count -= 1
  67.         neighborhood[santa_row][santa_col] = '-'
  68.         santa_row, santa_col = r, c
  69.         neighborhood[r][c] = 'S'
  70.         if presents_count == 0:
  71.             no_presents = True
  72.             return
  73.     elif cell == 'C':
  74.         neighborhood[santa_row][santa_col] = '-'
  75.         santa_row, santa_col = r, c
  76.         neighborhood[r][c] = 'S'
  77.         manage_cookies(r, c)
  78.  
  79.  
  80. def find_nice_kids(matrix: list):
  81.     kids = 0
  82.     for x in range(len(matrix)):
  83.         for y in range(len(matrix[x])):
  84.             cell = matrix[x][y]
  85.             if cell == 'V':
  86.                 kids += 1
  87.     return kids
  88.  
  89.  
  90. MOVES = {
  91.     'up': up,
  92.     'down': down,
  93.     'right': right,
  94.     'left': left,
  95. }
  96.  
  97.  
  98. no_presents = False
  99. presents_count = int(input())
  100. neighborhood_size = int(input())
  101.  
  102. neighborhood = [input().split() for _ in range(neighborhood_size)]
  103. santa_row, santa_col = find_santa(neighborhood)
  104. start_nice_kids = find_nice_kids(neighborhood)
  105.  
  106. while True:
  107.     command = input()
  108.     if command == 'Christmas morning':
  109.         break
  110.     new_row, new_col = MOVES[command](santa_row, santa_col)
  111.     manage_cell(new_row, new_col)
  112.     if no_presents:
  113.         break
  114.  
  115. nice_kids = find_nice_kids(neighborhood)
  116. if no_presents and nice_kids > 0:
  117.     print('Santa ran out of presents!')
  118. [print(*x) for x in neighborhood]
  119. if nice_kids == 0:
  120.     print(f'Good job, Santa! {start_nice_kids} happy nice kid/s.')
  121. else:
  122.     print(f'No presents for {nice_kids} nice kid/s.')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement