Created
April 8, 2014 11:08
-
-
Save higuma/10110407 to your computer and use it in GitHub Desktop.
覆面算を解く(Ruby/Pythonレクリエーション) ref: http://qiita.com/higuma/items/c44246ebd8e6e4a14a64
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 'set' | |
require 'mathn' | |
module Alphametics | |
def solve(puzzle) | |
puzzle = puzzle.upcase | |
words = puzzle.scan /[A-Z]+/ | |
chars = Set.new words.join.each_char | |
abort 'Too many letters' if chars.size > 10 | |
first_chars = Set.new words.select {|w| w.size > 1 }.map {|w| w[0] } | |
n = first_chars.size | |
sorted_chars = first_chars.to_a.join + (chars - first_chars).to_a.join | |
%w[0 1 2 3 4 5 6 7 8 9].permutation(chars.size).each do |guess| | |
next if guess[0, n].member? '0' | |
expr = puzzle.tr sorted_chars, guess.join | |
return expr if eval expr | |
end | |
return | |
end | |
end | |
if __FILE__ == $PROGRAM_NAME | |
include Alphametics | |
ARGV.each do |arg| | |
puts arg | |
solution = solve arg | |
puts solution if solution | |
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
$ ruby alphametics.rb 'SEND + MORE == MONEY' | |
SEND + MORE == MONEY | |
9567 + 1085 == 10652 | |
$ ruby alphametics.rb 'DO + YOU + FEEL == LUCKY' | |
DO + YOU + FEEL == LUCKY | |
57 + 870 + 9441 == 10368 |
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
$ ruby alphametics.rb 'NORTH / SOUTH == EAST / WEST' | |
NORTH / SOUTH == EAST / WEST | |
51304 / 61904 == 7260 / 8760 | |
$ ruby alphametics.rb 'PI * R ** 2 == AREA' | |
PI * R ** 2 == AREA | |
96 * 7 ** 2 == 4704 |
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
$ ruby alphametics.rb 'MALCOLM + X == MALCOLM' | |
MALCOLM + X == MALCOLM | |
1234531 + 0 == 1234531 |
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
$ time python3 alphametics.py 'EARTH + AIR + FIRE + WATER == NATURE' | |
EARTH + AIR + FIRE + WATER == NATURE | |
67432 + 704 + 8046 + 97364 == 173546 | |
real 0m35.305s | |
user 0m35.194s | |
sys 0m0.024s | |
$ rbenv local 1.9.3-p448 # rubyバージョン変更 | |
$ time ruby alphametics.rb 'EARTH + AIR + FIRE + WATER == NATURE' | |
EARTH + AIR + FIRE + WATER == NATURE | |
67432 + 704 + 8046 + 97364 == 173546 | |
real 0m20.051s | |
user 0m19.921s | |
sys 0m0.040s | |
$ rbenv local 2.0.0-p247 # rubyバージョン変更 | |
$ time ruby alphametics.rb 'EARTH + AIR + FIRE + WATER == NATURE' | |
EARTH + AIR + FIRE + WATER == NATURE | |
67432 + 704 + 8046 + 97364 == 173546 | |
real 0m24.925s | |
user 0m24.834s | |
sys 0m0.032s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment