day 12 python simplify
parent
e378777cd3
commit
6421ecf7fa
41
day12/sol.py
41
day12/sol.py
|
@ -1,28 +1,27 @@
|
||||||
import heapq
|
import heapq
|
||||||
import string
|
import string
|
||||||
|
|
||||||
chars = 'ES'+string.ascii_lowercase
|
elevation = {c:i for i, c in enumerate(string.ascii_lowercase)}
|
||||||
|
elevation['E'] = elevation['z']
|
||||||
|
elevation['S'] = elevation['a']
|
||||||
|
|
||||||
data = open("input").read().splitlines(False)
|
data = open("input").read().splitlines(False)
|
||||||
data = [list(map(chars.index, line)) for line in data]
|
#print(*data, sep="\n")
|
||||||
#print(data)
|
|
||||||
|
|
||||||
def get(x, y):
|
|
||||||
return data[y][x]
|
|
||||||
|
|
||||||
extra = []
|
extra = []
|
||||||
for x in range(len(data[0])):
|
for y, row in enumerate(data):
|
||||||
for y in range(len(data)):
|
for x, c in enumerate(row):
|
||||||
if get(x,y) == 1: # 'S'
|
if c == 'S':
|
||||||
data[y][x] = chars.index('a')
|
|
||||||
start = (x,y)
|
start = (x,y)
|
||||||
elif get(x,y) == 0: # 'E'
|
elif c == 'E':
|
||||||
data[y][x] = chars.index('z')
|
|
||||||
goal = (x,y)
|
goal = (x,y)
|
||||||
elif get(x,y) == 2: # 'a'
|
elif c == 'a':
|
||||||
# for part 2
|
# for part 2
|
||||||
extra.append((x,y))
|
extra.append((x,y))
|
||||||
|
|
||||||
|
def get(x, y):
|
||||||
|
return elevation[data[y][x]]
|
||||||
|
|
||||||
def solve(part):
|
def solve(part):
|
||||||
q = [(0, start)]
|
q = [(0, start)]
|
||||||
|
|
||||||
|
@ -41,19 +40,15 @@ def solve(part):
|
||||||
here = get(x,y)
|
here = get(x,y)
|
||||||
done[(x,y)] = cost
|
done[(x,y)] = cost
|
||||||
|
|
||||||
n = []
|
def push(x,y):
|
||||||
def check(x,y):
|
|
||||||
if 0 <= y < len(data):
|
if 0 <= y < len(data):
|
||||||
if 0 <= x < len(data[y]):
|
if 0 <= x < len(data[y]):
|
||||||
if get(x,y) <= here+1 and (x,y) not in done:
|
if get(x,y) <= here+1 and (x,y) not in done:
|
||||||
n.append((cost+1, (x,y)))
|
heapq.heappush(q, (cost+1, (x,y)))
|
||||||
check(x-1, y)
|
push(x-1, y)
|
||||||
check(x+1,y)
|
push(x+1,y)
|
||||||
check(x, y-1)
|
push(x, y-1)
|
||||||
check(x, y+1)
|
push(x, y+1)
|
||||||
|
|
||||||
for p in n:
|
|
||||||
heapq.heappush(q, p)
|
|
||||||
|
|
||||||
solve(1)
|
solve(1)
|
||||||
solve(2)
|
solve(2)
|
||||||
|
|
Loading…
Reference in New Issue