From 51f2e7a4fd0aa7b3d4bd0358d78104024db9e95c Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Mon, 9 Dec 2024 21:45:58 +0100 Subject: [PATCH] add 2023/day3.1 --- 2023/03/main.py | 69 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 2023/03/main.py diff --git a/2023/03/main.py b/2023/03/main.py new file mode 100644 index 0000000..a3a8c83 --- /dev/null +++ b/2023/03/main.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python +# -*- encoding: utf-8; py-indent-offset: 4 -*- + +# Author: Sebastian Mark +# CC-BY-SA (https://creativecommons.org/licenses/by-sa/4.0/deed.de) + +# pylint: disable=missing-module-docstring,missing-function-docstring,consider-using-f-string + +import re + + +def readinput(): + with open("input", "r", encoding="utf-8") as file: + lines = [line.rstrip("\n") for line in file.readlines()] + return lines + + +def find_numbers(lines: list) -> set: + # create a set of [(y,x,number),(y,x,number)] + numbers = set() + for y, line in enumerate(lines): + matches = re.finditer(r"\d+", line) + for match in matches: + x = match.start() + numbers.add((y, x, match.group())) + return numbers + + +def check_partnumber(lines: list, starty: int, startx: int, partnumber: str) -> bool: + directions = [ + (-1, 0), # Up + (-1, 1), # Up-right + (0, 1), # Right + (1, 1), # Down-right + (1, 0), # Down + (1, -1), # Down-left + (0, -1), # Left + (-1, -1), # Up-left + ] + + width = len(lines[0]) + height = len(lines) + + for charpos in range( + startx, startx + len(partnumber) + ): # for all numbers in the partnumber + for dy, dx in directions: # check all directions + nxty, nxtx = starty + dy, charpos + dx # new checkposition + if 0 <= nxtx < width and 0 <= nxty < height: # validate borders + if lines[nxty][nxtx] not in "0123456789.": + return True # found symbol, this is a valid partnumber + + return False + + +def main(): + lines = readinput() + numbers = find_numbers(lines) + + # part 1 + count = 0 + for y, x, number in numbers: + if check_partnumber(lines, y, x, number): + count += int(number) + print("Sum of partnumbers: %d" % count) + + +if __name__ == "__main__": + main()