Advertisement
Quatch

Untitled

May 13th, 2015
714
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.71 KB | None | 0 0
  1. http://www.reddit.com/r/askscience/comments/35uljq/if_i_wanted_to_randomly_find_someone_in_an/cr89r0o
  2.  
  3.  
  4. #options:
  5.  
  6.  
  7. #initialize:
  8.  
  9.  
  10.  
  11.  
  12. simulate=function(move2=F,verbose=F){
  13.     die=function(x=1){floor(runif(x,1,11))}
  14.     coords=function(who){pos=which(park==who);y=ceiling(pos/10);x=pos%%10;if(x==0)x=10;return(c(x,y))}
  15.    
  16.     park= matrix(data=".",nrow=10,ncol=10,byrow=T)
  17.    
  18.     #place A
  19.     posA=die(2)
  20.     park[posA[1],posA[2]]="A"
  21.  
  22.     #place B
  23.     overlap=T
  24.     while(overlap){
  25.         posB=die(2)
  26.         if(! all(posB==posA)){overlap=F}
  27.         }
  28.     park[posB[1],posB[2]]="B"
  29.  
  30.  
  31.     #plotpark=function(){for(i in 1:10){for(j in 1:10){cat(park[park$x==i & park$y==j,"who"], " ") };print("")}}#print()
  32.     plotpark=function(){print(park)}
  33.     if(verbose)plotpark()
  34.  
  35.     found=FALSE
  36.     counter=0
  37.     if(all(posB==posA)){
  38.         warning("+++ REDO FROM START +++               >> collision")
  39.         found=T
  40.         }
  41.  
  42.     while(!found){
  43.         counter=counter+1
  44.        
  45.         # ####  A
  46.         #get pos:
  47.         pos=coords("A")
  48.         x=pos[1]
  49.         y=pos[2]
  50.         if(verbose==2)cat("A: ",x,y,"\n")
  51.         park[x,y]=counter
  52.        
  53.         #valid moves:
  54.         dirs=c("U","UR","R","DR","D","DL","L","UL")
  55.         if(y==1)dirs=grep("[^L]$",dirs,value=T)
  56.         if(y==10)dirs=grep("[^R]$",dirs,value=T)
  57.         if(x==1)dirs=grep("^[^U]",dirs,value=T)
  58.         if(x==10)dirs=grep("^[^D]",dirs,value=T)
  59.        
  60.         if(verbose==2)print(dirs)
  61.        
  62.         dirs=sample(dirs,1)
  63.        
  64.         if(verbose==2)print(dirs)
  65.        
  66.         if(length(grep("D",dirs))==1) x=x+1
  67.         if(length(grep("U",dirs))==1) x=x-1
  68.         if(length(grep("R",dirs))==1) y=y+1
  69.         if(length(grep("L",dirs))==1) y=y-1
  70.        
  71.         if(verbose==2)cat(x,y,"\n")
  72.        
  73.         newA=c(x,y)
  74.        
  75.         # ####  B
  76.         #get pos:
  77.         pos=coords("B")
  78.         x=pos[1]
  79.         y=pos[2]
  80.         if(verbose==2)cat("B: ",x,y,"\n")
  81.         park[x,y]=counter
  82.        
  83.         #valid moves:
  84.         dirs=c("U","UR","R","DR","D","DL","L","UL")
  85.         if(y==1)dirs=grep("[^L]$",dirs,value=T)
  86.         if(y==10)dirs=grep("[^R]$",dirs,value=T)
  87.         if(x==1)dirs=grep("^[^U]",dirs,value=T)
  88.         if(x==10)dirs=grep("^[^D]",dirs,value=T)
  89.        
  90.         if(verbose==2)print(dirs)
  91.        
  92.         dirs=sample(dirs,1)
  93.        
  94.         if(verbose==2)print(dirs)
  95.        
  96.         if(length(grep("D",dirs))==1) x=x+1
  97.         if(length(grep("U",dirs))==1) x=x-1
  98.         if(length(grep("R",dirs))==1) y=y+1
  99.         if(length(grep("L",dirs))==1) y=y-1
  100.        
  101.         if(verbose==2)cat(x,y,"\n")
  102.        
  103.         newB=c(x,y)
  104.        
  105.        
  106.         if(all(newA==newB))
  107.             {
  108.             found=T
  109.             park[x,y]="X"
  110.             }
  111.             else{
  112.             park[newA[1],newA[2]]="A"
  113.             park[newB[1],newB[2]]="B"
  114.             }
  115.        
  116.         #Search nieghbouring squares
  117.        
  118.         if(verbose==2)plotpark()
  119.     }
  120.     if(verbose)plotpark()
  121.     if(verbose)cat("\n\nFound in: ",counter)
  122. return(counter)
  123. }
  124.  
  125. simulate()
  126.  
  127. nruns=10000
  128.  
  129. dataA=rep(0,nruns)
  130. dataAB=rep(0,nruns)
  131. for(i in 1:nruns)dataA[i]=simulate()
  132. for(i in 1:nruns)dataAB[i]=simulate(T)
  133.  
  134.  
  135. summary(data)
  136. summary(dataAB)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement