advent_of_code_2024/day_06/program.py
2024-12-07 15:10:15 +01:00

53 lines
1.5 KiB
Python
Executable file

#!/usr/bin/env python3
# https://adventofcode.com/2024/day/6
def get_lines(filename: str) -> list:
with open(filename, "r") as file:
return [line.strip() for line in file.readlines()]
def get_lab(lines: list) -> tuple:
lab = []
g_row = 0
g_col = 0
for row_num, row_content in enumerate(lines):
row = []
for col_num, cell_content in enumerate(row_content):
if cell_content == "^":
g_row, g_col = row_num, col_num
row.append('.')
else: row.append(cell_content)
lab.append(row)
return lab, g_row, g_col
def get_visits(lab: list, g_row: int, g_col: int) -> int:
visits = {}
directions = [(-1, 0), (0, 1), (1, 0), (0, -1)]
g_dir = 0
while True:
visits[g_row,g_col] = True
delta_row, delta_col = directions[g_dir]
new_row = g_row + delta_row
new_col = g_col + delta_col
# print(new_row, new_col)
if new_row < 0 or new_row >= len(lab) or new_col < 0 or new_col >= len(lab[0]):
return len(visits)
elif lab[new_row][new_col] == "#":
g_dir += 1
if g_dir >= len(directions):
g_dir = 0
else:
g_row = new_row
g_col = new_col
def main():
# lines = get_lines("sample-input.txt")
lines = get_lines("input.txt")
lab, g_row, g_col = get_lab(lines)
# print(lab)
# print(g_row, g_col)
print("Part 1: Visited positions:", get_visits(lab, g_row, g_col))
if __name__ == '__main__':
main()