conwy 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 93a9a3d96204158abe2531de9708ad16014bfac8
4
+ data.tar.gz: b508b491900f10117ace7762f34a75cfdc85fb2c
5
+ SHA512:
6
+ metadata.gz: eb076a351ed7100b5e58486dce0c9fc60e82c784b9d54a6405e829d81de353c084acb7036cdbc07e19a682a75b2921c51bcdfc9ae04b196e2ff6da2e5cb8d5f8
7
+ data.tar.gz: 3d2ac00abd1bfb5c254f8234a52f28a010ef85f3c06544844f63b6f5c3dea57d94f36a3da25cef958f5dafde34e3e9af91010bf1250544e17a10cd73f7225641
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in conwy.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Max Holder
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,63 @@
1
+ # Conwy
2
+
3
+ This gem plays [Conway's Game of
4
+ Life](https://en.wikipedia.org/wiki/Conway's_Game_of_Life).
5
+
6
+ The name comes from [a town in Wales](https://en.wikipedia.org/wiki/Conwy)
7
+ because `conway` was already taken.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'conwy'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install conwy
24
+
25
+ ## Usage
26
+
27
+ Conwy gets it initial state from stdin and then prints the next iteration:
28
+
29
+ ```
30
+ $ echo "010\n010\n010" > blinker
31
+ $ cat blinker
32
+ 010
33
+ 010
34
+ 010
35
+ $ cat blinker | conway
36
+ 000
37
+ 111
38
+ 000
39
+ ```
40
+
41
+ You can also provide a number of iterations to perform before printing:
42
+
43
+ ```
44
+ $ echo "00100\n10100\n01100\n00000" > glider
45
+ $ cat glider
46
+ 00100
47
+ 10100
48
+ 01100
49
+ 00000
50
+ $ cat glider | conwy 4
51
+ 00000
52
+ 00010
53
+ 01010
54
+ 00110
55
+ ```
56
+
57
+ ## Contributing
58
+
59
+ 1. Fork it ( https://github.com/mxhold/conwy/fork )
60
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
61
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
62
+ 4. Push to the branch (`git push origin my-new-feature`)
63
+ 5. Create a new Pull Request
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'conwy'
4
+
5
+ iterations = (ARGV.shift || 1).to_i
6
+ initial_input = ARGF.read
7
+
8
+ output = (1..iterations).reduce(initial_input) do |input, _|
9
+ Conwy.run(input)
10
+ end
11
+ puts output
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'conwy/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'conwy'
8
+ spec.version = Conwy::VERSION
9
+ spec.authors = ['Max Holder']
10
+ spec.email = ['mxhold@gmail.com']
11
+ spec.summary = %( Conway's Game of Life )
12
+ spec.description = %( a simple implementation of Conway's Game of Life in Ruby )
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(/^bin\//) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(/^(test|spec|features)\//)
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.7'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'rspec'
24
+ end
@@ -0,0 +1,10 @@
1
+ require 'delegate'
2
+ require 'conwy/version'
3
+ require 'conwy/cell'
4
+ require 'conwy/world'
5
+
6
+ module Conwy
7
+ def self.run(string)
8
+ World.new(string).next.to_s
9
+ end
10
+ end
@@ -0,0 +1,19 @@
1
+ module Conwy
2
+ class Cell
3
+ def initialize(alive:)
4
+ @alive = alive
5
+ end
6
+
7
+ def alive?
8
+ @alive
9
+ end
10
+
11
+ def next(alive_neighbors:)
12
+ if alive_neighbors == 3 || (alive? && alive_neighbors == 2)
13
+ self.class.new(alive: true)
14
+ else
15
+ self.class.new(alive: false)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module Conwy
2
+ VERSION = '1.0.1'
3
+ end
@@ -0,0 +1,87 @@
1
+ module Conwy
2
+ class World
3
+ def initialize(cell_string)
4
+ @cell_grid = CellGrid.from_s(cell_string)
5
+ end
6
+
7
+ def to_s
8
+ cell_grid.to_s
9
+ end
10
+
11
+ def next
12
+ self.class.new(cell_grid.next.to_s)
13
+ end
14
+
15
+ private
16
+
17
+ attr_reader :cell_grid
18
+
19
+ class CellGrid < SimpleDelegator
20
+ def self.from_s(string)
21
+ new(string.split("\n").map do |row|
22
+ row.each_char.map do |char|
23
+ Cell.new(alive: char == '1')
24
+ end
25
+ end)
26
+ end
27
+
28
+ def to_s
29
+ map do |row|
30
+ row.map do |cell|
31
+ cell.alive? ? 1 : 0
32
+ end.join
33
+ end.join("\n")
34
+ end
35
+
36
+ def next
37
+ self.class.new(map.with_index do |row, x|
38
+ row.map.with_index do |cell, y|
39
+ cell.next(alive_neighbors: alive_neighbors(Coordinate.new(x, y)))
40
+ end
41
+ end)
42
+ end
43
+
44
+ private
45
+
46
+ def alive_neighbors(coordinate)
47
+ neighbors(coordinate).select(&:alive?).size
48
+ end
49
+
50
+ def neighbors(coordinate)
51
+ coordinate.neighbors.map do |neighbor_coordinate|
52
+ at(neighbor_coordinate)
53
+ end.compact
54
+ end
55
+
56
+ def at(coordinate)
57
+ fetch(coordinate.x, {})[coordinate.y] unless coordinate.negative?
58
+ end
59
+
60
+ class Coordinate < Struct.new(:x, :y)
61
+ def negative?
62
+ x < 0 || y < 0
63
+ end
64
+
65
+ def neighbors
66
+ neighbor_shifts.map do |x, y|
67
+ shift(x, y)
68
+ end
69
+ end
70
+
71
+ private
72
+
73
+ def shift(x_shift, y_shift)
74
+ self.class.new(x + x_shift, y + y_shift)
75
+ end
76
+
77
+ def neighbor_shifts
78
+ [
79
+ [-1, -1], [-1, 0], [-1, 1],
80
+ [ 0, -1], [ 0, 1],
81
+ [ 1, -1], [ 1, 0], [ 1, 1],
82
+ ]
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'bin/conwy' do
4
+ context 'given a string of cells from stdin' do
5
+ it 'returns the next iteration of the game' do
6
+ expect(
7
+ `echo "010\n010\n010" | bundle exec ./bin/conwy`
8
+ ).to eql("000\n111\n000\n")
9
+ end
10
+ context 'and a number' do
11
+ it 'does the provided number of iterations and prints the result' do
12
+ expect(
13
+ `echo "010\n010\n010" | bundle exec ./bin/conwy 2`
14
+ ).to eql("010\n010\n010\n")
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ describe Conwy::Cell do
4
+ describe '#next' do
5
+ context 'alive cell' do
6
+ subject { described_class.new(alive: true) }
7
+ context '1 alive neighbor' do
8
+ it 'returns a dead cell' do
9
+ expect(subject.next(alive_neighbors: 1)).not_to be_alive
10
+ end
11
+ end
12
+ context '2 alive neighbors' do
13
+ it 'returns an alive cell' do
14
+ expect(subject.next(alive_neighbors: 2)).to be_alive
15
+ end
16
+ end
17
+ context '3 alive neighbors' do
18
+ it 'returns an alive cell' do
19
+ expect(subject.next(alive_neighbors: 3)).to be_alive
20
+ end
21
+ end
22
+ context '4 alive neighbor' do
23
+ it 'returns a dead cell' do
24
+ expect(subject.next(alive_neighbors: 4)).not_to be_alive
25
+ end
26
+ end
27
+ end
28
+ context 'dead cell' do
29
+ subject { described_class.new(alive: false) }
30
+ context '1 alive neighbor' do
31
+ it 'returns a dead cell' do
32
+ expect(subject.next(alive_neighbors: 1)).not_to be_alive
33
+ end
34
+ end
35
+ context '2 alive neighbors' do
36
+ it 'returns a dead cell' do
37
+ expect(subject.next(alive_neighbors: 2)).not_to be_alive
38
+ end
39
+ end
40
+ context '3 alive neighbors' do
41
+ it 'returns an alive cell' do
42
+ expect(subject.next(alive_neighbors: 3)).to be_alive
43
+ end
44
+ end
45
+ context '4 alive neighbor' do
46
+ it 'returns a dead cell' do
47
+ expect(subject.next(alive_neighbors: 4)).not_to be_alive
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe Conwy do
4
+ it 'has a version number' do
5
+ expect(Conwy::VERSION).not_to be nil
6
+ end
7
+
8
+ describe '.run' do
9
+ context 'given a string' do
10
+ it 'returns the outcome' do
11
+ expect(described_class.run("010\n010\n010")).to eql "000\n111\n000"
12
+ end
13
+ end
14
+
15
+ context 'given an oscillator' do
16
+ it 'returns the input after two runs' do
17
+ input = "010\n010\n010"
18
+ output = described_class.run(input)
19
+ expect(described_class.run(output)).to eql input
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,61 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'conwy'
3
+
4
+ RSpec.configure do |config|
5
+ # These two settings work together to allow you to limit a spec run
6
+ # to individual examples or groups you care about by tagging them with
7
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
8
+ # get run.
9
+ config.filter_run :focus
10
+ config.run_all_when_everything_filtered = true
11
+
12
+ # Many RSpec users commonly either run the entire suite or an individual
13
+ # file, and it's useful to allow more verbose output when running an
14
+ # individual spec file.
15
+ if config.files_to_run.one?
16
+ # Use the documentation formatter for detailed output,
17
+ # unless a formatter has already been configured
18
+ # (e.g. via a command-line flag).
19
+ config.default_formatter = 'doc'
20
+ end
21
+
22
+ # Print the 10 slowest examples and example groups at the
23
+ # end of the spec run, to help surface which specs are running
24
+ # particularly slow.
25
+ config.profile_examples = 5
26
+
27
+ # Run specs in random order to surface order dependencies. If you find an
28
+ # order dependency and want to debug it, you can fix the order by providing
29
+ # the seed, which is printed after each run.
30
+ # --seed 1234
31
+ config.order = :random
32
+
33
+ # Seed global randomization in this process using the `--seed` CLI option.
34
+ # Setting this allows you to use `--seed` to deterministically reproduce
35
+ # test failures related to randomization by passing the same `--seed` value
36
+ # as the one that triggered the failure.
37
+ Kernel.srand config.seed
38
+
39
+ # rspec-expectations config goes here. You can use an alternate
40
+ # assertion/expectation library such as wrong or the stdlib/minitest
41
+ # assertions if you prefer.
42
+ config.expect_with :rspec do |expectations|
43
+ # Enable only the newer, non-monkey-patching expect syntax.
44
+ # For more details, see:
45
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
46
+ expectations.syntax = :expect
47
+ end
48
+
49
+ # rspec-mocks config goes here. You can use an alternate test double
50
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
51
+ config.mock_with :rspec do |mocks|
52
+ # Enable only the newer, non-monkey-patching expect syntax.
53
+ # For more details, see:
54
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
55
+ mocks.syntax = :expect
56
+
57
+ # Prevents you from mocking or stubbing a method that does not exist on
58
+ # a real object. This is generally recommended.
59
+ mocks.verify_partial_doubles = true
60
+ end
61
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe Conwy::World do
4
+ subject do
5
+ described_class.new("010\n010\n010")
6
+ end
7
+
8
+ describe '#to_s' do
9
+ it 'returns a string of 0s and 1s representing dead and alive cells' do
10
+ expect(subject.to_s).to eql "010\n010\n010"
11
+ end
12
+ end
13
+
14
+ describe '#next' do
15
+ it 'returns a new world with evolved cells' do
16
+ expect(subject.next.to_s).to eql "000\n111\n000"
17
+ end
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: conwy
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Max Holder
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: " a simple implementation of Conway's Game of Life in Ruby "
56
+ email:
57
+ - mxhold@gmail.com
58
+ executables:
59
+ - conwy
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/conwy
71
+ - conwy.gemspec
72
+ - lib/conwy.rb
73
+ - lib/conwy/cell.rb
74
+ - lib/conwy/version.rb
75
+ - lib/conwy/world.rb
76
+ - spec/bin_spec.rb
77
+ - spec/cell_spec.rb
78
+ - spec/conwy_spec.rb
79
+ - spec/spec_helper.rb
80
+ - spec/world_spec.rb
81
+ homepage: ''
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.2.2
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Conway's Game of Life
105
+ test_files:
106
+ - spec/bin_spec.rb
107
+ - spec/cell_spec.rb
108
+ - spec/conwy_spec.rb
109
+ - spec/spec_helper.rb
110
+ - spec/world_spec.rb