Advertisement
Guest User

Day 24 Part 2

a guest
Dec 24th, 2019
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.91 KB | None | 0 0
  1. field=[[[1,0,1,0,1],[0,1,0,0,0],[0,0,0,1,0],[0,1,1,1,0],[1,1,1,0,1],0]] #input: 1==bug
  2. #field=[[[0,0,0,0,1],[1,0,0,1,0],[1,0,0,1,1],[0,0,1,0,0],[1,0,0,0,0],0]] #test data
  3. t=0
  4.  
  5. while t<200:
  6.   t+=1
  7.   if t%2: #every odd tick is first time bugs can go out a layer
  8.     field.reverse()
  9.     field.append([[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],-t//2]) #add a new outer layer to the front...
  10.     field.reverse()
  11.     field.append([[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],t//2+1]) #and a new inner one to the back
  12.   new=[] #start empty for next iteration
  13.   for l in range(len(field)):
  14.     new.append([[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],l-len(field)//2]) #add a blank field
  15.     #print(new)
  16.     for i in range(5):
  17.       for j in range(5):
  18.         if i==2 and j==2: #center square! DON'T DO THIS
  19.           continue
  20.         lcount=field[l][i][j] #start with the value of the field
  21.         lcount+=int(i!=0 and field[l][i-1][j]) #square left (NOT LEFTMOST COLUMN)
  22.         lcount+=int(j!=0 and field[l][i][j-1]) #square up (NOT TOPMOST ROW)
  23.         lcount+=int(i!=4 and field[l][i+1][j]) #square right (NOT RIGHTMOST ROW)
  24.         lcount+=int(j!=4 and field[l][i][j+1]) #square down (NOT BOTTOMMOST ROW)
  25.         if i==0 and l!=0: #for the leftmost column...
  26.           lcount+=field[l-1][1][2] #go up a level and look at the square left of center
  27.         if i==4 and l!=0: #repeat for rightmost...
  28.           lcount+=field[l-1][3][2]
  29.         if j==0 and l!=0: #topmost...
  30.           lcount+=field[l-1][2][1]
  31.         if j==4 and l!=0: #and bottommost.
  32.           lcount+=field[l-1][2][3]
  33.         if i==1 and j==2 and (l!=len(field)-1): #for the square left of center...
  34.           lcount+=(field[l+1][0][0]+field[l+1][0][1]+field[l+1][0][2]+field[l+1][0][3]+field[l+1][0][4]) #go down a level and look at the leftmost column
  35.         if i==3 and j==2 and (l!=len(field)-1): #repeat for right of center...
  36.           lcount+=(field[l+1][4][0]+field[l+1][4][1]+field[l+1][4][2]+field[l+1][4][3]+field[l+1][4][4])
  37.         if i==2 and j==1 and (l!=len(field)-1): #above center...
  38.           lcount+=(field[l+1][0][0]+field[l+1][1][0]+field[l+1][2][0]+field[l+1][3][0]+field[l+1][4][0])
  39.         if i==2 and j==3 and (l!=len(field)-1): #and below center
  40.           lcount+=(field[l+1][0][4]+field[l+1][1][4]+field[l+1][2][4]+field[l+1][3][4]+field[l+1][4][4])
  41.         if lcount==2: #if two total spaces have bugs, including the original square...
  42.           new[l][i][j]=1 #the square has bugs!!! yay!!!
  43.         if (lcount==1 and field[l][i][j]==0): #but if the square didn't have bugs before, and this total is one...
  44.           new[l][i][j]=1 #still has bugs!!!
  45.   field=new #update the whole field
  46.   print(t)
  47.  
  48. bcount=0 #start with no bugs
  49. for l in range(len(field)): #and just...
  50.   for i in range(5): #loop over...
  51.     for j in range(5): #everything
  52.       bcount+=field[l][i][j]
  53. print(bcount) #0ω0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement