आइंस्टीन की पहेली को हल करें


19

आइंस्टीन ने एक पहेली लिखी थी। उन्होंने कहा कि दुनिया के 98% लोग इसे हल नहीं कर सके।

मैंने ट्रेन में काम करने के दौरान लगभग 25 मिनट में इस पहेली को हल किया। यह मूल रूप से सिर्फ कटौती है।

पहेली

एक पंक्ति में 5 अलग-अलग रंगों में 5 घर हैं।
प्रत्येक घर में एक अलग राष्ट्रीयता वाला व्यक्ति रहता है।
5 मालिक एक निश्चित प्रकार का पेय पीते हैं, एक निश्चित ब्रांड का सिगार धूम्रपान करते हैं, और एक निश्चित पालतू जानवर रखते हैं।
किसी भी मालिक के पास एक ही पालतू जानवर नहीं है, एक ही ब्रांड का सिगार धूम्रपान करें, या एक ही पेय पीएं।
प्रश्न: मछली का मालिक कौन है?

इस पहेली को हल करने के लिए आइंस्टीन 15 संकेत प्रदान करता है:

  1. ब्रिटेन के लाल घर में रहते हैं।
  2. स्वेड कुत्तों को पालतू जानवर के रूप में रखता है।
  3. डेन चाय पीता है।
  4. ग्रीन हाउस व्हाइट हाउस के तत्काल बाईं ओर है।
  5. ग्रीन हाउस के मालिक ने कॉफी पी।
  6. पाल मॉल की धुनाई करने वाला मालिक पक्षियों को चीरता है।
  7. पीले घर का मालिक डनहिल धूम्रपान करता है।
  8. सेंटर हाउस में रहने वाला मालिक दूध पीता है।
  9. नॉर्वेजियन पहले घर में रहता है।
  10. ब्लेंड्स को मारने वाला मालिक बिल्लियों को रखने वाले के बगल में रहता है।
  11. जो मालिक घोड़े को रखता है वह डनहिल को मारने वाले के बगल में रहता है।
  12. ब्लूमास्टर्स को मारने वाला मालिक बीयर पीता है।
  13. जर्मन राजकुमार को धूम्रपान करता है।
  14. नॉर्वेजियन नीले घर के बगल में रहता है।
  15. ब्लेंड्स को मारने वाला मालिक पानी पीने वाले के बगल में रहता है।

इन संकेतों से आप किसी समाधान पर आ सकते हैं।

आपका कार्य: एक कार्यक्रम बनाएं जो आपके लिए इस पहेली को हल करेगा। हार्ड कोडिंग समाधान की अनुमति नहीं है (डुह)

इसे किसी भी प्रारूप में संकेत को हार्डकोड करने की अनुमति है।
उदाहरण स्वरूप:

//Hints in order
(Nationality:Brit)==(Color:Red)
(Nationality:Swede)==(Pet:Dogs)
(Nationality:Dane)==(Drink: Tea)
(Color:Green)/(Color:White)
(Color:Green)==(Drink:Coffee)
(Smoke:PallMall)==(Pet:Birds)
(Color:Yellow)==(Smoke:Dunhill)
(House:3)==(Drink:Milk)
(Nationality:Norwegian)==(House:1)
(Smoke:Blend)/\(Pet:Cats)

== का अर्थ है,
/ के बायीं ओर के बराबर / का
अर्थ है दाईं ओर
/ \ का अर्थ है बाएं या दाएं का

जैसे मैंने कहा कि इसकी अनुमति या तो हार्डकोड संकेत है या इनपुट के रूप में है।

आउटपुट: आउटपुट निम्न प्रारूप में होना चाहिए (सही मानों के साथ, बस ट्रोल्स के लिए कह रहा है;);

 _____________    _____________    _____________    _____________    _____________
|   Number    |  |   Number    |  |   Number    |  |   Number    |  |   Number    |
| Nationality |  | Nationality |  | Nationality |  | Nationality |  | Nationality |
|    Color    |  |    Color    |  |    Color    |  |    Color    |  |    Color    |
|    Drink    |  |    Drink    |  |    Drink    |  |    Drink    |  |    Drink    |
|    Smoke    |  |    Smoke    |  |    Smoke    |  |    Smoke    |  |    Smoke    |
|     Pet     |  |     Pet     |  |     Pet     |  |     Pet     |  |     Pet     |
---------------  ---------------  ---------------  ---------------  ---------------

