simeonshopov

Present delivery

Jun 25th, 2020
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.02 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.     cell = neighborhood[r][c]
  53.     if cell == '-':
  54.         neighborhood[santa_row][santa_col] = '-'
  55.         santa_row, santa_col = r, c
  56.         neighborhood[r][c] = 'S'
  57.     elif cell == 'X':
  58.         neighborhood[santa_row][santa_col] = '-'
  59.         santa_row, santa_col = r, c
  60.         neighborhood[r][c] = 'S'
  61.     elif cell == 'V':
  62.         presents_count -= 1
  63.         neighborhood[santa_row][santa_col] = '-'
  64.         santa_row, santa_col = r, c
  65.         neighborhood[r][c] = 'S'
  66.         if presents_count == 0:
  67.             no_presents = True
  68.             return
  69.     elif cell == 'C':
  70.         neighborhood[santa_row][santa_col] = '-'
  71.         santa_row, santa_col = r, c
  72.         neighborhood[r][c] = 'S'
  73.         manage_cookies(r, c)
  74.  
  75.  
  76. def find_nice_kids(matrix: list):
  77.     kids = 0
  78.     for x in range(len(matrix)):
  79.         for y in range(len(matrix[x])):
  80.             cell = matrix[x][y]
  81.             if cell == 'V':
  82.                 kids += 1
  83.     return kids
  84.  
  85.  
  86. MOVES = {
  87.     'up': up,
  88.     'down': down,
  89.     'right': right,
  90.     'left': left,
  91. }
  92.  
  93.  
  94. no_presents = False
  95. presents_count = int(input())
  96. neighborhood_size = int(input())
  97.  
  98. neighborhood = [input().split() for _ in range(neighborhood_size)]
  99. santa_row, santa_col = find_santa(neighborhood)
  100. start_nice_kids = find_nice_kids(neighborhood)
  101.  
  102. while True:
  103.     command = input()
  104.     if command == 'Christmas morning':
  105.         break
  106.     new_row, new_col = MOVES[command](santa_row, santa_col)
  107.     manage_cell(new_row, new_col)
  108.     if no_presents:
  109.         break
  110.  
  111. nice_kids = find_nice_kids(neighborhood)
  112. if no_presents and nice_kids > 0:
  113.     print('Santa ran out of presents!')
  114. [print(*x) for x in neighborhood]
  115. if nice_kids == 0:
  116.     print(f'Good job, Santa! {start_nice_kids} happy nice kid/s.')
  117. else:
  118.     print(f'No presents for {nice_kids} nice kid/s.')
Add Comment
Please, Sign In to add comment