80 lines
1.4 KiB
Python
80 lines
1.4 KiB
Python
#!/usr/bin/env python3
|
|
|
|
# N
|
|
# NW NO
|
|
# W X O
|
|
# SW SO
|
|
# S
|
|
|
|
# 1
|
|
x = list("XMAS")
|
|
|
|
h = list()
|
|
with open('day04') as f:
|
|
for l in f:
|
|
h.append([*l.strip()])
|
|
|
|
def idx(i,j,k):
|
|
global h,x
|
|
if i<0 or j<0 or i>=len(h) or j>=len(h[0]):
|
|
return False
|
|
if h[i][j] != x[k]:
|
|
return False
|
|
return True
|
|
|
|
def fmas(i,j):
|
|
n=o=s=w=1
|
|
no=so=sw=nw=1
|
|
|
|
for k in range(4):
|
|
if not idx(i,j+k, k): o = 0
|
|
if not idx(i,j-k, k): w = 0
|
|
if not idx(i+k,j,k): s = 0
|
|
if not idx(i-k,j,k): n = 0
|
|
|
|
if not idx(i-k,j+k,k): no = 0
|
|
if not idx(i+k,j-k,k): sw = 0
|
|
if not idx(i-k,j-k,k): nw = 0
|
|
if not idx(i+k,j+k,k): so = 0
|
|
|
|
return n+o+s+w+no+so+sw+nw
|
|
|
|
res = 0
|
|
for i in range(len(h)):
|
|
for j in range(len(h[0])):
|
|
if h[i][j] == 'X':
|
|
res += fmas(i,j)
|
|
|
|
# 2
|
|
def idx2(i,j):
|
|
global h,x
|
|
if i<0 or j<0 or i>=len(h) or j>=len(h[0]):
|
|
return False
|
|
if h[i][j] == 'S':
|
|
return 'S'
|
|
if h[i][j] == 'M':
|
|
return 'M'
|
|
return False
|
|
|
|
def fmas2(i,j):
|
|
no = idx2(i-1,j+1)
|
|
sw = idx2(i+1,j-1)
|
|
nw = idx2(i-1,j-1)
|
|
so = idx2(i+1,j+1)
|
|
|
|
if not no: return 0
|
|
if not sw: return 0
|
|
if not nw: return 0
|
|
if not so: return 0
|
|
|
|
if sw == no: return 0
|
|
if nw == so: return 0
|
|
|
|
return 1
|
|
|
|
|
|
res2 = 0
|
|
for i in range(len(h)):
|
|
for j in range(len(h[0])):
|
|
if h[i][j] == 'A':
|
|
res2 += fmas2(i,j)
|