Skip to content

Commit

Permalink
2024 day 4 part 1 and 2
Browse files Browse the repository at this point in the history
  • Loading branch information
lancelote committed Dec 4, 2024
1 parent 6c32d3e commit 1ba2039
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
- 2021 - ★★★★★ ★★★★★ ★★★★★ ★★★
- 2022 - ★★★★★ ★★★★★ ★★★★★ ★☆
- 2023 - ★★★★★ ★★★★★ ★★★★★ ★★★☆
- 2024 - ★★★
- 2024 - ★★★

## How to use

Expand Down
53 changes: 53 additions & 0 deletions src/year2024/day04a.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""2024 - Day 4 Part 1: Ceres Search"""

SHIFTS = {
# dr dc
(-1, -1),
(-1, 0),
(-1, +1),
(0, +1),
(+1, +1),
(+1, 0),
(+1, -1),
(0, -1),
}

XMAS = "XMAS"


def is_xmas(r: int, c: int, data: list[str]) -> int:
count = 0

rows = len(data)
cols = len(data[0])

for dr, dc in SHIFTS:
nr = r
nc = c

for i in range(4):
in_bounds = 0 <= nr < rows and 0 <= nc < cols
if not in_bounds or XMAS[i] != data[nr][nc]:
break

nr += dr
nc += dc
else:
count += 1

return count


def solve(task: str) -> int:
data = task.split("\n")

ans = 0

rows = len(data)
cols = len(data[0])

for r in range(rows):
for c in range(cols):
ans += is_xmas(r, c, data)

return ans
42 changes: 42 additions & 0 deletions src/year2024/day04b.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""2024 - Day 4 Part 2: Ceres Search"""

OPTIONS = {
"MM" "A" "SS",
"MS" "A" "MS",
"SS" "A" "MM",
"SM" "A" "SM",
}

SHIFTS = (
# dr dc
(0, 0),
(0, +2),
(+1, +1),
(+2, 0),
(+2, +2),
)


def is_xmas(r: int, c: int, data: list[str]) -> bool:
start = data[r][c]

if start not in {"S", "M"}:
return False

word = "".join(data[r + dr][c + dc] for dr, dc in SHIFTS)
return word in OPTIONS


def solve(task: str) -> int:
data = task.split("\n")

ans = 0

rows = len(data)
cols = len(data[0])

for r in range(rows - 2):
for c in range(cols - 2):
ans += is_xmas(r, c, data)

return ans
23 changes: 23 additions & 0 deletions tests/src/year2024/test_day04a.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""2024 - Day 4 Part 1: Ceres Search"""

from textwrap import dedent

from src.year2024.day04a import solve


def test_solve():
task = dedent(
"""
MMMSXXMASM
MSAMXMSMSA
AMXSXMAAMM
MSAMASMSMX
XMASAMXAMM
XXAMMXXAMA
SMSMSASXSS
SAXAMASAAA
MAMMMXMMMM
MXMXAXMASX
"""
).strip()
assert solve(task) == 18
23 changes: 23 additions & 0 deletions tests/src/year2024/test_day04b.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""2024 - Day 4 Part 2: Ceres Search"""

from textwrap import dedent

from src.year2024.day04b import solve


def test_solve():
task = dedent(
"""
.M.S......
..A..MSMS.
.M.S.MAA..
..A.ASMSM.
.M.S.M....
..........
S.S.S.S.S.
.A.A.A.A..
M.M.M.M.M.
..........
"""
).strip()
assert solve(task) == 9

0 comments on commit 1ba2039

Please sign in to comment.