Skip to content

Commit 37e3ff4

Browse files
committed
added compact version of 2024/day04
1 parent 47e4130 commit 37e3ff4

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

2024/day04/run-compact.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#! /usr/bin/env python3
2+
3+
import numpy as np
4+
5+
npa = lambda *s: np.array(list(map(list, s)))
6+
swv = np.lib.stride_tricks.sliding_window_view
7+
n_matches = lambda k: np.sum(np.all(np.logical_or(swv(input, k.shape) == k, k == '.'), axis=(2, 3)))
8+
num = lambda k: sum( n_matches(np.rot90(k, i)) for i in range(4) )
9+
s2k = lambda *s: num(npa(*s))
10+
11+
input = npa(*open('input.txt').read().splitlines())
12+
result1 = s2k('XMAS') + s2k('X...', '.M..', '..A.', '...S')
13+
result2 = s2k('M.S', '.A.', 'M.S')
14+
15+
print(result1, result2, sep='\n')

0 commit comments

Comments
 (0)