-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfour.py
145 lines (135 loc) · 4.29 KB
/
four.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
eg = """MMMSXXMASM
MSAMXMSMSA
AMXSXMAAMM
MSAMASMSMX
XMASAMXAMM
XXAMMXXAMA
SMSMSASXSS
SAXAMASAAA
MAMMMXMMMM
MXMXAXMASX
"""
def north(word, i):
return i >= (len(word)-1)
def south(word, i, x):
return i + len(word) <= len(x)
def east(word, j, y):
return j + len(word) <= len(y)
def west(word, j):
return j - (len(word)-1) >= 0
def one(source):
word = "XMAS"
count = 0
x = source.splitlines()
for i in range(0, len(x)):
y = x[i]
for j in range(0, len(y)):
if y[j] != 'X':
continue
# check east
if east(word, j, y):
match = True
for k in range(0, len(word)):
if y[j+k] != word[k]:
match = False
break
if match:
#print(f"found east at ({i},{j})")
count += 1
# check west
if west(word, j):
match = True
for k in range(0, len(word)):
if y[j-k] != word[k]:
match = False
break
if match:
#print(f"found west at ({i},{j})")
count += 1
# check north
if north(word, i):
match = True
for k in range(0, len(word)):
if x[i-k][j] != word[k]:
match = False
break
if match:
#print(f"found north at ({i},{j})")
count += 1
# check south
if south(word, i, x):
match = True
for k in range(0, len(word)):
if x[i+k][j] != word[k]:
match = False
break
if match:
#print(f"found south at ({i},{j})")
count += 1
# check north west
if north(word, i) and west(word, j):
match = True
for k in range(0, len(word)):
if x[i-k][j-k] != word[k]:
match = False
break
if match:
#print(f"found north west at ({i},{j})")
count += 1
# check north east
if north(word, i) and east(word, j, y):
match = True
for k in range(0, len(word)):
if x[i-k][j+k] != word[k]:
match = False
break
if match:
#print(f"found north east at ({i},{j})")
count += 1
# check south east
if south(word, i, x) and east(word, j, y):
match = True
for k in range(0, len(word)):
if x[i+k][j+k] != word[k]:
match = False
break
if match:
#print(f"found south east at ({i},{j})")
count += 1
# check south west
if south(word, i, x) and west(word, j):
match = True
for k in range(0, len(word)):
if x[i+k][j-k] != word[k]:
match = False
break
if match:
#print(f"found south west at ({i},{j})")
count += 1
print(f"found {count}")
one(eg)
print("---")
with open("four.txt", "r") as file:
one(file.read())
print("---")
def two(source):
word = "MAS"
count = 0
x = source.splitlines()
for i in range(0, len(x)):
y = x[i]
for j in range(0, len(y)):
if y[j] != 'A' or i == 0 or j == 0 or i == len(x)-1 or j == len(y)-1:
continue
nw = x[i-1][j-1] + x[i][j] + x[i+1][j+1]
ne = x[i-1][j+1] + x[i][j] + x[i+1][j-1]
sw = x[i+1][j-1] + x[i][j] + x[i-1][j+1]
se = x[i+1][j+1] + x[i][j] + x[i-1][j-1]
if [nw, ne, sw, se].count(word) == 2:
#print(f"found X-MAS: {nw} {ne} {sw} {se}")
count += 1
print(f"found {count}")
two(eg)
print("---")
with open("four.txt", "r") as file:
two(file.read())