From: Albert Ng Date: 2007-03-11T22:23:49+09:00 Subject: Re: [QUIZ] SimFrost (#117) ------=_Part_17925_9518028.1173619425212 Content-Type: multipart/alternative; boundary="----=_Part_17926_15519589.1173619425212" ------=_Part_17926_15519589.1173619425212 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable Content-Disposition: inline right, code got mangled, here's the file On 3/11/07, Christoffer Lern=F6 wrote: > > Fun Quiz! > > Here's my solution (ASCII only I'm afraid). I use a wrapper object to > handle a neighbourhood. Since I only allow access one at a time, I > simply recycled a single object. This is unsafe in the general case, > but works nicely here I think. > > My code for rotations are explicit than calculated, but I find it > easier to read and understand what is going on. > > > #!/usr/bin/env ruby -w > > module SimFrost > > class FrostGrid > > attr_reader :data > > def initialize(width, height, percent) > @width, @height =3D width, height > @data =3D Array.new(height) { Array.new(width) { rand * 100 < > percent ? '.' : ' ' }.join } > self[width / 2, height / 2] =3D ?* > @neighbourhood =3D Neighbourhood.new(self) > @tick =3D 0 > end > > def [](x, y) > @data[y % @height][x % @width] > end > > def []=3D(x, y, value) > @data[y % @height][x % @width] =3D value > end > > def tick > @tick +=3D 1 > vapour =3D 0 > each_neighbourhood do |neighbourhood| > neighbourhood.mutate > vapour +=3D 1 if neighbourhood.contains_vapour? > end > vapour > end > > def draw_freeze > draw # Before we start freezing > draw while tick > 0 > draw # After everything is frozen > end > > def draw > puts "Tick: #{@tick}" > puts "+" + "-" * @width + "+" > @data.each { |row| puts "|#{row}|" } > puts "+" + "-" * @width + "+" > end > > def each_neighbourhood > @tick.step(@tick + @height, 2) do |y| > @tick.step(@tick + @width, 2) do |x| > yield @neighbourhood[x, y] > end > end > end > > end > > class Neighbourhood > > 2.times do |y| > 2.times do |x| > class_eval "def xy#{x}#{y}; @grid[@x + #{x}, @y + #{y}]; end" > class_eval "def xy#{x}#{y}=3D(v); @grid[@x + #{x}, @y + #{y}] > =3D v; end" > end > end > > def initialize(grid) > @grid =3D grid > end > > def [](x, y) > @x, @y =3D x, y > self > end > > def ccw90 > self.xy00, self.xy10, self.xy01, self.xy11 =3D xy10, xy11, xy00, > xy01 > end > > def cw90 > self.xy00, self.xy10, self.xy01, self.xy11 =3D xy01, xy00, xy11, > xy10 > end > > def each_cell > @y.upto(@y + 1) { |y| @x.upto(@x + 1) { |x| yield x, y } } > end > > def contains?(c) > each_cell { |x, y| return true if @grid[x, y] =3D=3D c } > false > end > > def contains_ice? > contains? ?* > end > > def contains_vapour? > contains? ?. > end > > def freeze > each_cell { |x, y| @grid[x, y] =3D ?* if @grid[x, y] =3D=3D ?. } > end > > def rotate_random > rand < 0.5 ? ccw90 : cw90 > end > > def mutate > contains_ice? ? freeze : rotate_random > end > > def to_s > "+--+\n+" << xy00 << xy10 << "+\n+" << xy01 << xy11 << "+\n+--+" > end > end > > > def SimFrost.simfrost(width, height, percent =3D 50) > FrostGrid.new(width, height, percent).draw_freeze > end > > end > > if __FILE__ =3D=3D $PROGRAM_NAME > SimFrost::simfrost(40, 20, 35) > end > > > > ------=_Part_17926_15519589.1173619425212 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline right, code got mangled, here's the file

On 3/11/07, Christoffer Lern=F6= <lerno@dragonascendant.com= > wrote:
Fun Quiz!<= br>
Here's my solution (ASCII only I'm afraid). I use a wrapper = object to
handle a neighbourhood. Since I only allow access one at a time, I
s= imply recycled a single object. This is unsafe in the general case,
but = works nicely here I think.

My code for rotations are explicit than c= alculated, but I find it
easier to read and understand what is going on.


#!/usr/bin/e= nv ruby -w

module SimFrost

   class FrostGrid
     attr_reader :data

    = ; def initialize(width, height, percent)
     &= nbsp; @width, @height =3D width, height
       @data =3D Array.new(height) { Arra= y.new(width) { rand * 100 <
percent ? '.' : ' ' }.joi= n }
       self[width / 2, height / 2] =3D= ?*
       @neighbourhood =3D Neighbourhoo= d.new(self)
       @tick =3D 0
     end

     def [](x, = y)
       @data[y % @height][x % @width]     end

     def []=3D(x,= y, value)
       @data[y % @height][x % @= width] =3D value
     end

   &= nbsp; def tick
       @tick +=3D 1
       vapour =3D 0
   =     each_neighbourhood do |neighbourhood|
  &nb= sp;      neighbourhood.mutate
   = ;      vapour +=3D 1 if neighbourhood.contains_vap= our?
       end
    = ;   vapour
     end

  &nb= sp;  def draw_freeze
       draw # Before we start freezing       draw while tick > 0
  = ;     draw # After everything is frozen
  =    end

     def draw
  &n= bsp;    puts "Tick: #{@tick}"
   = ;    puts "+" + "-" * @width + "+&q= uot;
       @data.each { |row| puts "|#{r= ow}|" }
       puts "+" + &= quot;-" * @width + "+"
     end
     def each_neighbourhood
   &nbs= p;   @tick.step(@tick + @height, 2) do |y|
         @tick.step(@tick + @wi= dth, 2) do |x|
         &nb= sp; yield @neighbourhood[x, y]
       = ;  end
       end
  &nbs= p;  end

   end

   class Neighbourhoo= d

     2.times do |y|
    = ;   2.times do |x|
         class_eval &quo= t;def xy#{x}#{y}; @grid[@x + #{x}, @y + #{y}]; end"
  &nb= sp;      class_eval "def xy#{x}#{y}=3D(v); @g= rid[@x + #{x}, @y + #{y}]
=3D v; end"
    &n= bsp;  end
     end

   &nb= sp; def initialize(grid)
       @grid =3D grid
  &nbs= p;  end

     def [](x, y)
  &n= bsp;    @x, @y =3D x, y
     &nb= sp; self
     end

     de= f ccw90
       self.xy00, self.xy10, self.= xy01, self.xy11 =3D xy10, xy11, xy00,
xy01
     e= nd

     def cw90
     =   self.xy00, self.xy10, self.xy01, self.xy11 =3D xy01, xy00, xy11,
= xy10
     end

     def ea= ch_cell
       @y.upto(@y + 1) { |y| @x.up= to(@x + 1) { |x| yield x, y } }
     end

     def contains?(c)
   &nbs= p;   each_cell { |x, y| return true if @grid[x, y] =3D=3D c }
=        false
     end<= br>
     def contains_ice?
   &nbs= p;   contains? ?*
     end

 &n= bsp;   def contains_vapour?
       contains? ?.
   &nbs= p; end

     def freeze
   &nbs= p;   each_cell { |x, y| @grid[x, y] =3D ?* if @grid[x, y] =3D=3D = ?. }
     end

     def ro= tate_random
       rand < 0.5 ? ccw90 := cw90
     end

     def mutate
      = contains_ice? ? freeze : rotate_random
     end
=
     def to_s
     &nbs= p; "+--+\n+" << xy00 << xy10 << "+\n+"= ; << xy01 << xy11 << "+\n+--+"
     end
   end


  = ; def SimFrost.simfrost(width, height, percent =3D 50)
   = ;  FrostGrid.new(width, height, percent).draw_freeze
   e= nd

end

if __FILE__ =3D=3D $PROGRAM_NAME
   SimFr= ost::simfrost(40, 20, 35)
end




