diff options
author | Thomas Voss <mail@thomasvoss.com> | 2023-12-03 15:02:28 +0100 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2023-12-03 15:02:28 +0100 |
commit | fdba09c1c6c67c9081cf470cd6b3c4e8b7670cf6 (patch) | |
tree | 43143d417c72e81ccf2a52a5d489b9aca9e984b3 /2023/03/puzzle-1.py | |
parent | c1875f1bee7c07227942a831e466f2c262339439 (diff) |
Add 2023 day 3 part 1 solution
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() |