diff options
Diffstat (limited to '22.24/main.py')
-rw-r--r-- | 22.24/main.py | 24 |
1 files changed, 18 insertions, 6 deletions
diff --git a/22.24/main.py b/22.24/main.py index 49fb875..bd348a0 100644 --- a/22.24/main.py +++ b/22.24/main.py @@ -16,7 +16,7 @@ def tdir(f): data = stdin.readlines() start = (data[0].index('.') - 1, -1) -end = (data[-1].index('.') - 1, len(data) - 3) +end = (data[-1].index('.') - 1, len(data) - 2) data = [l.strip()[1:-1] for l in data[1:-1]] w = len(data[0]) h = len(data) @@ -32,6 +32,7 @@ for y, line in enumerate(data): def coll(x, y, t): if (x, y) == start: return False + if (x, y) == end: return False if not 0 <= x < w: return True if not 0 <= y < h: return True for d in range(4): @@ -41,20 +42,31 @@ def coll(x, y, t): return False def search(): - cur = {(start, 0)} + cur = {(start, 0, 0)} frontier = set() + visited = set(cur) while True: - for bv, t in cur: + for bv, t, gid in cur: vs = [bv] for d in range(4): vs.append(tadd(bv, tdir(d))) for v in vs: if not coll(*v, t+1): - if v == end: + new = None + if v == end and gid == 0: + new = (v, t+1, 1) + elif v == start and gid == 1: + new = (v, t+1, 2) + elif v == end and gid == 2: return t+1 - frontier.add((v, t+1)) + else: + new = (v, t+1, gid) + chk = (new[0], new[1] % cycle, new[2]) + if not chk in visited: + visited.add(chk) + frontier.add(new) cur = frontier frontier = set() return None -print(search() + 1) +print(search()) |