Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def next_move(p_r, p_c, direction_, steps_):
- new_p_r, new_p_c = p_r, p_c
- if direction_ == 'up':
- new_p_r -= steps_
- elif direction_ == 'down':
- new_p_r += steps_
- elif direction_ == "left":
- new_p_c -= steps_
- elif direction_ == "right":
- new_p_c += steps_
- return new_p_r, new_p_c
- def valid_idx(r, c, size_):
- return 0 <= r < size_ and 0 <= c < size_
- presents = int(input())
- size = int(input())
- matrix = []
- total_good_kids = 0
- total_delivered_to_good_kids = 0
- santa_row, santa_col = None, None
- for row in range(size):
- current_row = input().split()
- matrix.append(current_row)
- for col in range(size):
- if matrix[row][col] == "S":
- santa_row, santa_col = row, col
- elif matrix[row][col] == "V":
- total_good_kids += 1
- while True:
- direction = input()
- if direction == 'Christmas morning':
- break
- matrix[santa_row][santa_col] = '-'
- next_row, next_col = next_move(santa_row, santa_col, direction, 1)
- if not valid_idx(next_row, next_col, size):
- continue
- if matrix[next_row][next_col] == 'V':
- total_delivered_to_good_kids += 1
- presents -= 1
- elif matrix[next_row][next_col] == 'C':
- if matrix[next_row][next_col-1] not in ['-', 'C'] and presents:
- presents -= 1
- if matrix[next_row][next_col - 1] == 'V':
- total_delivered_to_good_kids += 1
- matrix[next_row][next_col - 1] = '-'
- if matrix[next_row][next_col + 1] not in ['-', 'C'] and presents:
- presents -= 1
- if matrix[next_row][next_col + 1] == 'V':
- total_delivered_to_good_kids += 1
- matrix[next_row][next_col + 1] = '-'
- if matrix[next_row - 1][next_col] not in ['-', 'C'] and presents:
- presents -= 1
- if matrix[next_row - 1][next_col] == 'V':
- total_delivered_to_good_kids += 1
- matrix[next_row - 1][next_col] = '-'
- if matrix[next_row + 1][next_col] not in ['-', 'C'] and presents:
- presents -= 1
- if matrix[next_row + 1][next_col] == 'V':
- total_delivered_to_good_kids += 1
- matrix[next_row + 1][next_col] = '-'
- santa_row, santa_col = next_row, next_col
- matrix[santa_row][santa_col] = 'S'
- if not presents:
- break
- if not presents and total_delivered_to_good_kids < total_good_kids:
- print("Santa ran out of presents!")
- for row in matrix:
- print(*row)
- if total_good_kids == total_delivered_to_good_kids:
- print(f"Good job, Santa! {total_good_kids} happy nice kid/s.")
- else:
- print(f"No presents for {total_good_kids - total_delivered_to_good_kids} nice kid/s.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement