Task: One-dimensional cellular automata





Java: public class Life{
	public static void main(String[] args) throws Exception{
		String start= "_###_##_#_#_#_#__#__";
		int numGens = 10;
		for(int i= 0; i < numGens; i++){
			System.out.println("Generation " + i + ": " + start);
			start= life(start);
		}
	}

	public static String life(String lastGen){
		String newGen= "";
		for(int i= 0; i < lastGen.length(); i++){
			int neighbors= 0;
			if (i == 0){
				neighbors= lastGen.charAt(1) == '#' ? 1 : 0;
			} else if (i == lastGen.length() - 1){
				neighbors= lastGen.charAt(i - 1) == '#' ? 1 : 0;
			} else{
				neighbors= getNeighbors(lastGen.substring(i - 1, i + 2));
			}

			if (neighbors == 0){
				newGen+= "_";
			}
			if (neighbors == 1){
				newGen+= lastGen.charAt(i);
			}
			if (neighbors == 2){
				newGen+= lastGen.charAt(i) == '#' ? "_" : "#";
			}
		}
		return newGen;
	}

	public static int getNeighbors(String group){
		int ans= 0;
		if (group.charAt(0) == '#') ans++;
		if (group.charAt(2) == '#') ans++;
		return ans;
	}
}





COBOL:  Identification division.                                        
 Program-id. rc-1d-cell.                                         
 
 Data division.                                                  
 Working-storage section.                                        
 

 01 max-gens            pic  999  value   9.                     
 01 state-width         pic   99  value  20.                     
 01 state-table-init    pic x(20) value ".@@@.@@.@.@.@.@..@..".  
 01 alive               pic    x  value "@".                     
 01 dead                pic    x  value ".".                     
 

 01 state-gen           pic  999  value   0.                     
 01 state-row.                                                  
    05 state-row-gen   pic zz9.                                
    05 filler          pic  xx   value ": ".                   
    05 state-table.                                            
        10 state-cells pic   x   occurs 20 times.              
 

 01 new-state-table.                                            
    05 new-state-cells pic   x   occurs 20 times.              
 

 01 cell-index          pic   99.                               
    88 at-beginning    value  1.                               
    88 is-inside       values 2 thru 19.                       
    88 at-end          value 20.                               
 

 01 neighbor-count-def.                          
   03 neighbor-count      pic   9.
     88 is-comfy        value 1.                    
     88 is-ripe         value 2.                    
 
 Procedure division.                                
     Perform Init-state-table.                      
     Perform max-gens times                         
         perform Display-row                        
         perform Next-state                         
     end-perform.                                   
     Perform Display-row.                           
     Stop run.                                      
 
 Display-row.                                       
     Move state-gen to state-row-gen.     
     Display state-row.                   
 

 Next-state.                              
     Add 1 to state-gen.                  
     Move state-table to new-state-table. 
 
     Perform with test after              
         varying cell-index from 1 by 1   
         until at-end                     
         perform Count-neighbors          
         perform Die-off                             
         perform New-births                          
     end-perform                                     
 
     move new-state-table to state-table.            
 

 Die-off.                                            
     if state-cells(cell-index) =                    
     alive and not is-comfy    
         then move dead to new-state-cells(cell-index)           
     end-if                                                      
     .                                                           
 

 New-births.                                                     
     if state-cells(cell-index) = dead and is-ripe
         then move alive to new-state-cells(cell-index)          
     end-if                                                      
    .                                                           

 Count-neighbors.                                                
     Move 0 to neighbor-count                        
     if at-beginning or at-end then                              
         add 1 to neighbor-count                      
     else                                                        
       if is-inside and state-cells(cell-index - 1) = alive        
       then                                                        
           add 1 to neighbor-count                     
       end-if                                                      
       if is-inside and state-cells(cell-index + 1) = alive        
       then                                                        
           add 1 to neighbor-count                     
       end-if                                                       
     end-if                                                        
     .                                                             
 



 
 Init-state-table.                                                 
     Perform with test after                    
         varying cell-index from 1 by 1         
         until at-end                           
         move state-table-init(cell-index:1)    
           to state-cells(cell-index)           
      end-perform
      .