Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!usr/local/bin/python3.8
- # -*- coding: utf-8 -*import
- def is_valid(x: int):
- global neighborhood_size
- return 0 <= x < neighborhood_size
- def find_santa(matrix: list):
- for r in range(len(matrix)):
- for c in range(len(matrix[r])):
- if matrix[r][c] == 'S':
- return r, c
- def up(r: int, c: int):
- if is_valid(r - 1):
- return r - 1, c
- def down(r: int, c: int):
- if is_valid(r + 1):
- return r + 1, c
- def right(r: int, c: int):
- if is_valid(c + 1):
- return r, c + 1
- def left(r: int, c: int):
- if is_valid(c - 1):
- return r, c - 1
- def manage_cookies(r: int, c: int):
- global no_presents, neighborhood, santa_row, santa_col, presents_count
- rows, cols = (-1, 0, 0, +1), (0, -1, +1, 0)
- for x in range(len(rows)):
- n_r, n_c = r + rows[x], c + cols[x]
- if neighborhood[n_r][n_c] in ('X', 'V'):
- neighborhood[n_r][n_c] = '-'
- presents_count -= 1
- if presents_count == 0:
- no_presents = True
- return
- def manage_cell(r: int, c: int):
- global neighborhood, santa_row, santa_col, presents_count, no_presents
- cell = neighborhood[r][c]
- if cell == '-':
- neighborhood[santa_row][santa_col] = '-'
- santa_row, santa_col = r, c
- neighborhood[r][c] = 'S'
- elif cell == 'X':
- neighborhood[santa_row][santa_col] = '-'
- santa_row, santa_col = r, c
- neighborhood[r][c] = 'S'
- elif cell == 'V':
- presents_count -= 1
- neighborhood[santa_row][santa_col] = '-'
- santa_row, santa_col = r, c
- neighborhood[r][c] = 'S'
- if presents_count == 0:
- no_presents = True
- return
- elif cell == 'C':
- neighborhood[santa_row][santa_col] = '-'
- santa_row, santa_col = r, c
- neighborhood[r][c] = 'S'
- manage_cookies(r, c)
- def find_nice_kids(matrix: list):
- kids = 0
- for x in range(len(matrix)):
- for y in range(len(matrix[x])):
- cell = matrix[x][y]
- if cell == 'V':
- kids += 1
- return kids
- MOVES = {
- 'up': up,
- 'down': down,
- 'right': right,
- 'left': left,
- }
- no_presents = False
- presents_count = int(input())
- neighborhood_size = int(input())
- neighborhood = [input().split() for _ in range(neighborhood_size)]
- santa_row, santa_col = find_santa(neighborhood)
- start_nice_kids = find_nice_kids(neighborhood)
- while True:
- command = input()
- if command == 'Christmas morning':
- break
- new_row, new_col = MOVES[command](santa_row, santa_col)
- manage_cell(new_row, new_col)
- if no_presents:
- break
- nice_kids = find_nice_kids(neighborhood)
- if no_presents and nice_kids > 0:
- print('Santa ran out of presents!')
- [print(*x) for x in neighborhood]
- if nice_kids == 0:
- print(f'Good job, Santa! {start_nice_kids} happy nice kid/s.')
- else:
- print(f'No presents for {nice_kids} nice kid/s.')
Add Comment
Please, Sign In to add comment