Last active
August 29, 2015 14:16
-
-
Save sentientmonkey/c6f6372c909187470a62 to your computer and use it in GitHub Desktop.
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
| require "continuation" | |
| # via http://c2.com/cgi/wiki?AmbInRuby | |
| class Amb | |
| def initialize | |
| @paths = [] | |
| end | |
| def choose choices | |
| choices.each do |choice| | |
| callcc do |cc| | |
| @paths << cc | |
| return choice | |
| end | |
| end | |
| fail | |
| end | |
| def require condition | |
| fail unless condition | |
| end | |
| def fail | |
| @paths.pop.call rescue abort "Choice tree exhausted." | |
| end | |
| end |
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
| #!/usr/bin/env ruby -w | |
| require_relative 'amb.rb' | |
| # Generates unique Pythagorean triples | |
| # This is where a^2 + b^c = c^2 where a, b, c are not co-prime | |
| # | |
| # http://en.wikipedia.org/wiki/Pythagorean_triple | |
| def py_trip from, to | |
| range = from..to | |
| amb = Amb.new | |
| a = amb.choose range | |
| b = amb.choose range | |
| c = amb.choose range | |
| amb.require (a**2 + b**2 == c**2) && | |
| a.gcd(b) == 1 && | |
| b.gcd(c) == 1 && | |
| a.gcd(c) == 1 | |
| return a,b,c | |
| end | |
| puts py_trip ARGV[0].to_i, ARGV[1].to_i |
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
| #!/usr/bin/env ruby -w | |
| require_relative 'amb.rb' | |
| # Baker, Cooper, Fletcher, Miller, and Smith live on different floors of an apartment house that contains only five floors. | |
| # Baker does not live on the top floor. | |
| # Cooper does not live on the bottom floor. | |
| # Fletcher does not live on either the top or the bottom floor. | |
| # Miller lives on a higher floor than does Cooper. | |
| # Smith does not live on a floor adjacent to Fletcher's. | |
| # Fletcher does not live on a floor adjacent to Cooper's. | |
| # Where does everyone live? | |
| amb = Amb.new | |
| floors = 1..5 | |
| baker = amb.choose floors | |
| cooper = amb.choose floors | |
| fletcher = amb.choose floors | |
| miller = amb.choose floors | |
| smith = amb.choose floors | |
| apartment = {baker: baker, | |
| cooper: cooper, | |
| fletcher: fletcher, | |
| miller: miller, | |
| smith: smith} | |
| amb.require baker != 5 && | |
| cooper != 1 && | |
| fletcher != 1 && fletcher != 5 && | |
| miller > cooper && | |
| (smith-fletcher).abs != 1 && | |
| (fletcher-cooper).abs != 1 && | |
| apartment.values.uniq.length == 5 | |
| puts apartment | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment