diff --git a/day4/common.py b/day4/common.py index 1e6a340..5306ad4 100644 --- a/day4/common.py +++ b/day4/common.py @@ -1,3 +1,3 @@ -def parse(filename: str) -> list[str]: +def parse(filename: str) -> list[int]: with open(filename) as f: return [line.strip() for line in f.readlines()] diff --git a/day4/part2.py b/day4/part2.py new file mode 100755 index 0000000..5e9fa11 --- /dev/null +++ b/day4/part2.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python +from common import parse + + +def is_x_mas(input: list[str], x: int, y: int) -> bool: + return "".join([input[y + i][x + i] for i in range(3)]) in ["MAS", "SAM"] and "".join( + [input[y + 2 - i][x + i] for i in range(3)] + ) in ["MAS", "SAM"] + + +def solve(input: list[str]) -> str: + starts = [(x, y) for x in range(len(input[0]) - 2) for y in range(len(input) - 2)] + return len(list(filter(lambda p: is_x_mas(input, p[0], p[1]), starts))) + + +def main(): + print(solve(parse("input"))) + + +if __name__ == "__main__": + main() diff --git a/get_input.sh b/get_input.sh index 4ca569d..b5a9932 100755 --- a/get_input.sh +++ b/get_input.sh @@ -1,5 +1,7 @@ #!/bin/bash day=$1 -mkdir day${day} -cp template/* day${day} +if [ ! -d day${day} ]; then + mkdir day${day} + cp template/* day${day} +fi curl -o day${day}/input --cookie ~/AdventOfCode/2024/cookies.txt https://adventofcode.com/2024/day/$day/input