MCPcopy Create free account
hub / github.com/AxlLind/AdventOfCode / apply_regex

Function apply_regex

2018/src/20.py:34–55  ·  view source on GitHub ↗
(m: Graph, regex: Regex, coords: set[tuple[int,int]])

Source from the content-addressed store, hash-verified

32 return i, (RegexType.LIST, r)
33
34def apply_regex(m: Graph, regex: Regex, coords: set[tuple[int,int]]) -> set[tuple[int,int]]:
35 match regex[0]:
36 case RegexType.SIMPLE:
37 newcoords = set()
38 for r,c in coords:
39 for d in regex[1]:
40 rr,cc = r,c
41 match d:
42 case 'N': r -= 1
43 case 'S': r += 1
44 case 'E': c += 1
45 case 'W': c -= 1
46 m[rr,cc].add((r,c))
47 m[r,c].add((rr,cc))
48 newcoords.add((r,c))
49 return newcoords
50 case RegexType.OR:
51 return set.union(*(apply_regex(m,re,coords) for re in regex[1]))
52 case RegexType.LIST:
53 for re in regex[1]:
54 coords = apply_regex(m,re,coords)
55 return coords
56
57def bfs_distances(m: Graph) -> list[int]:
58 q, dist = deque([(0,0)]), {(0,0): 0}

Callers 1

mainFunction · 0.85

Calls 1

unionMethod · 0.80

Tested by

no test coverage detected