-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
142 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |