루비 1166 자
편집 : 올바른 출력 형식으로 업데이트
MBP i5에서 0.2 초 이내에 일관되게 실행됩니다.
출처 : Github에서보기
짧막 한 농담:
def q(a);a.permutation;end;def z(a,b,c,d);l(a,b,c,d)||l(c,d,a,b);end;def l(a,b,c,d);(0..4).any?{|i|a[i]==b&&c[i+1]==d};end;def i(a,b,c,d);(0..4).any?{|i|a[i]==b&&c[i]==d};end;def t(i);['White','Yellow','Blue','Red','Green','German','Swede','Brit','Norwegian','Dane','Birds','Cats','Horses','Fish','Dogs','Beer','Water','Tea','Milk','Coffee','Blends','PallMall','Prince','Bluemasters','Dunhill'][i];end;def y(s);l=13-s.length;'|'+' '*(l/2.0).floor+s+' '*(l/2.0).ceil+'|';end;def d(s);b=[' '+(0..4).map{'_'*13}.join(' '*4)];u=' ';b<<[1,2,3,4,5].map{|i|y(i.to_s)}.join(u);s.each{|i|b<<i.map{|j|y(t(j))}.join(u)};b<<(0..4).map{'-'*15}.join(u);b<<'';z=s[4].index(13);b<<"The #{t s[0][z]} in the #{t s[1][z]} house owns the fish!";b.join "\n";end;q([0,1,2,3,4]).each{|c|l(c,4,c,0)||next;q([5,6,7,8,9]).each{|n|i(n,7,c,3)||next;n[0]==8||next;z(n,8,c,2)||next;q([10,11,12,13,14]).each{|a|i(n,6,a,14)||next;q([15,16,17,18,19]).each{|d|d[2]==18||next;i(c,4,d,19)||next;i(n,9,d,17)||next;q([20,21,22,23,24]).each{|s|z(a,12,s,24)||next;i(s,21,a,10)||next;z(s,20,d,16)||next;z(s,20,a,11)||next;i(n,5,s,22)||next;i(c,1,s,24)||next;i(s,23,d,15)||next;puts d([n,c,d,s,a]);exit}}}}}
페이지에 맞게 줄 바꿈을 채 웁니다.
def q(a);a.permutation;end;def z(a,b,c,d);l(a,b,c,d)||l(c,d,a,b);end
def l(a,b,c,d);(0..4).any?{|i|a[i]==b&&c[i+1]==d};end;def i(a,b,c,d);
(0..4).any?{|i|a[i]==b&&c[i]==d};end;def t(i);['White','Yellow','Blue',
'Red','Green','German','Swede','Brit','Norwegian','Dane','Birds',
'Cats','Horses','Fish','Dogs','Beer','Water','Tea','Milk','Coffee',
'Blends','PallMall','Prince','Bluemasters','Dunhill'][i];end;def y(s);
l=13-s.length;'|'+' '*(l/2.0).floor+s+' '*(l/2.0).ceil+'|';end;
def d(s);b=[' '+(0..4).map{'_'*13}.join(' '*4)];u=' ';
b<<[1,2,3,4,5].map{|i|y(i.to_s)}.join(u);s.each{|i|b<<i.map{|j|
y(t(j))}.join(u)};b<<(0..4).map{'-'*15}.join(u);b<<'';
z=s[4].index(13);
b<<"The #{t s[0][z]} in the #{t s[1][z]} house owns the fish!";
b.join "\n";end;q([0,1,2,3,4]).each{|c|l(c,4,c,0)||next;
q([5,6,7,8,9]).each{|n|i(n,7,c,3)||next;n[0]==8||next;z(n,8,c,2)||next;
q([10,11,12,13,14]).each{|a|i(n,6,a,14)||next;
q([15,16,17,18,19]).each{|d|d[2]==18||next;i(c,4,d,19)||next;
i(n,9,d,17)||next;q([20,21,22,23,24]).each{|s|z(a,12,s,24)||next;
i(s,21,a,10)||next;z(s,20,d,16)||next;z(s,20,a,11)||next;i(n,5,s,22)||next;
i(c,1,s,24)||next;i(s,23,d,15)||next;puts d([n,c,d,s,a]);exit}}}}}
출력 :
_____________ _____________ _____________ _____________ _____________
| 1 | | 2 | | 3 | | 4 | | 5 |
| Norwegian | | Dane | | Brit | | German | | Swede |
| Yellow | | Blue | | Red | | Green | | White |
| Water | | Tea | | Milk | | Coffee | | Beer |
| Dunhill | | Blends | | PallMall | | Prince | | Bluemasters |
| Cats | | Horses | | Birds | | Fish | | Dogs |
--------------- --------------- --------------- --------------- ---------------
The German in the Green house owns the fish!
Ungolfed Code
@colors = [:white, :yellow, :blue, :red, :green].shuffle.permutation
@cigars = [:blends, :pall_mall, :prince, :bluemasters, :dunhill].shuffle.permutation
@nationalities = [:german, :swedish, :british, :norwegian, :danish,].shuffle.permutation
@drinks = [:beer, :water, :tea, :milk, :coffee].shuffle.permutation
@pets = [:birds, :cats, :horses, :fish, :dogs].shuffle.permutation
def next_to?(set_a, val_a, set_b, val_b)
left_of?(set_a, val_a, set_b, val_b) ||
left_of?(set_b, val_b, set_a, val_a)
end
def left_of?(set_a, val_a, set_b, val_b)
(0..4).any? do |i|
set_a[i] == val_a &&
set_b[i+1] == val_b
end
end
def implies?(set_a, val_a, set_b, val_b)
(0..4).any? do |i|
set_a[i] == val_a &&
set_b[i] == val_b
end
end
def solve
i = 0
@colors.each do |colors|
i += 1
next unless left_of?(colors, :green, colors, :white)
@nationalities.each do |nationalities|
i += 1
next unless implies?(nationalities, :british, colors, :red)
next unless nationalities[0] == :norwegian
next unless next_to?(nationalities, :norwegian, colors, :blue)
@pets.each do |pets|
i += 1
next unless implies?(nationalities, :swedish, pets, :dogs)
@drinks.each do |drinks|
i += 1
next unless drinks[2] == :milk
next unless implies?(colors, :green, drinks, :coffee)
next unless implies?(nationalities, :danish, drinks, :tea)
@cigars.each do |cigars|
i += 1
next unless next_to?(pets, :horses, cigars, :dunhill)
next unless implies?(cigars, :pall_mall, pets, :birds)
next unless next_to?(cigars, :blends, drinks, :water)
next unless next_to?(cigars, :blends, pets, :cats)
next unless implies?(nationalities , :german, cigars, :prince)
next unless implies?(colors, :yellow, cigars, :dunhill)
next unless implies?(cigars, :bluemasters, drinks, :beer)
return [colors, nationalities, pets, drinks, cigars], i
end
end
end
end
end
end
class Symbol
def humanize
result = self.to_s
result.gsub!('_', ' ')
result.split(' ').collect{|part| part.capitalize }.join(' ')
end
end
solution, attempts = solve
puts "\nSolved in #{attempts} attempts.\n\n"
for i in (0..4)
number, color, nationality, cigar, drink, pet = i+1, solution[0][i], solution[1][i].humanize, solution[4][i].humanize, solution[3][i], solution[2][i]
puts "House #{number} is #{color}. The owner is #{nationality}, smokes #{cigar}, drinks #{drink}, and keeps #{pet}."
end
puts "\n"
출력 :
Solved in 37663 attempts.
House 1 is yellow. The owner is Norwegian, smokes Dunhill, drinks water, and keeps cats.
House 2 is blue. The owner is Danish, smokes Blends, drinks tea, and keeps horses.
House 3 is red. The owner is British, smokes Pall Mall, drinks milk, and keeps birds.
House 4 is green. The owner is German, smokes Prince, drinks coffee, and keeps fish.
House 5 is white. The owner is Swedish, smokes Bluemasters, drinks beer, and keeps dogs.
shuffle
초기 설정에서 사용 하면 매번 고유 한 솔루션 경로가 보장됩니다. 실행 간 변경 시도 횟수로이를 확인할 수 있습니다. 따라서 코드를 변경하는 것이 쉽지는 않지만 입력을 개별적으로 공급해야하는 필요성이 줄어 듭니다.