From 1153fac5fef9b3b8b1ce75e969b9b416fe38e6d9 Mon Sep 17 00:00:00 2001 From: setop Date: Wed, 4 Dec 2024 10:11:37 +0100 Subject: [PATCH] day 4, basic grid processing --- d04/part1.py | 26 ++++++++++++++++++++++++++ d04/part2.py | 16 ++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 d04/part1.py create mode 100644 d04/part2.py diff --git a/d04/part1.py b/d04/part1.py new file mode 100644 index 0000000..5c1e9a8 --- /dev/null +++ b/d04/part1.py @@ -0,0 +1,26 @@ +import sys, os + +L = sys.stdin.read().strip().split("\n") +G = [list(l) for l in L] +H = len(G) +W = len(G[0]) + +M4 = [(0,1),(0,-1),(1,0),(-1,0)] +D4 = [(1,1),(-1,-1),(1,-1),(-1,1)] +M8 = M4 + D4 + +S = 0 +for i in range(H): # rows + for j in range(W): # cols + if G[i][j] == "X": + for (dx,dy) in M8: + xi = i + yj = j + for l in 'MAS': + xi += dx + yj += dy + if xi<0 or xi>=H or yj<0 or yj>=W or G[xi][yj]!=l: + break + else: # if the inner loop wasn't broken. + S += 1 +print(S) diff --git a/d04/part2.py b/d04/part2.py new file mode 100644 index 0000000..cec036e --- /dev/null +++ b/d04/part2.py @@ -0,0 +1,16 @@ +import sys, os + +L = sys.stdin.read().strip().split("\n") +G = [list(l) for l in L] +H = len(G) +W = len(G[0]) + +S = 0 +for i in range(1,H-1): # rows + for j in range(1, W-1): # cols + if G[i][j] == "A": + for P in ["MMSS", "SMMS", "SSMM", "MSSM"]: + if G[i-1][j-1] == P[0] and G[i-1][j+1] == P[1] \ + and G[i+1][j+1] == P[2] and G[i+1][j-1] == P[3]: + S+=1 +print(S)