The <Nationality> in the <Color> house owns the fish!

Ascii कला बक्से को आपके द्वारा बदला जा सकता है, जब तक वे बक्से हैं, इससे कोई फर्क नहीं पड़ता कि आप किन प्रतीकों का उपयोग करते हैं।

इस पहेली और समाधान पर किसी भी ज्ञान का उपयोग कार्यक्रम में नहीं किया जा सकता है। यह पहेली को हल करने के लिए शुद्ध तर्क और कटौती का उपयोग करना चाहिए।

यह एक कोडगॉल्फ के रूप में टैग किया गया, लेकिन एक कोड चुनौती हो सकती है, निश्चित नहीं। एक कोड चुनौती के लिए मानदंड जीतने पर कोई विचार साझा करने के लिए स्वतंत्र महसूस :)

अब के लिए यह कोड-गोल्फ है इसलिए सबसे कम बायटेकाउंट जीत के साथ कार्यक्रम।

गुड लक और हैप्पी कोडिंग :)


15
पहले पैराग्राफ के दावे लगभग निश्चित रूप से झूठे हैं।
पीटर टेलर

1
@PeterTaylor क्या मतलब है आपका? 98% और आइंस्टीन बिट? यह निश्चित रूप से नहीं पता होगा, इंटरव्यू इसे कहते हैं और इसकी परवाह किए बिना, पृष्ठभूमि के लिए इसका मज़ा :) मैंने इसे हल करने पर विशेष महसूस किया, "उन 2% में yay im"
Teun Pronk

4
उन दोनों बिट्स। इंटरव्यू में भी टिमकेब की सुविधा होती है, इसलिए आपको अच्छी तरह से सलाह दी जाएगी कि वे अपनी हर बात पर विश्वास न करें, खासकर जब वे कोई सबूत नहीं जोड़ते हैं।
पीटर टेलर

1
@PeterTaylor हाँ मुझे पता है, लेकिन जैसा मैंने कहा, किसी भी तरह से पृष्ठभूमि के लिए इसका मज़ा और इसके तथ्य, या कल्पना की तरह, दिए गए समस्या के लिए आवश्यक नहीं है। इसके अलावा विकिपीडिया कहता है कि इसके लिए कोई वास्तविक प्रमाण नहीं है :)
त्युन प्रोक

3
मुझे लगता है कि समाधानों को घरों, संपत्तियों और संकेतों के एक अनियंत्रित संख्या के सामान्यीकरण में सक्षम होना चाहिए। तब यह वास्तव में एक दिलचस्प सवाल होगा, जिसे आपको एक नियम के रूप में "नो हार्डकोडिंग" में फेंकना नहीं होगा। जो मेरे अनुभव में हमेशा एक सवाल की ओर जाता है कि यह वास्तव में कितना कठिन है।
क्रंचर

जवाबों:


24

प्रस्तावना - 954 वर्ण

