From 460d51db5ca5abfd7494f3aff091acb8ede43c5b Mon Sep 17 00:00:00 2001 From: Andrew Ekstedt Date: Sat, 24 Dec 2022 11:22:49 -0800 Subject: [PATCH] day 24 python minor optimization i can't believe this is faster but it is (slightly) --- day24/sol.py | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/day24/sol.py b/day24/sol.py index 8ba9f5c..fc303d6 100644 --- a/day24/sol.py +++ b/day24/sol.py @@ -8,13 +8,6 @@ import astar #print(*data, sep="\n") -blizzards = {'>': {}, '<': {}, '^': {}, 'v': {}} -for y in range(len(data)): - for x in range(len(data[y])): - c = data[y][x] - if c in blizzards: - blizzards[c][x,y] = 1 - def show(t): H = len(data)-2 for y in range(len(data)): @@ -27,19 +20,19 @@ def show(t): else: bliz = [] u = 1 + ((x - t)-1)%W - if (u,y) in blizzards['>']: + if data[y][u] == '>': bliz.append('>') u = 1 + ((x + t)-1)%W - if (u,y) in blizzards['<']: + if data[y][u] == '<': bliz.append('<') v = 1 + ((y - t)-1)%H - if (x,v) in blizzards['v']: + if data[v][x] == 'v': bliz.append('v') v = 1 + ((y + t)-1)%H - if (x,v) in blizzards['^']: + if data[v][x] == '^': bliz.append('^') if len(bliz) == 0: @@ -65,19 +58,19 @@ def blocked(x,y,t): W = len(data[y])-2 u = 1 + ((x - t)-1)%W - if (u,y) in blizzards['>']: + if data[y][u] == '>': return True u = 1 + ((x + t)-1)%W - if (u,y) in blizzards['<']: + if data[y][u] == '<': return True v = 1 + ((y - t)-1)%H - if (x,v) in blizzards['v']: + if data[v][x] == 'v': return True v = 1 + ((y + t)-1)%H - if (x,v) in blizzards['^']: + if data[v][x] == '^': return True return False