Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2022
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.63 KB | Source Code | 0 0
  1. from collections import Counter
  2.  
  3. infile=open("ash.txt")
  4. elfs={(x,y)
  5.        for y, line in enumerate(infile)
  6.        for x,ch in enumerate(line) if ch == '#'}
  7.  
  8. considerations=[((0,-1),(1,-1),(-1,-1)),
  9.                 ((0,1),(1,1),(-1,1)),
  10.                 ((-1,0),(-1,1),(-1,-1)),
  11.                 ((1,0),(1,1),(1,-1))]
  12.  
  13. def vis():
  14.     xmin=min(elf[0] for elf in elfs)
  15.     ymin=min(elf[1] for elf in elfs)
  16.     xmax=max(elf[0] for elf in elfs)
  17.     ymax=max(elf[1] for elf in elfs)
  18.     empties=0
  19.     for y in range(ymin, ymax+1):
  20.         for x in range(xmin, xmax+1):
  21.             if (x,y) in elfs:
  22.                 print("#", end="")
  23.             else:
  24.                 print(".", end="")
  25.                 empties += 1
  26.         print()
  27.     print(empties)
  28.    
  29. for rnd in range(10000):
  30.     props={}
  31.     colls=Counter()
  32.     for elf in elfs:
  33.         if any((elf[0]+dx, elf[1]+dy) in elfs
  34.                for dx in (-1,0,1)
  35.                for dy in (-1,0,1) if dx != 0 or dy!=0):
  36.             for cns in (considerations[(rnd+i)%4] for i in range(4)):
  37.                 if all((elf[0]+dx, elf[1]+dy) not in elfs for dx,dy in cns):
  38.                     props[elf]=(elf[0]+cns[0][0],elf[1]+cns[0][1])
  39.                     colls[props[elf]]+=1
  40.                     break
  41.             else:
  42.                 props[elf]=elf
  43.         else:
  44.             props[elf]=elf
  45.     elfs.clear()
  46.     moves=0
  47.     for elf, prop in props.items():
  48.         if colls[prop] <= 1:
  49.             if elf != prop:
  50.                 moves += 1
  51.             elfs.add(prop)
  52.         else:
  53.             elfs.add(elf)
  54.     if moves == 0:
  55.         break
  56. #vis()
  57. print (rnd+1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement