-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.py
45 lines (40 loc) · 1.29 KB
/
Solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Daniel Delijani
# Solution to DEATH BY PROBLEM
import copy
from itertools import combinations
def nonbrokenseats(seats):
ans = []
for r in range(len(seats)):
for c in range(len(seats[0])):
if (seats[r][c] == "."):
ans += [r * len(seats[0]) + c]
return ans
def valid(chosenseats, seats):
seats = copy.deepcopy(seats)
for seat in chosenseats:
row = seat // len(seats[0])
col = seat % len(seats[0])
seats[row][col] = "T"
if not validseat(row, col, seats):
return False
return True
def validseat(row, col, seats):
if col - 1 >= 0:
if seats[row][col-1] == "T":
return False
if row - 1 >= 0 and col - 1 >= 0:
if seats[row-1][col-1] == "T":
return False
if row - 1 >= 0 and col + 1 <= len(seats[0]) - 1:
if seats[row-1][col+1] == "T":
return False
if col + 1 <= len(seats[0]) - 1:
if seats[row][col+1] == "T":
return False
return True
def maxstudents(seats):
goodseats = nonbrokenseats(seats)
for numstudents in range(len(goodseats), 0, -1):
for combo in combinations(goodseats, numstudents):
if valid(combo, seats):
return numstudents