m(A,Z):-member(A,Z).
e:-e(Z),w(Z).
e(Z):-Z=[[1,A,F,K,P,U],[2,B,G,L,Q,V],[3,C,H,M,R,W],[4,D,I,N,S,X],[5,E,J,O,T,Y]],m([_,b,r,_,_,_],Z),m([_,s,_,d,_,_],Z),m([_,d,_,_,t,_],Z),m([WH,_,w,_,_,_],Z),m([GH,_,g,_,_,_],Z),GH=:=WH-1,m([_,_,g,_,c,_],Z),m([_,_,_,b,_,p],Z),m([_,_,y,_,_,d],Z),m([3,_,_,_,m,_],Z),m([1,n,_,_,_,_],Z),m([BH,_,_,_,_,b],Z),m([CH,_,_,c,_,_],Z),(BH=:=CH+1;BH=:=CH-1),m([DH,_,_,_,_,d],Z),m([HH,_,_,h,_,_],Z),(HH=:=DH+1;HH=:=DH-1),m([_,_,_,_,b,l],Z),m([_,g,_,_,_,r],Z),m([NH,n,_,_,_,_],Z),m([YH,_,b,_,_,_],Z),(NH=:=YH+1;NH=:=YH-1),m([SH,_,_,_,_,b],Z),m([XH,_,_,_,w,_],Z),(SH=:=XH+1;SH=:=XH-1),p([n,d,b,g,s],[A,B,C,D,E]),p([y,b,r,g,w],[F,G,H,I,J]),p([c,h,b,f,d],[K,L,M,N,O]),p([w,t,m,c,b],[P,Q,R,S,T]),p([d,b,p,r,l],[U,V,W,X,Y]).
t(X,[X|R],R).
t(X,[F|R],[F|S]):-t(X,R,S).
p([W|X],Y):-p(X,V),t(W,Y,V).
p([],[]).
b:-write('+--+--+--+--+--+--+'),nl.
z(A):-writef('|%2L|%2L|%2L|%2L|%2L|%2L|',A),nl.
w([A,B,C,D,E]):-b,z(A),z(B),z(C),z(D),z(E),b.

उत्पादन

+--+--+--+--+--+--+
|1 |n |y |c |w |d |
|2 |d |b |h |t |b |
|3 |b |r |b |m |p |
|4 |g |g |f |c |r |
|5 |s |w |d |b |l |
+--+--+--+--+--+--+

चाभी:

  • पहला कॉलम हाउस नंबर है;
  • दूसरा स्तंभ राष्ट्रीयता है;
  • तीसरा स्तंभ पसंदीदा रंग है;
  • चौथा स्तंभ पालतू है;
  • पांचवां स्तंभ पेय है; तथा
  • छठा स्तंभ सिगरेट है (आर = राजकुमार, एल = ब्लेमेस्टर)।

9
यह नौकरी के लिए सही भाषा है।
मारीनस

क्या आपको गोल्फ नंबरों को तुलनीय बनाने के लिए नामों की व्याख्या नहीं करनी चाहिए?
blabla999

5

रूबी 322 + इनपुट 442

लगभग 25 बिलियन संभावित उत्तरों की ब्रूट फोर्स खोज ।
मेरे कंप्यूटर को इस प्रोग्राम को चलाने में लगभग 75 दिन लगेंगे ।
मैंने कभी सत्यापित नहीं किया कि क्या यह कार्यक्रम एक सही उत्तर छापता है!

ऐसे दोड़ो ruby riddle.rb < riddle.in

riddle.rb (332 बाइट्स)

g=readlines
v=g.shift(5).map &:split
c,*d=v.map{|a|[*a.permutation]}
c.product(*d){|a|(f=eval a.map{|b|b.each_with_index.map{|s,i|"#{s}=#{i}\n"}.join}.join+'g.map{|h|eval h}.all?&&fish')&&(r=['-----------']*5
puts [r,[*0..4],*a,r].map{|s|s.map{|t|'|%11s|'%t}.join},"The #{a[0][f]} in the #{a[1][f]} house owns the fish!")}

riddle.in (442 बाइट्स)

brit dane german norwegian swede
blue green red white yellow
beer coffee milk tea water
blends bluemasters dunhill pall_mall prince
birds cats dogs fish horse
brit==red
swede==dogs
dane==tea
green==white-1
green==coffee
pall_mall==birds
yellow==dunhill
2==milk
norwegian==0
blends==cats-1||blends==cats+1
horse==dunhill-1||horse==dunhill+1
bluemasters==beer
german==prince
norwegian==blue-1||norwegian==blue+1
blends==water-1||blends==water+1

इनपुट फ़ाइल में 5 पंक्तियों का नाम होना चाहिए, उसके बाद तार्किक अभिव्यक्तियों की पंक्तियाँ होनी चाहिए। हाउस नंबर 0 से 4 हैं fish। ए होना चाहिए ।

