I lost time because of two blunders:
Insights for simpler code:
import sys def find(s, lines, i, j, diri, dirj): if s == "": return True if not 0 <= i < len(lines): return False if not 0 <= j < len(lines[i]): return False if not lines[i][j] == s[0]: return False return find(s[1:], lines, i+diri, j+dirj, diri, dirj) with open(sys.argv[1]) as f: lines = f.readlines() s = 0 for i in range(len(lines)): for j in range(len(lines[i])): for diri in [-1, 0, 1]: for dirj in [-1, 0, 1]: s += find("XMAS", lines, i, j, diri, dirj) print(s)
import sys def find(s, lines, i, j, diri, dirj): if s == "": return True if not 0 <= i < len(lines): return False if not 0 <= j < len(lines[i]): return False if not lines[i][j] == s[0]: return False return find(s[1:], lines, i+diri, j+dirj, diri, dirj) with open(sys.argv[1]) as f: lines = f.readlines() s = 0 for i in range(len(lines)): for j in range(len(lines[i])): n = 0 for diri in [-1, 1]: for dirj in [-1, 1]: n += find("MAS", lines, i-diri, j-dirj, diri, dirj) if n >= 2: s += 1 print(s)
2024-12-11
text/gemini; lang=en
This content has been proxied by September (ba2dc).