-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
53 lines (40 loc) · 1.74 KB
/
main.py
File metadata and controls
53 lines (40 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import time
import re
from functools import reduce
import math
directions = {}
instructions = []
def got_to_z(position, start=0):
cnt = start
stop = not position.endswith("Z") if start == 0 else True
while stop:
# L is first item in tuple, R is second
instruction = (ord(instructions[cnt % len(instructions)]) - ord('L')) % (ord("R") - ord("L") - 1)
position = directions[position][instruction]
cnt += 1
stop = not position.endswith("Z")
if start != 0 and cnt-start != start:
print("LOOP TIME NOT EQUAL:", (cnt-start))
return cnt-start, position
def main(input_file, stage=1):
global directions, instructions
with open(input_file, "r") as file:
maps = file.readlines()
instructions, nodes = maps[0].strip(), [node.strip().replace(" ", "").split("=")for node in maps[2:]]
directions = {k: re.sub(r"\(|\)", "", v).split(",") for k, v in nodes}
positions = [direction for direction in directions.keys() if direction.endswith("A" if stage == 2 else "AAA")]
steps = [got_to_z(position) for position in positions]
# check how long it takes for the pattern to repeat to z
steps = [got_to_z(end, start)[0] for start, end in steps]
print(reduce(lambda x, y: math.lcm(x, y), steps))
if __name__ == "__main__":
use_example = False
file_name = "example" if use_example else "input"
start_time = time.time()
main(file_name, 1) # sol: 20659
print(f"Stage 1 time: {time.time()-start_time:.10f}")
start_time = time.time()
if use_example:
file_name += "_stage2"
main(file_name, 2) # sol: 249204891
print(f"Stage 2 time: {time.time()-start_time:.10f}")