started day4
This commit is contained in:
parent
2cd5a8f9fd
commit
10a0971346
39
2024/day4/main.py
Normal file
39
2024/day4/main.py
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
import sys
|
||||||
|
|
||||||
|
with open('test.text', 'r') as file:
|
||||||
|
data: list[str] = file.readlines()
|
||||||
|
|
||||||
|
y_bound = len(data)
|
||||||
|
x_bound = len(data[0])
|
||||||
|
|
||||||
|
# Part 1
|
||||||
|
def search_all_directions(s: str, x: int, y: int):
|
||||||
|
print(s)
|
||||||
|
if s != 'X' and s != 'XM' and s != 'XMA' and s != 'XMAS':
|
||||||
|
return 0
|
||||||
|
|
||||||
|
if x < 0 or y < 0 or x >= x_bound or y >= y_bound:
|
||||||
|
return 0
|
||||||
|
|
||||||
|
# print(x, y)
|
||||||
|
if data[y][x] != s:
|
||||||
|
s = s + data[y][x]
|
||||||
|
print(s)
|
||||||
|
|
||||||
|
if s == 'XMAS':
|
||||||
|
return 1
|
||||||
|
|
||||||
|
return (search_all_directions(s, y, x - 1)
|
||||||
|
+ search_all_directions(s, y, x + 1)
|
||||||
|
+ search_all_directions(s, y + 1, x)
|
||||||
|
+ search_all_directions(s, y - 1, x)
|
||||||
|
)
|
||||||
|
|
||||||
|
total = 0
|
||||||
|
for y, line in enumerate(data):
|
||||||
|
for x, char in enumerate(line):
|
||||||
|
if char == 'X':
|
||||||
|
total += search_all_directions(char, x, y)
|
||||||
|
|
||||||
|
print(total)
|
||||||
|
|
||||||
10
2024/day4/test.text
Normal file
10
2024/day4/test.text
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
MMMSXXMASM
|
||||||
|
MSAMXMSMSA
|
||||||
|
AMXSXMAAMM
|
||||||
|
MSAMASMSMX
|
||||||
|
XMASAMXAMM
|
||||||
|
XXAMMXXAMA
|
||||||
|
SMSMSASXSS
|
||||||
|
SAXAMASAAA
|
||||||
|
MAMMMXMMMM
|
||||||
|
MXMXAXMASX
|
||||||
Loading…
x
Reference in New Issue
Block a user