diff options
author | Marvin Borner | 2024-12-04 06:41:32 +0100 |
---|---|---|
committer | Marvin Borner | 2024-12-04 06:41:32 +0100 |
commit | 71c541fff3f1375e0549f277be6ac7c5ebd1f138 (patch) | |
tree | 6d9de787bb68b136672514f922583c2692d41a5d | |
parent | 06553af16d4726fa6619b8d4042823da5c484bfa (diff) |
Slightly better, still annoyed
-rwxr-xr-x | 2024/04/solve.py | 65 |
1 files 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) |