From 2a82cd35e9dd6df88aacb315300fa05ca6035f41 Mon Sep 17 00:00:00 2001 From: Fedaya Date: Wed, 4 Dec 2024 07:28:47 +0100 Subject: [PATCH] Day4 part1 completed / renamed global requirements.txt --- day4/common.py | 3 +++ day4/part1.py | 34 ++++++++++++++++++++++++ day4/requirements.txt | 1 + requirements.txt => dev_requirements.txt | 0 4 files changed, 38 insertions(+) create mode 100644 day4/common.py create mode 100755 day4/part1.py create mode 100644 day4/requirements.txt rename requirements.txt => dev_requirements.txt (100%) diff --git a/day4/common.py b/day4/common.py new file mode 100644 index 0000000..1e6a340 --- /dev/null +++ b/day4/common.py @@ -0,0 +1,3 @@ +def parse(filename: str) -> list[str]: + with open(filename) as f: + return [line.strip() for line in f.readlines()] diff --git a/day4/part1.py b/day4/part1.py new file mode 100755 index 0000000..72d199f --- /dev/null +++ b/day4/part1.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python +from collections.abc import Iterable + +import regex as re +from common import parse + + +def transposed( + matrix: list[str], +) -> Iterable: # see https://docs.python.org/3/library/itertools.html for the base "transpose" function + return map(lambda x: "".join(x), zip(*matrix, strict=True)) + + +def solve(input: list[str]) -> int: + diagonal = 0 + horizontal = sum([len(re.findall(r"(XMAS|SAMX)", line, overlapped=True)) for line in input]) # Horizontal + vertical = sum([len(re.findall(r"(XMAS|SAMX)", line, overlapped=True)) for line in transposed(input)]) # Vertical + + for y in range(len(input) - 3): + for x in range(len(input) - 3): + # print(y, x, "\\", "".join([input[y + i][x + i] for i in range(4)])) + diagonal += 1 if "".join([input[y + i][x + i] for i in range(4)]) in ["XMAS", "SAMX"] else 0 + # print(y + 3, x, "/", "".join([input[y + 3 - i][x + i] for i in range(4)])) + diagonal += 1 if "".join([input[y + 3 - i][x + i] for i in range(4)]) in ["XMAS", "SAMX"] else 0 + # print(diagonal, horizontal, vertical) + return diagonal + horizontal + vertical + + +def main(): + print(solve(parse("input"))) + + +if __name__ == "__main__": + main() diff --git a/day4/requirements.txt b/day4/requirements.txt new file mode 100644 index 0000000..4f9256d --- /dev/null +++ b/day4/requirements.txt @@ -0,0 +1 @@ +regex diff --git a/requirements.txt b/dev_requirements.txt similarity index 100% rename from requirements.txt rename to dev_requirements.txt