-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4_1.py
49 lines (40 loc) · 1.98 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
search = 'XMAS'
length = len(search)
with open("input/4_1.txt") as f:
lines = f.read().splitlines()
line_length = len(lines[0])
line_count = len(lines)
count = 0
for line_num, line in enumerate(lines):
for letter_pos, letter in enumerate(line):
if letter != 'X':
continue
if line_num >= length - 1:
# perform upward search
if search == ''.join(lines[line_num - i][letter_pos] for i in range(length)):
count += 1
# perform upward left search
if letter_pos >= length - 1:
if search == ''.join(lines[line_num - i][letter_pos - i] for i in range(length)):
count += 1
# perform upward right search
if letter_pos + length <= line_length:
if search == ''.join(lines[line_num - i][letter_pos + i] for i in range(length)):
count += 1
if line_num + length <= line_count:
# perform downward search
if search == ''.join(lines[line_num + i][letter_pos] for i in range(length)):
count += 1
# perform downward left search
if letter_pos >= length - 1:
if search == ''.join(lines[line_num + i][letter_pos - i] for i in range(length)):
count += 1
# perform downward right search
if letter_pos + length <= line_length:
if search == ''.join(lines[line_num + i][letter_pos + i] for i in range(length)):
count += 1
# perform right search
count += 1 if letter_pos + length <= line_length and line[letter_pos:letter_pos + length] == search else 0
# perform left search
count += 1 if letter_pos - length + 1 >= 0 and search == ''.join(line[letter_pos - i] for i in range(length)) else 0
print(count)