Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Day 4: Ceres Search #32

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ Development occurs in language-specific directories:
|[Day1.hs](hs/src/Day1.hs)|[Day1.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day1.kt)|[day1.py](py/aoc2024/day1.py)|[day1.rs](rs/src/day1.rs)|
|[Day2.hs](hs/src/Day2.hs)|[Day2.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day2.kt)|[day2.py](py/aoc2024/day2.py)|[day2.rs](rs/src/day2.rs)|
|[Day3.hs](hs/src/Day3.hs)|[Day3.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day3.kt)|[day3.py](py/aoc2024/day3.py)|[day3.rs](rs/src/day3.rs)|
|[Day4.hs](hs/src/Day4.hs)|[Day4.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day4.kt)|||
|[Day4.hs](hs/src/Day4.hs)|[Day4.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day4.kt)|[day4.py](py/aoc2024/day4.py)||
52 changes: 52 additions & 0 deletions py/aoc2024/day4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""
Day 4: Ceres Search
"""

SAMPLE_INPUT = """
MMMSXXMASM
MSAMXMSMSA
AMXSXMAAMM
MSAMASMSMX
XMASAMXAMM
XXAMMXXAMA
SMSMSASXSS
SAXAMASAAA
MAMMMXMMMM
MXMXAXMASX
"""


def part1(data: str) -> int:
"""
>>> part1(SAMPLE_INPUT)
18
"""
lines = data.splitlines()
return sum(
all(
(y2 := y + i * dy) in range(len(lines))
and (x2 := x + i * dx) in range(len(lines[y2]))
and lines[y2][x2] == c
for i, c in enumerate("XMAS")
)
for y, line in enumerate(lines)
for x in range(len(line))
for dx in range(-1, 2)
for dy in range(-1, 2)
)


def part2(data: str) -> int:
"""
>>> part2(SAMPLE_INPUT)
9
"""
lines = data.splitlines()
return sum(
"".join(chars) in ("MMASS", "MSAMS", "SMASM", "SSAMM")
for above, line, below in zip(lines, lines[1:], lines[2:])
for chars in zip(above, above[2:], line[1:], below, below[2:])
)


parts = (part1, part2)
1 change: 1 addition & 0 deletions py/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ aoc2024 = "aoc2024.main:main"
day1 = "aoc2024.day1:parts"
day2 = "aoc2024.day2:parts"
day3 = "aoc2024.day3:parts"
day4 = "aoc2024.day4:parts"

[build-system]
requires = ["poetry-core"]
Expand Down
Loading