Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def process(s, num_days=0):
- offsets = dict(e=(0, 1), w=(0, -1), sw=(1, 0), se=(1, 1),
- nw=(-1, -1), ne=(-1, 0))
- indices = set()
- for line in s.strip('\n').splitlines():
- index = (0, 0)
- while line:
- for prefix, offset in offsets.items():
- if line.startswith(prefix):
- index = tuple(map(sum, zip(index, offset)))
- line = line[len(prefix):]
- indices ^= {index}
- def neighbors(index):
- for offset in offsets.values():
- # yield tuple(map(sum, zip(index, offset))) # 2x slower
- yield index[0] + offset[0], index[1] + offset[1]
- for _ in range(num_days):
- neighbor_counts = collections.Counter(
- itertools.chain.from_iterable(neighbors(index) for index in indices))
- indices = {index for index, count in neighbor_counts.items()
- if count == 2 or count == 1 and index in indices}
- return len(indices)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement