[QUIZ][SOLUTION] Cellular Automata (#134)

J

James Koppel

Here's my simple solution. Note that, rather than parsing options, it merel=
y expects rule, steps, state in that order.=0A=0Arequire 'enumerator'=0A=0A=
def step(state,rule)=0A cur_arr =3D ([0,0] + state.split(//) + [0,0]).map{=
|s| s.to_i}=0A next_arr =3D []=0A cur_arr.each_cons(3) do |neighborhood|=
=0A ##Checks the (neighborhood+1)th bit of rule=0A ##E.g.: If neighbo=
rhood is [0,1,0], then inserts a 1 if the third bit of rule is on=0A if =
(2**(neighborhood.join.to_i(2)))&rule !=3D 0=0A next_arr << 1=0A el=
se=0A next_arr << 0=0A end=0A end=0A next_arr.join=0Aend=0A=0Arul=
e =3D ARGV[0].chomp.to_i=0Asteps =3D ARGV[1].chomp.to_i=0Astate =3D ARGV[2]=
chomp=0A=0Aresult =3D [state]=0A=0Asteps.times do=0A state =3D step(state=
, rule)=0A result << state=0Aend=0A=0Alength =3D result.last.size=0A=0Aout=
put =3D result.map {|row|=0A ([0]*((length-row.size)/2)+row.split(//)).map=
{|b|=0A b=3D=3D"1" ? "X" : " "}.join=0A }=0A=0Aputs output=0A=0A=0A=0A =
=0A__________________________________________________________________=
__________________=0ALooking for a deal? Find great prices on flights and h=
otels with Yahoo! FareChase.=0Ahttp://farechase.yahoo.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top