कार्यक्रम Array#permutationराष्ट्रीयता, रंग, पेय, धूम्रपान और पालतू जानवरों की सरणियों के आदेश के सभी तरीकों को खोजने के लिए पांच बार कॉल करता है। लंबा लूप लगभग 25 बिलियन संभावित उत्तरों को पुनरावृत्त करने के लिए c.product(*d){|a|...}बुला रहा Array#productहै। लूप बॉडी का रूप है (f=eval ...)&&(...)eval ...भाग तार्किक अभिव्यक्ति मूल्यांकन करता है। यदि सभी सच हैं, तो fमछली का घर नंबर है और &&(...)भाग उत्तर को प्रिंट करता है। रूबी में हाउस नंबर 0 सच है।

कोड गोल्फ का मतलब गति के लिए कोड जोड़ना नहीं है! लेकिन कार्यक्रम चलाने के लिए मेरे पास 75 दिनों का अभाव है !


1
यहां तक ​​कि ब्रूट बल के बजाय एक या दो सुराग का उपयोग करने से बहुत सारी खोज को बचाया जा सकेगा।
qwr

मैं रूबी को नहीं जानता, लेकिन इंटरनेट कहता है कि यह धीमा है । मैंने शर्त लगाई कि यदि आपने अपने उत्तर को संकलित भाषा में अनुवादित किया है और फिर इसे थोड़ा सा भी अनुकूलित किया है, तो आप 5000% स्पीडअप के करीब पहुंच सकते हैं, जो आपको उचित समय में शुद्धता को सत्यापित करने की आवश्यकता है। यदि आपने सबसे कठिन, लेकिन अच्छी तरह से प्रलेखित, जावा अनुवाद लिखा है, तो मुझे आपके लिए इसे अनुकूलित करने और फिर शुद्धता को सत्यापित करने में खुशी होगी।
बारिश का दौर

4

प्रोलॉग, 653 वर्ण

a([],L,L).
a([H|T],L2,[H|L3]):-a(T,L2,L3).
m(X,L):-a(_,[X|_],L).
r(X,Y,L):-a(_,[X,Y|_],L).
n(X,Y,L):-r(X,Y,L);r(Y,X,L).
s:-s(S),w(S).
s(S):-length(S,5),m([b,r,_,_,_],S),m([s,_,_,_,d],S),m([d,_,t,_,_],S),r([_,g,_,_,_],[_,w,_,_,_],S),m([_,g,c,_,_],S),m([_,_,_,p,b],S),m([_,y,_,d,_],S),S=[_,_,[_,_,m,_,_],_,_],S=[[n,_,_,_,_],_,_,_,_],n([_,_,_,b,_],[_,_,_,_,c],S),n([_,_,_,d,_],[_,_,_,_,h],S),m([_,_,b,l,_],S),m([g,_,_,r,_],S),n([n,_,_,_,_],[_,b,_,_,_],S),n([_,_,_,b,_],[_,_,w,_,_],S),m([_,_,_,_,f],S).
b:-write('----------------+'),nl.
z(Y,A):-write(Y),writef('|%2L|%2L|%2L|%2L|%2L|',A),nl.
w([S1,S2,S3,S4,S5]):-b,z(1,S1),z(2,S2),z(3,S3),z(4,S4),z(5,S5),b.

1
मैंने आपके कोड में अनावश्यक रिक्त स्थान और लाइनब्रेक को हटाने की स्वतंत्रता ली, जो इसे 653 वर्णों तक ले जाती है।
21

1
एसडब्ल्यूआई-प्रोलॉग पर परीक्षण किया गया, बस प्रकार? - एस।
ej8000

3

स्मालटाक 1332 चर

सादे स्मालटाक का उपयोग करना:

