For the read-in, I used a trick from previous days: instead of keeping state in separate variables and changing it when encountering an empty line or the end of the file, I make the state depend on the modulus of the line number.
import sys W, H = 5, 5 locks = [] keys = [] with open(sys.argv[1]) as f: for i, line in enumerate(f): line = line.strip() if i % (H+3) == 0: pattern = W * [0] elif 1 <= i % (H+3) <= H: for j, c in enumerate(line): pattern[j] += c == "#" elif i % (H+3) == (H+1): (locks, keys)[line[0] == "#"].append(pattern) def check(lock, key): return all(lock[i]+key[i] <= H for i in range(W)) print(sum(check(lock, key) for lock in locks for key in keys))
2024-12-25
text/gemini; lang=en
This content has been proxied by September (ba2dc).