2025-12-08 20:29:46 -06:00
|
|
|
#!/usr/bin/env python3
|
2025-12-07 20:00:13 -06:00
|
|
|
|
|
|
|
|
from logging import debug, DEBUG, basicConfig
|
|
|
|
|
from sys import argv
|
|
|
|
|
from re import finditer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def parse_input(input_filepath: str) -> list[str]:
|
|
|
|
|
|
|
|
|
|
with open(file=input_filepath, mode="r") as input_file:
|
|
|
|
|
input_data: list[str] = input_file.readlines()
|
|
|
|
|
|
|
|
|
|
return [s.strip() for s in input_data]
|
|
|
|
|
|
|
|
|
|
|
2025-12-07 20:00:18 -06:00
|
|
|
def find_split_count_and_timelines(grid: list[str]) -> tuple[int, int]:
|
|
|
|
|
beam_dict: dict = {grid[0].find("S"): 1}
|
2025-12-07 19:56:46 -06:00
|
|
|
split_count: int = 0
|
2025-12-07 20:00:18 -06:00
|
|
|
for row in range(len(grid[1:])):
|
|
|
|
|
splitters: set[int] = set([i.start(0) for i in finditer("\\^", grid[row])])
|
2025-12-07 20:00:13 -06:00
|
|
|
if not splitters:
|
|
|
|
|
debug(
|
2025-12-07 19:56:46 -06:00
|
|
|
f"{"".join(list[str](map(lambda x: "|" if (x in beam_dict.keys() and beam_dict[x] != 0) else ("^" if x in splitters else "."), range(len(grid)))))}"
|
2025-12-07 20:00:13 -06:00
|
|
|
)
|
|
|
|
|
continue
|
2025-12-07 19:56:46 -06:00
|
|
|
intersections = set(
|
|
|
|
|
[x for x in beam_dict.keys() if beam_dict[x] != 0]
|
|
|
|
|
).intersection(splitters)
|
2025-12-07 20:00:13 -06:00
|
|
|
split_count += len(intersections)
|
2025-12-07 20:00:18 -06:00
|
|
|
for i in intersections:
|
|
|
|
|
temp_beams = beam_dict[i]
|
|
|
|
|
beam_dict[i] = 0
|
|
|
|
|
if (i - 1) in beam_dict.keys():
|
|
|
|
|
beam_dict[i - 1] += temp_beams
|
|
|
|
|
else:
|
|
|
|
|
beam_dict[i - 1] = temp_beams
|
|
|
|
|
if (i + 1) in beam_dict.keys():
|
|
|
|
|
beam_dict[i + 1] += temp_beams
|
|
|
|
|
else:
|
|
|
|
|
beam_dict[i + 1] = temp_beams
|
2025-12-07 20:00:13 -06:00
|
|
|
debug(
|
2025-12-07 19:56:46 -06:00
|
|
|
f"{"".join(list[str](map(lambda x: "|" if (x in beam_dict.keys() and beam_dict[x]) else ("^" if x in splitters else "."), range(len(grid)))))}"
|
2025-12-07 20:00:13 -06:00
|
|
|
)
|
2025-12-07 20:00:18 -06:00
|
|
|
return (split_count, sum(beam_dict.values()))
|
2025-12-07 20:00:13 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def main() -> None:
|
|
|
|
|
input_filepath = "input/grid.txt"
|
2025-12-07 20:00:18 -06:00
|
|
|
grid = parse_input(input_filepath)
|
|
|
|
|
split_count, timelines = find_split_count_and_timelines(grid)
|
2025-12-07 20:00:13 -06:00
|
|
|
print(f"The beam splits {split_count} times.")
|
2025-12-07 20:00:18 -06:00
|
|
|
print(f"There are {timelines} possible beam timelines.")
|
2025-12-07 20:00:13 -06:00
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
if "-d" in argv or "--debug" in argv:
|
|
|
|
|
basicConfig(filename="debug.log", level=DEBUG)
|
|
|
|
|
main()
|
|
|
|
|
exit(0)
|