diff options
Diffstat (limited to '2023/03/puzzle-1.py')
-rwxr-xr-x | 2023/03/puzzle-1.py | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/2023/03/puzzle-1.py b/2023/03/puzzle-1.py new file mode 100755 index 0000000..d078c4d --- /dev/null +++ b/2023/03/puzzle-1.py @@ -0,0 +1,35 @@ +#!/usr/bin/python + +import re + + +def main() -> None: + with open("input", "r", encoding="utf-8") as f: + data = f.read() + + symbols = set() + matches = [] + + lines = [line for line in data.split('\n') if line] + for i, line in enumerate(lines): + for match in re.finditer(r'\d+', line): + matches.append((i, match)) + for match in re.finditer(r'[^0-9.]', line): + symbols.add((i, match.start())) + + acc = 0 + for match in matches: + l, m = match + s, e = m.span() + ps = [(l, s-1), (l, e)] + for i in range(s-1, e+1): + ps.append((l-1, i)) + ps.append((l+1, i)) + for p in ps: + if p in symbols: + acc += int(m.group(0)) + + print(acc) + +if __name__ == "__main__": + main() |