Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import Counter
- infile=open("ash.txt")
- elfs={(x,y)
- for y, line in enumerate(infile)
- for x,ch in enumerate(line) if ch == '#'}
- considerations=[((0,-1),(1,-1),(-1,-1)),
- ((0,1),(1,1),(-1,1)),
- ((-1,0),(-1,1),(-1,-1)),
- ((1,0),(1,1),(1,-1))]
- def vis():
- xmin=min(elf[0] for elf in elfs)
- ymin=min(elf[1] for elf in elfs)
- xmax=max(elf[0] for elf in elfs)
- ymax=max(elf[1] for elf in elfs)
- empties=0
- for y in range(ymin, ymax+1):
- for x in range(xmin, xmax+1):
- if (x,y) in elfs:
- print("#", end="")
- else:
- print(".", end="")
- empties += 1
- print()
- print(empties)
- for rnd in range(10000):
- props={}
- colls=Counter()
- for elf in elfs:
- if any((elf[0]+dx, elf[1]+dy) in elfs
- for dx in (-1,0,1)
- for dy in (-1,0,1) if dx != 0 or dy!=0):
- for cns in (considerations[(rnd+i)%4] for i in range(4)):
- if all((elf[0]+dx, elf[1]+dy) not in elfs for dx,dy in cns):
- props[elf]=(elf[0]+cns[0][0],elf[1]+cns[0][1])
- colls[props[elf]]+=1
- break
- else:
- props[elf]=elf
- else:
- props[elf]=elf
- elfs.clear()
- moves=0
- for elf, prop in props.items():
- if colls[prop] <= 1:
- if elf != prop:
- moves += 1
- elfs.add(prop)
- else:
- elfs.add(elf)
- if moves == 0:
- break
- #vis()
- print (rnd+1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement