2018 day 20 wip
This commit is contained in:
parent
2647ccd353
commit
5fae7c0366
107
2018/d20.py
Normal file
107
2018/d20.py
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
from lib import get_data, add2
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
|
data = "^WNE$"
|
||||||
|
data = "^ENWWW(NEEE|SSE(EE|N))$"
|
||||||
|
data = "^ESSWWN(E|NNENN(EESS(WNSE|)SSS|WWWSSSSE(SW|NNNE)))$"
|
||||||
|
data = "^WSSEESWWWNW(S|NENNEEEENN(ESSSSW(NWSW|SSEN)|WSWWN(E|WWS(E|SS))))$"
|
||||||
|
|
||||||
|
DIRS = {
|
||||||
|
"N": (-1, 0),
|
||||||
|
"E": (0, 1),
|
||||||
|
"S": (1, 0),
|
||||||
|
"W": (0, -1),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def part_1(data):
|
||||||
|
data = data[1:]
|
||||||
|
|
||||||
|
# def parse(i):
|
||||||
|
# max_len = 0
|
||||||
|
# cur_len = 0
|
||||||
|
# while i < len(data):
|
||||||
|
# c = data[i]
|
||||||
|
# if c in DIRS.keys():
|
||||||
|
# i += 1
|
||||||
|
# cur_len += 1
|
||||||
|
# elif c == "(":
|
||||||
|
# sub_len, i = parse(i + 1)
|
||||||
|
# cur_len += sub_len
|
||||||
|
# elif c == "|":
|
||||||
|
# max_len = max(max_len, cur_len)
|
||||||
|
# cur_len = 0
|
||||||
|
# i += 1
|
||||||
|
# elif c == ")":
|
||||||
|
# all_max = max(max_len, cur_len)
|
||||||
|
# return all_max, i + 1
|
||||||
|
# elif c == "$":
|
||||||
|
# max_len = max(max_len, cur_len)
|
||||||
|
# i += 1
|
||||||
|
# else:
|
||||||
|
# print(c)
|
||||||
|
# assert False
|
||||||
|
# return max_len, i
|
||||||
|
#
|
||||||
|
# print(parse(0)[0])
|
||||||
|
|
||||||
|
g = defaultdict(set)
|
||||||
|
def parse(xs, i):
|
||||||
|
xs_orig = xs.copy()
|
||||||
|
xs_done = []
|
||||||
|
|
||||||
|
while i < len(data):
|
||||||
|
c = data[i]
|
||||||
|
if c in DIRS.keys():
|
||||||
|
for xi in range(len(xs)):
|
||||||
|
pos = xs[xi]
|
||||||
|
npos = add2(pos, DIRS[c])
|
||||||
|
g[pos].add(npos)
|
||||||
|
g[npos].add(pos)
|
||||||
|
xs[xi] = npos
|
||||||
|
i += 1
|
||||||
|
elif c == "(":
|
||||||
|
xs, i = parse(xs, i + 1)
|
||||||
|
elif c == "|":
|
||||||
|
xs_done += xs
|
||||||
|
xs = xs_orig
|
||||||
|
i += 1
|
||||||
|
elif c == ")":
|
||||||
|
xs_done += xs
|
||||||
|
return xs_done, i + 1
|
||||||
|
elif c == "$":
|
||||||
|
xs_done += xs
|
||||||
|
i += 1
|
||||||
|
else:
|
||||||
|
assert False
|
||||||
|
return xs_done, i
|
||||||
|
|
||||||
|
parse([(0, 0)], 0)
|
||||||
|
|
||||||
|
seen = set()
|
||||||
|
dists = {}
|
||||||
|
xs = [(0, 0)]
|
||||||
|
steps = 0
|
||||||
|
while len(xs) > 0:
|
||||||
|
nxs = []
|
||||||
|
for x in xs:
|
||||||
|
if x in seen:
|
||||||
|
continue
|
||||||
|
seen.add(x)
|
||||||
|
dists[x] = steps
|
||||||
|
for nb in g[x]:
|
||||||
|
if not nb in seen:
|
||||||
|
nxs.append(nb)
|
||||||
|
xs = nxs
|
||||||
|
steps += 1
|
||||||
|
|
||||||
|
print(max(dists.values()))
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
# data = get_data(__file__).strip()
|
||||||
|
part_1(data)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
Loading…
Reference in New Issue
Block a user