Skip to content

Instantly share code, notes, and snippets.

@andriiburka
Last active June 24, 2020 14:34
Show Gist options
  • Save andriiburka/d6186c8cb7f436d705da95ebfd791a6d to your computer and use it in GitHub Desktop.
Save andriiburka/d6186c8cb7f436d705da95ebfd791a6d to your computer and use it in GitHub Desktop.
def presents_for_all_nearby(cmd):
global presents, good_kids_left
possible_moves = ['up', 'down', 'left', 'right']
if cmd == 'up':
possible_moves.remove('down')
elif cmd == 'down':
possible_moves.remove('up')
elif cmd == 'left':
possible_moves.remove('right')
elif cmd == 'right':
possible_moves.remove('left')
while possible_moves:
direction = possible_moves.pop()
if direction == 'up':
if matrix[santa_pos[0]-1][santa_pos[1]] != '-':
presents -= 1
if matrix[santa_pos[0]-1][santa_pos[1]] == 'V':
good_kids_left -= 1
matrix[santa_pos[0]-1][santa_pos[1]] = '-'
elif direction == 'down':
if matrix[santa_pos[0]+1][santa_pos[1]] != '-':
presents -= 1
if matrix[santa_pos[0]+1][santa_pos[1]] == 'V':
good_kids_left -= 1
matrix[santa_pos[0]+1][santa_pos[1]] = '-'
elif direction == 'left':
if matrix[santa_pos[0]][santa_pos[1]-1] != '-':
presents -= 1
if matrix[santa_pos[0]][santa_pos[1]-1] == 'V':
good_kids_left -= 1
matrix[santa_pos[0]][santa_pos[1]-1] = '-'
elif direction == 'right':
if matrix[santa_pos[0]][santa_pos[1]+1] != '-':
presents -= 1
if matrix[santa_pos[0]][santa_pos[1]+1] == 'V':
good_kids_left -= 1
matrix[santa_pos[0]][santa_pos[1]+1] = '-'
presents = int(input())
matrix = [input().split(' ') for i in range(int(input()))]
move = {'up': {'r': -1, 'c': 0}, 'down': {'r': +1, 'c': 0}, 'left': {'r': 0, 'c': -1}, 'right': {'r': 0, 'c': +1}}
c_pos = []
c_in_matrix = ["C" for row in range(len(matrix)) for col in range(len(matrix[row])) if matrix[row][col] == 'C']
if c_in_matrix:
c_pos = [[row, col] for row in range(len(matrix)) for col in range(len(matrix[row])) if matrix[row][col] == 'C'][0]
santa_pos = [[row, col] for row in range(len(matrix)) for col in range(len(matrix[row])) if matrix[row][col] == 'S'][0]
good_kids_pos = [[row, col] for row in range(len(matrix)) for col in range(len(matrix[row])) if matrix[row][col] == 'V']
all_good_kids = len(good_kids_pos)
good_kids_left = len(good_kids_pos)
while True:
command = input()
if not command or command == 'Christmas morning':
break
if command == 'up':
matrix[santa_pos[0]][santa_pos[1]] = '-'
santa_pos[0] -= 1
matrix[santa_pos[0]][santa_pos[1]] = 'S'
if santa_pos in good_kids_pos:
presents -= 1
good_kids_left -= 1
elif santa_pos == c_pos:
presents_for_all_nearby(cmd=command)
elif command == 'down':
matrix[santa_pos[0]][santa_pos[1]] = '-'
santa_pos[0] += 1
matrix[santa_pos[0]][santa_pos[1]] = 'S'
if santa_pos in good_kids_pos:
presents -= 1
good_kids_left -= 1
elif santa_pos == c_pos:
presents_for_all_nearby(cmd=command)
elif command == 'left':
matrix[santa_pos[0]][santa_pos[1]] = '-'
santa_pos[1] -= 1
matrix[santa_pos[0]][santa_pos[1]] = 'S'
if santa_pos in good_kids_pos:
presents -= 1
good_kids_left -= 1
elif santa_pos == c_pos:
presents_for_all_nearby(cmd=command)
elif command == 'right':
matrix[santa_pos[0]][santa_pos[1]] = '-'
santa_pos[1] += 1
matrix[santa_pos[0]][santa_pos[1]] = 'S'
if santa_pos in good_kids_pos:
presents -= 1
good_kids_left -= 1
elif santa_pos == c_pos:
presents_for_all_nearby(cmd=command)
if not presents:
print("Santa ran out of presents!")
[print(" ".join(i)) for i in matrix]
print(f"No presents for {good_kids_left} nice kid/s."
if good_kids_left else f"Good job, Santa! {all_good_kids} happy nice kid/s.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment