04
This commit is contained in:
41
04/04-02.py
Normal file
41
04/04-02.py
Normal file
@@ -0,0 +1,41 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
def count_x(data:list[list[str]], pattern:str) -> int:
|
||||
result = 0
|
||||
for i in range(len(data)):
|
||||
for j in range(len(data[0])):
|
||||
try:
|
||||
if data[i][j] == pattern[0] and data[i+1][j+1] == pattern[1] and data[i+2][j+2] == pattern[2]:
|
||||
try:
|
||||
if data[i+2][j] == pattern[0] and data[i][j+2] == pattern[2]:
|
||||
result += 1
|
||||
except IndexError:
|
||||
pass
|
||||
try:
|
||||
if data[i+2][j] == pattern[2] and data[i][j+2] == pattern[0]:
|
||||
result += 1
|
||||
except IndexError:
|
||||
pass
|
||||
except IndexError:
|
||||
pass
|
||||
return result
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
solution = 0
|
||||
field=[]
|
||||
input_file = open("input.txt", "r")
|
||||
for line in input_file:
|
||||
line = line.strip()
|
||||
if line == "":
|
||||
continue
|
||||
temp = []
|
||||
for b in line:
|
||||
temp.append(b)
|
||||
field.append(temp)
|
||||
input_file.close()
|
||||
|
||||
solution += count_x(field, 'MAS')
|
||||
solution += count_x(field, 'SAM')
|
||||
|
||||
print(f'Solution:{solution}')
|
||||
Reference in New Issue
Block a user