prettify and lint optimization

This commit is contained in:
2023-12-10 17:24:14 +01:00
parent 274c69d0c4
commit 4cd62ef794
32 changed files with 599 additions and 262 deletions

View File

@@ -1,18 +1,19 @@
import sys
from math import gcd
def parse_file(file_path):
print(f"Parsing file: {file_path}")
with open(file_path, 'r') as file:
with open(file_path, "r") as file:
D = file.read().strip()
steps, rule = D.split('\n\n')
GO = {'L': {}, 'R': {}}
for line in rule.split('\n'):
st, lr = line.split('=')
left, right = lr.split(',')
GO['L'][st.strip()] = left.strip()[1:].strip()
GO['R'][st.strip()] = right[:-1].strip()
return [0 if char == 'L' else 1 for char in steps], GO
steps, rule = D.split("\n\n")
GO = {"L": {}, "R": {}}
for line in rule.split("\n"):
st, lr = line.split("=")
left, right = lr.split(",")
GO["L"][st.strip()] = left.strip()[1:].strip()
GO["R"][st.strip()] = right[:-1].strip()
return [0 if char == "L" else 1 for char in steps], GO
def lcm(xs):
ans = 1
@@ -20,16 +21,17 @@ def lcm(xs):
ans = (x * ans) // gcd(x, ans)
return ans
def navigate_network_simultaneously(steps, GO):
print("Starting navigation of network.")
POS = [s for s in GO['L'] if s.endswith('A')]
POS = [s for s in GO["L"] if s.endswith("A")]
T = {}
t = 0
while True:
NP = []
for i, p in enumerate(POS):
p = GO['L' if steps[t % len(steps)] == 0 else 'R'][p]
if p.endswith('Z'):
p = GO["L" if steps[t % len(steps)] == 0 else "R"][p]
if p.endswith("Z"):
T[i] = t + 1
if len(T) == len(POS):
print(f"All paths reached 'Z' nodes at step {t + 1}.")
@@ -40,19 +42,24 @@ def navigate_network_simultaneously(steps, GO):
print(f"Step {t}: Current nodes - {POS}")
assert False
def run_test():
print("Running test...")
expected_result = 6
steps, GO = parse_file("../test2.txt")
result = navigate_network_simultaneously(steps, GO)
assert result == expected_result, f"Test failed, expected {expected_result} but got {result}"
assert (
result == expected_result
), f"Test failed, expected {expected_result} but got {result}"
print(f"Test passed with {result} steps.")
def main():
run_test()
steps, GO = parse_file("../input.txt")
result = navigate_network_simultaneously(steps, GO)
print(f"All paths reached 'Z' nodes simultaneously in {result} steps.")
if __name__ == "__main__":
main()