-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
187 additions
and
44 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
class ConstrainedTowers < Towers | ||
@@directions = {} | ||
|
||
def initialize discs | ||
super | ||
@ternary = ConstrainedTowers.ternarise @count, @discs | ||
end | ||
|
||
def move | ||
flip = Towers.diff ConstrainedTowers.ternarise(@count, @discs), ConstrainedTowers.ternarise(@count += 1, @discs) | ||
source = Towers.find_disc flip, @stacks | ||
|
||
@stacks[ConstrainedTowers.find_adjacent_stack flip, source, @stacks].push @stacks[source].pop | ||
end | ||
|
||
def binary | ||
ternary | ||
end | ||
|
||
def ternary | ||
ConstrainedTowers.ternarise @count, @discs | ||
end | ||
|
||
def solved | ||
ternary.chars.all? { |trit| trit.to_i == 2 } | ||
end | ||
|
||
def inspect | ||
{ | ||
stacks: @stacks, | ||
count: ternary | ||
} | ||
end | ||
|
||
def ConstrainedTowers.ternarise value, width | ||
'%0*d' % [width, value.to_s(3)] | ||
end | ||
|
||
def ConstrainedTowers.find_adjacent_stack disc, source, stacks | ||
begin | ||
direction = @@directions[disc] | ||
rescue KeyError | ||
@@directions[disc] = :right | ||
direction = right | ||
end | ||
|
||
case source | ||
when 0 | ||
@@directions[disc] = :right | ||
return 1 | ||
when 2 | ||
@@directions[disc] = :left | ||
return 1 | ||
when 1 | ||
if @@directions[disc] == :right | ||
return 2 | ||
else | ||
return 0 | ||
end | ||
end | ||
end | ||
end |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
describe ConstrainedTowers do | ||
it 'generates ternary numbers' do | ||
expect(ConstrainedTowers.ternarise 19, 5).to eq '00201' | ||
end | ||
|
||
context 'diffing ternary numbers' do | ||
specify 'easy case' do | ||
expect(Towers.diff '001', '002').to eq 0 | ||
end | ||
|
||
specify 'trickier case' do | ||
expect(Towers.diff '012', '020').to eq 1 | ||
end | ||
end | ||
context 'adjacent stacks' do | ||
context 'noddy cases' do | ||
it 'moves from left to centre' do | ||
stacks = [[], [], []] | ||
expect(ConstrainedTowers.find_adjacent_stack 0, 0, stacks).to eq 1 | ||
end | ||
|
||
it 'moves from right to centre' do | ||
stacks = [[], [], []] | ||
expect(ConstrainedTowers.find_adjacent_stack 0, 2, stacks).to eq 1 | ||
end | ||
end | ||
end | ||
|
||
it 'follows the rules' do | ||
towers = ConstrainedTowers.new 4 | ||
|
||
until towers.solved do | ||
towers.move | ||
towers.stacks.each do |stack| | ||
expect(stack.sort.reverse).to eq stack | ||
end | ||
end | ||
end | ||
|
||
context 'matrices' do | ||
context 'matrices' do | ||
towers = ConstrainedTowers.new 3 | ||
towers.move | ||
towers.move | ||
it 'has the correct second matrix' do | ||
expect(towers.matrix).to eq [ | ||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
[0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
[0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, | ||
0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0] | ||
] | ||
end | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
require 'ruby/towers' | ||
require 'ruby/constrained_towers' | ||
require 'coveralls' | ||
|
||
Coveralls.wear! | ||
|
This file contains 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
This file contains 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