|n c v g p t r|n:=#(Brit Swede Dane Norwegian German
Red Green White Yellow Blue
Tea Coffee Milk Beer Water
PallMall Dunhill Blends Bluemasters Prince
Dogs Birds Cats Horses Fish).
v:=Dictionary new.n do:[:n|v at:n put:nil].g:=n splitForSize:5.
c:={{[:b :r|b==r].#Brit.#Red}.{[:s :d|s==d].#Swede.#Dogs}.{[:d :t|d==t].#Dane.#Tea}.
{[:g :w|g==(w-1)].#Green.#White}.{[:g :c|g==c].#Green.#Coffee}.
{[:p :b|p==b].#PallMall.#Birds}.{[:y :d|y==d].#Yellow.#Dunhill}.
{[:m|m==3].#Milk}.{[:n|n==1].#Norwegian}.{[:b :c|(b-c)abs==1].#Blends.#Cats}.
{[:h :d|(h-d)abs==1].#Horses.#Dunhill}.{[:m :b|m==b].#Bluemasters.#Beer}.
{[:g :p|g==p].#German.#Prince}.{[:n :b|(n-b)abs==1].#Norwegian.#Blue}.
{[:b :w|(b-w)abs==1].#Blends.#Water}}.
t:=[:c|x:=(c from:2collect:[:n|v at:n]).(x includes:nil)or:[c first valueWithArguments:x]].
p:=[|f|(((''-'',* 16),'' ''),*5)printCR.g do:[:n||w|w:=n collect:[:n|v at:n].w sortWith:n.
n do:[:n|''|''print.(n centerPaddedTo:14)print.''| ''print].''''printCR.].(((''-'',* 16),'' ''),*5)printCR.
f:=v at:#Fish.(''The %1 in the %2 house owns the fish''bindWith:(g first detect:[:n|(v at:n)==f])with:((g at:2) detect:[:n|(v at:n)==f]))printCR].
r:=[:gi|gi==0ifTrue:p ifFalse:[#(1 2 3 4 5)permutationsDo:[:perm|v declareAll:(g at:gi)from:perm.
(c conform:t)ifTrue:[r value:gi-1]].v declareAll:(g at:gi)from:#(nil),*5]].
r value:g size.

आउटपुट:

---------------- ---------------- ---------------- ---------------- ---------------- 
|  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

चूंकि गोल्फ संस्करण लगभग अपठनीय है, यहां डेवलपर का कोड उपयुक्त नाम, इंडेंटेशन और पठनीयता के लिए रिक्त स्थान है:

|names constraints foundSolution v groups printSolution testC test try|

names := #(Brit Swede Dane Norwegian German
 Red Green White Yellow Blue
 Tea Coffee Milk Beer Water
 PallMall Dunhill Blends Bluemasters Prince
 Dogs Birds Cats Horses Fish).

v := Dictionary new.
names do:[:n | v at:n put:nil].
groups := names splitForSize:5.

constraints := {
        {[:b :r | b==r] . #Brit . #Red   }.          "/ The Brit lives in the red house.
        {[:s :d | s==d] . #Swede . #Dogs }.          "/ The Swede keeps dogs as pets.
        {[:d :t | d==t] . #Dane . #Tea   }.          "/ The Dane drinks tea.
        {[:g :w | g==(w-1)] . #Green . #White }.     "/ The green house is on the immediate left of the white house.
        {[:g :c | g==c] . #Green . #Coffee}.         "/ The green house's owner drinks coffee.
        {[:p :b | p==b] . #PallMall . #Birds}.       "/ The owner who smokes Pall Mall rears birds.
        {[:y :d | y==d] . #Yellow . #Dunhill}.       "/ The owner of the yellow house smokes Dunhill.
        {[:m | m==3] . #Milk}.                       "/ The owner living in the center house drinks milk.
        {[:n | n==1] . #Norwegian }.                 "/ The Norwegian lives in the first house.
        {[:b :c | (b-c)abs==1] . #Blends . #Cats}.   "/ The owner who smokes Blends lives next to the one who keeps cats.
        {[:h :d | (h-d)abs==1] . #Horses . #Dunhill}."/ The owner who keeps the horse lives next to the one who smokes Dunhill.
        {[:m :b | m==b] . #Bluemasters . #Beer}.     "/ The owner who smokes Bluemasters drinks beer.
        {[:g :p | g==p] . #German . #Prince}.        "/ The German smokes Prince.
        {[:n :b | (n-b)abs==1] . #Norwegian . #Blue}."/ The Norwegian lives next to the blue house.
        {[:b :w | (b-w)abs==1] . #Blends . #Water}.  "/ The owner who smokes Blends lives next to the one who drinks water.
}.
testC := [:c | vi:=((c from:2) collect:[:n|v at:n]). (vi includes:nil) or:[c first valueWithArguments:vi]].
test := [constraints conform:testC].

printSolution :=
 [  |f|
    ((('-',* 16),' '),*5) printCR.
    groups do:[:names|
         |values|
         values := names collect:[:nm|v at:nm].
         values sortWith:names.
         names do:[:n| '|'print. (n centerPaddedTo:14)print.'| ' print].
         '' printCR.
    ].
    ((('-',* 16),' '),*5) printCR.
    f := v at:#Fish.
    ('The %1 in the %2 house owns the fish'
        bindWith:((groups at:1) detect:[:n|(v at:n)==f])
        with:((groups at:2) detect:[:n|(v at:n)==f]))printCR
 ].


try := [:gi |
    gi == 0 
     ifTrue: printSolution
     ifFalse:[
        (1 to:5) asArray permutationsDo:[:perm |
            v declareAll:(groups at:gi) from:perm.
            test value ifTrue:[
                try value:(gi-1).
            ].
        ].
        v declareAll:(groups at:gi) from:#(nil nil nil nil nil).
    ].
].
try value:groups size.

एक बाधा विलायक पुस्तकालय का उपयोग:

एक बाधा सॉल्वर लाइब्रेरी का उपयोग करना, जो कि OZ / मोज़ार्ट मैनुअल में वर्णित के समान है। जैसा कि मुझे उम्मीद है कि आप इस प्रतियोगिता के लिए मान्य नहीं होने की शिकायत करेंगे;; यह आपके मनोरंजन और प्रेरणा के लिए ही जोड़ा गया है (अपनी भाषा में एक गर्भनिरोधक पैकेज के लिए देखें), अपुष्ट, और न कि गिनती गिनते हुए।

|solution|

solution := Solver
    anySolutionFor:[
        |Brit Swede Dane Norwegian German
         Red Green White Yellow Blue
         PallMall Dunhill Prince Blends Bluemasters
         Tea Coffee Water Beer Milk
         Dogs Birds Cats Horses Fish|

        Brit        := (1 %% 5) name:#Brit.
        Swede       := (1 %% 5) name:#Swede.
        Dane        := (1 %% 5) name:#Dane.
        Norwegian   := (1 %% 5) name:#Norwegian.
        German      := (1 %% 5) name:#German.

        Red     := (1 %% 5) name:#Red.
        Green   := (1 %% 5) name:#Green.
        White   := (1 %% 5) name:#White.
        Yellow  := (1 %% 5) name:#Yellow.
        Blue    := (1 %% 5) name:#Blue.

        PallMall     := (1 %% 5) name:#PallMall.
        Dunhill      := (1 %% 5) name:#Dunhill.
        Prince       := (1 %% 5) name:#Prince.
        Blends       := (1 %% 5) name:#Blends.
        Bluemasters  := (1 %% 5) name:#Bluemasters.

        Tea    := (1 %% 5) name:#Tea.
        Coffee := (1 %% 5) name:#Coffee.
        Milk   := (1 %% 5) name:#Milk.
        Water  := (1 %% 5) name:#Water.
        Beer   := (1 %% 5) name:#Beer.

        Dogs         := (1 %% 5) name:#Dogs.
        Birds        := (1 %% 5) name:#Birds.
        Cats         := (1 %% 5) name:#Cats.
        Horses       := (1 %% 5) name:#Horses.
        Fish         := (1 %% 5) name:#Fish.

        Solver allDistinct:{ Brit . Swede . Dane . Norwegian . German. }.
        Solver allDistinct:{ Red. Green. White. Yellow. Blue. }.
        Solver allDistinct:{ PallMall. Dunhill. Prince. Blends. Bluemasters. }.
        Solver allDistinct:{ Tea. Coffee. Milk. Water. Beer }.
        Solver allDistinct:{ Dogs. Birds. Cats. Horses. Fish. }.

        Brit       =: Red.           "/ The Brit lives in the red house.
        Swede      =: Dogs.          "/ The Swede keeps dogs as pets.
        Dane       =: Tea.           "/ The Dane drinks tea.
        Green =: (White - 1).        "/ The green house is on the immediate left of the white house.
        Green =: Coffee.             "/ The green house's owner drinks coffee.
        PallMall   =: Birds.         "/ The owner who smokes Pall Mall rears birds.
        Yellow =: Dunhill.           "/ The owner of the yellow house smokes Dunhill.
        Milk =: 3.                   "/ The owner living in the center house drinks milk.
        Norwegian =: 1.              "/ The Norwegian lives in the first house.
        (Blends - Cats) abs =: 1.    "/ The owner who smokes Blends lives next to the one who keeps cats.
        (Horses - Dunhill) abs =: 1. "/ The owner who keeps the horse lives next to the one who smokes Dunhill.
        Bluemasters =: Beer.         "/ The owner who smokes Bluemasters drinks beer.
        German =: Prince.            "/ The German smokes Prince.
        (Norwegian - Blue) abs =: 1. "/ The Norwegian lives next to the blue house.
        (Blends - Water) abs =: 1.   "/ The owner who smokes Blends lives next to the one who drinks water.
    ].

solution printCR.

"=:" ऑपरेटर को नोटिस करें, जिसका अर्थ है "साथ एकीकृत"। सॉल्वर समस्या ब्लॉक में परिभाषित बाधाओं के सेट पर एक बैकग्राउंड एल्गोरिथ्म का उपयोग करता है। शेष कोड बॉक्स में समाधान प्रिंट करता है:

    |pLine pSorted f |

    pLine := [ 
        (1 to:5) do:[:i | ('-' ,* 15) print] separatedBy:[' ' print].
        '' printCR.
    ].
    pSorted := [:keys |
        |items t|

        items := keys collect:[:k| solution at:k].
        t := keys copy. items values sortWith:t. 
        pLine value:(t collect:[:x | '| ',(x centerPaddedTo:11),' |']).
    ].

    pLine value.
    pSorted value:#( Brit Swede Dane Norwegian German ).
    pSorted value:#( Red Green White Yellow Blue ).
    pSorted value:#( Tea Coffee Milk Water Beer ).
    pSorted value:#( PallMall Dunhill Prince Blends Bluemasters ).
    pSorted value:#( Dogs Birds Cats Horses Fish ).
    pLine value.

    f := solution at:'Fish'.
    ('The %1 in the %2 house owns the fish!'
        bindWith:(#( Brit Swede Dane Norwegian German ) detect:[:n| (solution at:n) == f])
        with:(#( Red Green White Yellow Blue ) detect:[:n| (solution at:n) == f])
            ) printCR.

आउटपुट:

Solution(Brit->3 Swede->5 Dane->2 Norwegian->1 German->4 Red->3 Green->4
 White->5 Yellow->1 Blue->2 PallMall->3 Dunhill->1 Prince->4 Blends->2
 Bluemasters->5 Tea->2 Coffee->4 Milk->3 Water->1 Beer->5 Dogs->5 Birds->3
 Cats->1 Horses->2 Fish->4)
--------------- --------------- --------------- --------------- ---------------
|  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!

संपादित करें: उफ़ - मैं "जो मछली का मालिक है" को सादे संस्करण में मुद्रित करना भूल गया।


3

रूबी 1166 अक्षर

संपादित करें: सही आउटपुट स्वरूप के लिए अपडेट किया गया

MBP i5 पर 0.2 सेकंड से कम में लगातार चलता है।

स्रोत: जीथुब पर देखें

एक लाइन:

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!

अघोषित कोड

@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प्रारंभिक सेटअप का उपयोग हर बार अद्वितीय समाधान पथ सुनिश्चित करता है। आप इसे रनों के बीच परिवर्तन के प्रयासों की संख्या से देख सकते हैं। यह इनपुट में अलग से फीड करने की आवश्यकता को कम करता है, हालांकि ऐसा करने के लिए कोड को बदलना तुच्छ होगा।


2
आउटपुट आवश्यक प्रारूप से मेल नहीं खाता है।
मेगो

पीपीसीजी में, इसका सही समाधान होना पर्याप्त नहीं है। उत्तरों को विनिर्देश से मेल खाना चाहिए , जिसमें इसे सही तरीके से प्रारूपित करना शामिल है।
एलेंडिया स्ट्राटन

2
ठीक है। मूल पोस्ट चुनौती कल्पना को पूरा करने के लिए कोड स्वरूपण के साथ अद्यतन किया गया।
seanh
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.