diff options
-rw-r--r-- | aoc/2024/04/part-one.py | 23 | ||||
-rw-r--r-- | aoc/2024/04/part-two.py | 10 |
2 files changed, 33 insertions, 0 deletions
diff --git a/aoc/2024/04/part-one.py b/aoc/2024/04/part-one.py new file mode 100644 index 0000000..ba25336 --- /dev/null +++ b/aoc/2024/04/part-one.py @@ -0,0 +1,23 @@ +from sys import stdin + + +def index(m, n, i, j, c): + return (m[i][j], m[n-j-1][i], m[n-i-1][n-j-1], m[j][n-i-1])[c] + + +def rot(m, c): + n = len(m) + if n != len(m[0]): raise ValueError + for i in range(n): + yield ''.join(index(m, n, i-j, j, c) for j in range(i+1)) + for i in range(1, n): + yield ''.join(index(m, n, n-j-1, i+j, c) for j in range(n-i)) + + +reverse_all = lambda strings: (s[::-1] for s in strings) +count = lambda strings: sum(s.count('XMAS') for s in strings) +lines = tuple(map(str.strip, stdin.readlines())) +columns = tuple(''.join(column) for column in zip(*lines)) +print((count(lines) + count(reverse_all(lines)) + + count(columns) + count(reverse_all(columns)) + + sum(count(rot(lines, c)) for c in range(4)))) diff --git a/aoc/2024/04/part-two.py b/aoc/2024/04/part-two.py new file mode 100644 index 0000000..a3e7c4a --- /dev/null +++ b/aoc/2024/04/part-two.py @@ -0,0 +1,10 @@ +from itertools import islice +from sys import stdin + +mat = tuple(map(str.strip, stdin.readlines())) +m = len(mat) +n = len(mat[0]) +print(sum(c == 'A' and ({mat[i-1][j-1], mat[i+1][j+1]} + == {mat[i-1][j+1], mat[i+1][j-1]} == {'M', 'S'}) + for i, line in islice(enumerate(mat), 1, m-1) + for j, c in islice(enumerate(line), 1, n-1))) |