this post was submitted on 04 Dec 2024
18 points (95.0% liked)

Advent Of Code

996 readers
4 users here now

An unofficial home for the advent of code community on programming.dev!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

AoC 2024

Solution Threads

M T W T F S S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 1 year ago
MODERATORS
 

Day 4: Ceres Search

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

you are viewing a single comment's thread
view the rest of the comments
[โ€“] [email protected] 1 points 1 month ago* (last edited 1 month ago)

Python

Essentially I'm extracting strings from the word search and compare them to the desired value. For part one that means extracting from an X in eight directions. Because I'm reading from the central X outwards, I don't need to reverse any of them.
Part two reads two strings in an X-shape around the coordinates of each X. The resulting strings are filtered down to include only "MAS" and "SAM". If there are exactly two strings we found an X-MAS.

from pathlib import Path


def parse_input(input: str) -> list[str]:
    return input.strip().splitlines()


def extract_strings_one(m: int, n: int, haystack: list[str], l: int = 4) -> list[str]:
    result = []
    # Right
    if m + l <= len(haystack[n]):
        result.append(haystack[n][m : m + l])
    # Up-Right
    if m + l <= len(haystack[n]) and n > l - 2:
        result.append("".join([haystack[n - i][m + i] for i in range(l)]))
    # Up
    if n > l - 2:
        result.append("".join([haystack[n - i][m] for i in range(l)]))
    # Up-Left
    if m > l - 2 and n > l - 2:
        result.append("".join([haystack[n - i][m - i] for i in range(l)]))
    # Left
    if m > l - 2:
        result.append("".join([haystack[n][m - i] for i in range(l)]))
    # Down-Left
    if m > l - 2 and n + l <= len(haystack):
        result.append("".join([haystack[n + i][m - i] for i in range(l)]))
    # Down
    if n + l <= len(haystack):
        result.append("".join([haystack[n + i][m] for i in range(l)]))
    # Down-Right
    if m + l <= len(haystack[n]) and n + l <= len(haystack):
        result.append("".join([haystack[n + i][m + i] for i in range(l)]))
    return result


def extract_strings_two(m: int, n: int, haystack: list[str], d: int = 1) -> list[str]:
    result = []
    if 0 <= m - d and m + d < len(haystack[n]) and 0 <= n - d and n + d < len(haystack):
        result.append("".join([haystack[n + i][m + i] for i in range(-d, d + 1)]))
        result.append("".join([haystack[n - i][m + i] for i in range(-d, d + 1)]))
    return result


def part_one(input: str) -> int:
    lines = parse_input(input)
    xmas_count = 0
    for i, line in enumerate(lines):
        x = line.find("X", 0)
        while x != -1:
            xmas_count += len(
                list(filter(lambda s: s == "XMAS", extract_strings_one(x, i, lines)))
            )
            x = line.find("X", x + 1)
    return xmas_count


def part_two(input: str) -> int:
    lines = parse_input(input)
    x_mas_count = 0
    for i, line in enumerate(lines[1:-1], 1):
        a = line.find("A", 0)
        while a != -1:
            if (
                len(
                    list(
                        filter(
                            lambda s: s in ("MAS", "SAM"),
                            extract_strings_two(a, i, lines),
                        )
                    )
                )
                == 2
            ):
                x_mas_count += 1
            a = line.find("A", a + 1)
    return x_mas_count


if __name__ == "__main__":
    input = Path("input").read_text("utf-8")
    print(part_one(input))
    print(part_two(input))