prisoner_puzzle 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c23ea65d1c4f3e0dfd349f46058ac24744226b46
4
+ data.tar.gz: 3bea6176d0b2fe03db13615cc16abfbf39ac5bd9
5
+ SHA512:
6
+ metadata.gz: 3c89e4b054bd63829d3bfd6ec0c2bfb9d1066804903f58275ed3e298f1046cef9e14660f894a64ffd47f7a3f1a412a01968604e0679a8aec454eff810de41708
7
+ data.tar.gz: ea993b8800b4bf372e130391cbed17d15cec71b7ea7472aaa782a808df978a7b06ce287dbfc5e04b316edaf1c459001a2c9d8abd11c487856a0ed26fc3cb5ffa
data/lib/prisoner.rb ADDED
@@ -0,0 +1,41 @@
1
+ class Prisoner
2
+ attr_accessor :have_flipped, :leader, :warden
3
+ def initialize switchbox, prisoner_count, leader = false
4
+ @have_flipped = false
5
+ @switchbox = switchbox
6
+ @leader = leader
7
+ @warden = nil
8
+ @prisoner_count = prisoner_count
9
+ @count = 0
10
+ end
11
+
12
+ def visit
13
+ if @leader
14
+ if @count == @prisoner_count + 1
15
+ puts "I am the leader of this prison gang, and I say we are free."
16
+ self.declare_finished(@warden)
17
+ elsif @switchbox.left == :OFF
18
+ @count += 1
19
+ @switchbox.left_flip
20
+ puts "Alright, one more prisoner has been here. That's #{@count} so far."
21
+ else
22
+ @switchbox.right_flip
23
+ end
24
+
25
+
26
+ else
27
+ if @switchbox.left == :ON and @have_flipped == false
28
+ @switchbox.left_flip
29
+ have_flipped == true
30
+ puts "New room!"
31
+ else
32
+ @switchbox.right_flip
33
+ puts "Oh. This room again."
34
+ end
35
+ end
36
+ end
37
+
38
+ def declare_finished warden
39
+ warden.finish
40
+ end
41
+ end
@@ -0,0 +1,21 @@
1
+ require './prisoner.rb'
2
+ require './warden.rb'
3
+ require './switchbox.rb'
4
+
5
+ class PrisonerPuzzle
6
+ def initialize
7
+ box = Switchbox.new
8
+ prisoners = []
9
+ 23.times { prisoners.push(Prisoner.new(box, 23))}
10
+ @warden_gentles = Warden.new(prisoners)
11
+ prisoners[1].leader = true
12
+ prisoners.each {|p| p.warden = @warden_gentles}
13
+ end
14
+
15
+ def run
16
+ while not @warden_gentles.done
17
+ @warden_gentles.take_into_room
18
+ end
19
+ puts "That took a total of #{@warden_gentles.hours_elapsed/24} days."
20
+ end
21
+ end
data/lib/switchbox.rb ADDED
@@ -0,0 +1,32 @@
1
+ class Switchbox
2
+ attr_accessor :right, :left
3
+ def initialize
4
+ if rand(2) == 0
5
+ @left = :ON
6
+ else
7
+ @left = :OFF
8
+ end
9
+
10
+ if rand(2) == 0
11
+ @right = :ON
12
+ else
13
+ @right = :OFF
14
+ end
15
+ end
16
+
17
+ def left_flip
18
+ if @left == :ON
19
+ @left = :OFF
20
+ else
21
+ @left = :ON
22
+ end
23
+ end
24
+
25
+ def right_flip
26
+ if @right == :ON
27
+ @right = :OFF
28
+ else
29
+ @right = :ON
30
+ end
31
+ end
32
+ end
data/lib/warden.rb ADDED
@@ -0,0 +1,28 @@
1
+ class Warden
2
+ attr_accessor :roster, :visited, :done, :hours_elapsed
3
+ def initialize *prisoners
4
+ @roster = []
5
+ @visited = {}
6
+ prisoners.each do |p|
7
+ @roster.push(p)
8
+ @visited[p] = false
9
+ end
10
+ @done = false
11
+ @hours_elapsed = 0
12
+ end
13
+
14
+ def take_into_room
15
+ selectee = @roster.flatten.sample
16
+ selectee.visit
17
+ @hours_elapsed += rand(73)
18
+ end
19
+
20
+ def finish
21
+ if @visited.all?
22
+ puts "You're all free to go."
23
+ else
24
+ puts "To the alligators with you!"
25
+ end
26
+ @done = true
27
+ end
28
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prisoner_puzzle
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Rob Whitehead
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-11 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: "Simulation of the 100 prisoners and a light switch logic puzzle. \n
14
+ \ \t\t\t\t This gem simulates that experiment with 23 prisoners implementing the
15
+ usual solution."
16
+ email: rob.whitehead@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/prisoner_puzzle.rb
22
+ - lib/prisoner.rb
23
+ - lib/warden.rb
24
+ - lib/switchbox.rb
25
+ homepage: https://rubygems.org/gems/prisoner_puzzle
26
+ licenses: []
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 2.0.3
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: Simulation of a popular logic puzzle.
48
+ test_files: []