Advent of Code 2024, Day 4

I lost time because of two blunders:

Insights for simpler code:

Part 1

import sys

def find(s, lines, i, j, diri, dirj):
    if s == "": return True
    if not 0 <= i < len(lines): return False
    if not 0 <= j < len(lines[i]): return False
    if not lines[i][j] == s[0]: return False
    return find(s[1:], lines, i+diri, j+dirj, diri, dirj)

with open(sys.argv[1]) as f:
    lines = f.readlines()

s = 0
for i in range(len(lines)):
    for j in range(len(lines[i])):
        for diri in [-1, 0, 1]:
            for dirj in [-1, 0, 1]:
                s += find("XMAS", lines, i, j, diri, dirj)
print(s)

Part 2

import sys

def find(s, lines, i, j, diri, dirj):
    if s == "": return True
    if not 0 <= i < len(lines): return False
    if not 0 <= j < len(lines[i]): return False
    if not lines[i][j] == s[0]: return False
    return find(s[1:], lines, i+diri, j+dirj, diri, dirj)

with open(sys.argv[1]) as f:
    lines = f.readlines()

s = 0
for i in range(len(lines)):
    for j in range(len(lines[i])):
        n = 0
        for diri in [-1, 1]:
            for dirj in [-1, 1]:
                n += find("MAS", lines, i-diri, j-dirj, diri, dirj)
        if n >= 2:
            s += 1
print(s)

2024-12-11

Proxy Information
Original URL
gemini://dkalak.de/aoc/04.gmi
Status Code
Success (20)
Meta
text/gemini; lang=en
Capsule Response Time
115.735314 milliseconds
Gemini-to-HTML Time
0.439158 milliseconds

This content has been proxied by September (ba2dc).