------=_Part_17926_15519589.1173619425212-- ------=_Part_17925_9518028.1173619425212 Content-Type: application/octet-stream; name=snowflake.rb Content-Transfer-Encoding: base64 X-Attachment-Id: f_ez5j2szu Content-Disposition: attachment; filename="snowflake.rb" Y2xhc3MgU25vd2ZsYWtlDQogIGF0dHJfcmVhZGVyIDpncmlkLCA6dmFwb3INCiAgZGVmIGluaXRp YWxpemUgKHlfc2l6ZT0yNCwgeF9zaXplPTMwLCB2YXBvcl9wZXJjZW50PTMwKQ0KICAgIEB5X3Np emU9eV9zaXplLzIqMiAgICAjdGhpcyBzaG91bGQgdGFrZSBjYXJlIG9mIHRob3NlIG9kZCBudW1i ZXJzDQogICAgQHhfc2l6ZT14X3NpemUvMioyDQogICAgQHZhcG9yX3BlcmNlbnQ9dmFwb3JfcGVy Y2VudA0KICAgIEB2YWN1dW09IiAiDQogICAgQHZhcG9yPSIrIg0KICAgIEBpY2U9IioiDQogICAg QG9mZnNldD0xDQogICAgY3JlYXRlX2dyaWQNCiAgZW5kDQogIA0KICBkZWYgY3JlYXRlX2dyaWQN CiAgICBAZ3JpZD1BcnJheS5uZXcoQHlfc2l6ZSl7QXJyYXkubmV3KEB4X3NpemUpfQ0KICAgIEBn cmlkLmNvbGxlY3QhIGRvIHxyb3d8DQogICAgICByb3cuY29sbGVjdCEgZG8gfHNxdWFyZXwNCiAg ICAgICAgcmFuZCgxMDApIDwgQHZhcG9yX3BlcmNlbnQgPyBAdmFwb3IgOiBAdmFjdXVtDQogICAg ICBlbmQNCiAgICBlbmQNCiAgICBAZ3JpZFtAeV9zaXplLzJdW0B4X3NpemUvMl09QGljZQ0KICBl bmQNCiAgDQogIGRlZiBjaGVja19uZWlnaGJvcmhvb2RzDQogICAgQG9mZnNldCA9IChAb2Zmc2V0 ICsxKSUyDQogICAgQGdyaWQuY29sbGVjdCF7fHJvd3wgcm93LnB1c2gocm93LnNsaWNlISgwKSl9 LnB1c2goQGdyaWQuc2xpY2UhKDApKSBpZiBAb2Zmc2V0ID09IDEgICN0b3J1cyBtZSENCiAgICAo MC4uLkB5X3NpemUpLnN0ZXAoMikgZG8gfGl8DQogICAgICAoMC4uLkB4X3NpemUpLnN0ZXAoMikg ZG8gfGp8DQogICAgICAgIG5laWdoYm9yaG9vZD1bQGdyaWRbaV1bal0sIEBncmlkW2ldW2orMV0s IEBncmlkW2krMV1bal0sIEBncmlkW2krMV1baisxXV0NCiAgICAgICAgaWYgIW5laWdoYm9yaG9v ZC5pbmNsdWRlPyhAdmFwb3IpDQogICAgICAgIGVsc2lmIG5laWdoYm9yaG9vZC5pbmNsdWRlPyhA aWNlKSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICN0aGVyZSdzIGdvdCB0byBi ZSBhIHJ1YnllciB3YXkgb2YgZG9pbmcgdGhpcy4uLg0KICAgICAgICAgIEBncmlkW2ldW2pdICAg ICAgICAgPUBpY2UgaWYgIEBncmlkW2ldW2pdICAgICAgICAgICA9PSBAdmFwb3IgICAgICN0b3Ag bGVmdCBjb3JuZXINCiAgICAgICAgICBAZ3JpZFtpXVtqKzFdICAgICA9QGljZSBpZiAgQGdyaWRb aV1baisxXSAgICAgICA9PSBAdmFwb3IgICAgICNvbmUgcmlnaHQNCiAgICAgICAgICBAZ3JpZFtp KzFdW2pdICAgICA9QGljZSBpZiAgQGdyaWRbaSsxXVtqXSAgICAgICA9PSBAdmFwb3IgICAgICNv bmUgZG93bg0KICAgICAgICAgIEBncmlkW2krMV1baisxXSA9QGljZSBpZiAgQGdyaWRbaSsxXVtq KzF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HVkZT8ocy52YXBvcil9LmluY2x1ZGU/KHRydWUpDQogIHB1dHMgcw0KICA1LnRpbWVzIGRv IHB1dHMgZW5kDQogIHNsZWVwKDAuMSkNCiAgcy5jaGVja19uZWlnaGJvcmhvb2RzDQplbmQNCiAg cHV0cyBzDQoNCj1iZWdpbiBSdW5uaW5nIHRocnUgdGhlIGZpbmlzaCBsaW5lDQoNCiAgICAgICAg ICAgICAgICoqDQogICAgICAgKiAgICAgICAqDQogICAgICAgKiAgICogICoNCiAgICAgICAgKiAg ICoqDQogICAgICAgICAqKiAqKg0KICAgICAgICAgICoqDQogICAgICAgICoqKiAqDQogICAgICAg ICAgKioqDQogICAgICAgICAgICogKg0KICAgICAgICAgICAgKiAqDQogICAgICAgICAgICAgKg0K ICAgICAgICAgICAgICoNCj1lbmQ= ------=_Part_17925_9518028.1173619425212--