From 71c541fff3f1375e0549f277be6ac7c5ebd1f138 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Wed, 4 Dec 2024 06:41:32 +0100 Subject: Slightly better, still annoyed --- 2024/04/solve.py | 65 +++++++++++++++----------------------------------------- 1 file changed, 17 insertions(+), 48 deletions(-) diff --git a/2024/04/solve.py b/2024/04/solve.py index 77c9419..65b9050 100755 --- a/2024/04/solve.py +++ b/2024/04/solve.py @@ -12,81 +12,50 @@ def get(l, idx): return l[idx] +def scan(what, y, x, yshift, xshift): + return ( + get(get(L, y), x) == what[0] + and get(get(L, y + 1 * yshift), x + 1 * xshift) == what[1] + and get(get(L, y + 2 * yshift), x + 2 * xshift) == what[2] + and get(get(L, y + 3 * yshift), x + 3 * xshift) == what[3] + ) + + def part1(): res = 0 for y in range(len(L)): for x in range(len(L[y])): # horizontal - if ( - get(get(L, y), x) == "X" - and get(get(L, y), x + 1) == "M" - and get(get(L, y), x + 2) == "A" - and get(get(L, y), x + 3) == "S" - ): + if scan("XMAS", y, x, 0, 1): res += 1 # horizontal inversed - if ( - get(get(L, y), x) == "S" - and get(get(L, y), x + 1) == "A" - and get(get(L, y), x + 2) == "M" - and get(get(L, y), x + 3) == "X" - ): + if scan("SAMX", y, x, 0, 1): res += 1 # diagonal right - if ( - get(get(L, y), x) == "X" - and get(get(L, y + 1), x + 1) == "M" - and get(get(L, y + 2), x + 2) == "A" - and get(get(L, y + 3), x + 3) == "S" - ): + if scan("XMAS", y, x, 1, 1): res += 1 # diagonal right inversed - if ( - get(get(L, y), x) == "S" - and get(get(L, y + 1), x + 1) == "A" - and get(get(L, y + 2), x + 2) == "M" - and get(get(L, y + 3), x + 3) == "X" - ): + if scan("SAMX", y, x, 1, 1): res += 1 # vertical - if ( - get(get(L, y), x) == "X" - and get(get(L, y + 1), x) == "M" - and get(get(L, y + 2), x) == "A" - and get(get(L, y + 3), x) == "S" - ): + if scan("XMAS", y, x, 1, 0): res += 1 # vertical inversed - if ( - get(get(L, y), x) == "S" - and get(get(L, y + 1), x) == "A" - and get(get(L, y + 2), x) == "M" - and get(get(L, y + 3), x) == "X" - ): + if scan("SAMX", y, x, 1, 0): res += 1 # diagonal left - if ( - get(get(L, y), x) == "X" - and get(get(L, y + 1), x - 1) == "M" - and get(get(L, y + 2), x - 2) == "A" - and get(get(L, y + 3), x - 3) == "S" - ): + if scan("XMAS", y, x, 1, -1): res += 1 # diagonal left inversed - if ( - get(get(L, y), x) == "S" - and get(get(L, y + 1), x - 1) == "A" - and get(get(L, y + 2), x - 2) == "M" - and get(get(L, y + 3), x - 3) == "X" - ): + if scan("SAMX", y, x, 1, -1): res += 1 print(res) -- cgit v1.2.3