solve day 6 part 1

This commit is contained in:
Heiko Ludwig 2024-12-07 15:10:15 +01:00
parent 6b9d579764
commit 5d48e673e1

View file

@ -7,10 +7,47 @@ def get_lines(filename: str) -> list:
return [line.strip() for line in file.readlines()] return [line.strip() for line in file.readlines()]
def main(): def get_lab(lines: list) -> tuple:
lines = get_lines("sample-input.txt") lab = []
# lines = get_lines("input.txt") 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__': if __name__ == '__main__':
main() main()