-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtetramino.py
More file actions
91 lines (76 loc) · 3.12 KB
/
Copy pathtetramino.py
File metadata and controls
91 lines (76 loc) · 3.12 KB
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
import sys
def read_map_from_user_input() -> str:
counter = 0
map_string: str = ""
print("Please input map 8x8:")
for i in sys.stdin:
map_string += i + "\n"
counter += 1
if counter == 8:
break
return map_string
def read_map_from_file() -> str:
with open("input.txt", "r") as map_file:
map_content: str = map_file.read()
return map_content
def shape_map(map_string: str) -> list[list[str]]:
map_list: list[str] = map_string.split()
if len(map_list) != 8:
sys.stderr.write('Exception! Bad size map!')
exit()
map_2d: list[list[str]] = list()
for string in map_list:
map_2d.append(list(string.replace("\\n", "")))
if len(map_2d[-1]) != 8:
sys.stderr.write('Exception! Bad size line in map!')
exit()
return map_2d
class TetraminoPositionCounter:
def __init__(self):
if "input.txt" in sys.argv:
tetris_map: str = read_map_from_file()
else:
tetris_map: str = read_map_from_user_input()
self.shaped_map: list[list[str]] = shape_map(tetris_map)
self.result: int = 0
def search_space_around(self, line_id: int, place_id: int):
if line_id < 7:
if place_id < 6:
if (self.shaped_map[line_id][place_id + 1] == '.'
and self.shaped_map[line_id][place_id + 2] == '.'
and self.shaped_map[line_id + 1][place_id + 1] == '.'):
self.result += 1
if place_id < 7:
if (self.shaped_map[line_id + 1][place_id - 1] == '.'
and self.shaped_map[line_id + 1][place_id] == '.'
and self.shaped_map[line_id + 1][place_id + 1] == '.'):
self.result += 1
if line_id < 6:
if place_id < 7:
if (self.shaped_map[line_id + 1][place_id] == '.'
and self.shaped_map[line_id + 1][place_id + 1] == '.'
and self.shaped_map[line_id + 2][place_id] == '.'):
self.result += 1
if (self.shaped_map[line_id + 1][place_id - 1] == '.'
and self.shaped_map[line_id + 1][place_id] == '.'
and self.shaped_map[line_id + 2][place_id] == '.'):
self.result += 1
def check_map(self):
for line_id, line in enumerate(self.shaped_map):
for place_id, place in enumerate(line):
if place == '.':
self.search_space_around(line_id, place_id)
elif place == '*':
continue
else:
sys.stderr.write('Exception! Wrong symbol in map!')
exit()
def main(self):
self.check_map()
if "output.txt" in sys.argv:
with open("output.txt", 'w', encoding="utf-8") as file_result:
file_result.write(str(self.result))
else:
sys.stdout.write(str(self.result))
if __name__ == '__main__':
TetraminoPositionCounter().main()