-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4.1.py
101 lines (83 loc) · 3.45 KB
/
4.1.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from typing import List
# row -> text[]
# column -> text[0][]
text = """
MMMSXXMASM
MSAMXMSMSA
AMXSXMAAMM
MSAMASMSMX
XMASAMXAMM
XXAMMXXAMA
SMSMSASXSS
SAXAMASAAA
MAMMMXMMMM
MXMXAXMASX
""".split("\n")
text = [list(i) for i in text if i]
def safeCheck(text: List[List[int]], row: int, column: int):
total = 0
try:
# horizontal right
if (0 <= row < len(text) and 0 <= column < len(text[0]) and 0 <= column+4 < len(text[0])):
if text[row][column:column+4] == list("XMAS"):
print("1ST CONDITION", text[row][column:column+4])
total += 1
# horizontal left
if (0 <= row < len(text) and 0 <= column < len(text[0]) and 0 <= column-4 < len(text[0])):
if text[row][column-3:column+1] == list("XMAS")[::-1]:
print("2ND CONDITION", text[row][column-3:column+1])
total += 1
#vertical down
if (0 <= row < len(text) and 0 <= row+4 < len(text) and 0 <= column-4 < len(text[0])):
if [i[column] for i in text[row:row+4]] == list("XMAS"):
print("3RD CONDITION", [i[column] for i in text[row:row+4]])
total += 1
#vertical up
if (0 <= row < len(text) and 0 <= row-4 < len(text) and 0 <= column-4 < len(text[0])):
if [i[column] for i in text[row-3:row+1]] == list("XMAS")[::-1]:
print("4TH CONDITION", [i[column] for i in text[row-3:row+1]])
total += 1
#diagonal left top
if (0 <= row < len(text) and 0 <= row-4 < len(text) and 0 <= column < len(text[0]) and 0 <= column-4 < len(text[0])):
if [text[row+i][column+i] for i in range(-3, 1)] == list("XMAS")[::-1]:
print("5TH CONDITION", [text[row+i][column+i] for i in range(-3, 1)])
total += 1
#diagonal left bottom
if (0 <= row < len(text) and 0 <= row+4 < len(text) and 0 <= column < len(text[0]) and 0 <= column-4 < len(text[0])):
if [text[row-i][column+i] for i in range(-3, 1)] == list("XMAS")[::-1]:
print("6TH CONDITION", [text[row-i][column+i] for i in range(-3, 1)])
total += 1
#diagonal right top
if (0 <= row < len(text) and 0 <= row-4 < len(text) and 0 <= column < len(text[0]) and 0 <= column+4 < len(text[0])):
if [text[row+i][column-i] for i in range(-3, 1)] == list("XMAS"):
print("7TH CONDITION", [text[row+i][column-i] for i in range(-3, 1)])
total += 1
#diagonal right bottom
if (0 <= row < len(text) and 0 <= row+4 < len(text) and 0 <= column < len(text[0]) and 0 <= column+4 < len(text[0])):
if [text[row+i][column+i] for i in range(-3, 1)] == list("XMAS"):
print("8TH CONDITION", [text[row+i][column+i] for i in range(-3, 1)])
total += 1
except Exception as e:
print(row, row-4, row+4, column)
raise e
return total
a = []
total = 0
for i in range(len(text)):
print(i, "|", end=" ")
a.append([])
for j in range(len(text[0])):
a[i].append(safeCheck(text, row=i, column=j) if text[i][j] == 'X' else 0)
total += a[i][j]
print()
print()
li = ["".join(i) for i in text]
for index, element in enumerate(li):
print(index, "|", element)
print()
for i in range(len(text)):
print(i, "|", end=" ")
for j in range(len(text[0])):
print(a[i][j], end="")
print()
print(total)