Created
January 23, 2016 15:32
-
-
Save mitio/3a32f03d8915fac68b61 to your computer and use it in GitHub Desktop.
Game of Life without vowels
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
slct = ->(lst, &blk) { | |
lst == [] ? [] : ( | |
((r = blk.(lst[0])) ? [r] : []) + slct.(lst[1..-1], &blk) | |
) | |
} | |
n_cs = ->((x, z)) { | |
[ | |
[x - 1, z - 1], [x, z - 1], [x + 1, z - 1], | |
[x - 1, z ], [x + 1, z ], | |
[x - 1, z + 1], [x, z + 1], [x + 1, z + 1], | |
] | |
} | |
NLL = [][0] | |
GL = ->(clls) { | |
nxt_gn = slct.(clls) { |c| | |
n_cnt = 0 | |
slct.(n_cs.(c)) { |nc| | |
(clls & [nc]) == [nc] ? (n_cnt += 1) : NLL | |
} | |
n_cnt == 2 || n_cnt == 3 ? c : NLL | |
} | |
d_ns = [] | |
slct.(clls) { |c| | |
slct.(n_cs.(c)) { |nc| | |
clls & [nc] == [nc] ? NLL : ( | |
d_ns & [nc] == [nc] ? NLL : ( | |
d_ns << nc | |
) | |
) | |
} | |
} | |
nxt_gn += slct.(d_ns) { |c| | |
n_cnt = 0 | |
slct.(n_cs.(c)) { |nc| | |
(clls & [nc]) == [nc] ? (n_cnt += 1) : NLL | |
} | |
n_cnt == 3 ? c : NLL | |
} | |
nxt_gn | |
} | |
describe 'Game of life' do | |
it 'returns an empty world when called with an empty world' do | |
expect(GL.([])).to eq [] | |
end | |
it 'kills underpopulated cells' do | |
next_generation = GL.([ | |
[1, 1], | |
[2, 2], | |
]) | |
expect(next_generation).to eq [] | |
end | |
it 'preserves cells with 2 neighbours' do | |
next_generation = GL.([ | |
[1, 1], [2, 1], | |
[1, 2], | |
]) | |
expect(next_generation).to include([1, 1]) | |
end | |
it 'kills overpopulated cells' do | |
next_generation = GL.([ | |
[1, 1], [2, 1], | |
[1, 2], [2, 2], [3, 2], | |
]) | |
expect(next_generation).not_to include([2, 2]) | |
end | |
it 'gives birth to dead cells with 3 neighbours' do | |
next_generation = GL.([ | |
[1, 1], [2, 1], | |
[1, 2], | |
]) | |
expect(next_generation).to match_array [ | |
[1, 1], [2, 1], | |
[1, 2], [2, 2], | |
] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment