Day4 part1 completed / renamed global requirements.txt
This commit is contained in:
parent
defa32fc9c
commit
2a82cd35e9
3
day4/common.py
Normal file
3
day4/common.py
Normal file
@ -0,0 +1,3 @@
|
||||
def parse(filename: str) -> list[str]:
|
||||
with open(filename) as f:
|
||||
return [line.strip() for line in f.readlines()]
|
34
day4/part1.py
Executable file
34
day4/part1.py
Executable file
@ -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()
|
1
day4/requirements.txt
Normal file
1
day4/requirements.txt
Normal file
@ -0,0 +1 @@
|
||||
regex
|
Loading…
x
Reference in New Issue
Block a user