ไขปริศนาของไอน์สไตน์


19

Einstein เขียนปริศนา เขาบอกว่า 98% ของโลกไม่สามารถแก้ไขได้

ฉันไขปริศนานี้ได้ในเวลาประมาณ 25 นาทีขณะที่อยู่บนรถไฟเพื่อทำงาน มันเป็นเพียงแค่การหักเงิน

ปัญหาลับสมอง

มี 5 บ้านใน 5 สีที่แตกต่างกันในแถว
ในแต่ละบ้านมีบุคคลที่มีสัญชาติแตกต่างกัน
เจ้าของ 5 คนดื่มเครื่องดื่มบางประเภทสูบบุหรี่ยี่ห้อหนึ่งและรักษาสัตว์เลี้ยง
เจ้าของไม่มีสัตว์เลี้ยงตัวเดียวกันสูบซิการ์ยี่ห้อเดียวกันหรือดื่มเครื่องดื่มชนิดเดียวกัน
คำถาม: ใครเป็นเจ้าของปลา

วิธีแก้ปริศนา Einstein นี้ให้คำใบ้ 15 คำ:

  1. ชาวอังกฤษอาศัยอยู่ในบ้านสีแดง
  2. ชาวสวีเดนเลี้ยงสุนัขเป็นสัตว์เลี้ยง
  3. ชาวเดนมาร์กดื่มชา
  4. บ้านสีเขียวอยู่ทางด้านซ้ายของทำเนียบขาว
  5. เจ้าของบ้านสีเขียวดื่มกาแฟ
  6. เจ้าของที่สูบบุหรี่พอลมอลล์จะดักนกไว้
  7. เจ้าของบ้านสีเหลืองสูบบุหรี่ Dunhill
  8. เจ้าของอาศัยอยู่ในบ้านกลางดื่มนม
  9. ชาวนอร์เวย์อาศัยอยู่ในบ้านหลังแรก
  10. เจ้าของที่สูบบุหรี่ Blends อาศัยอยู่ถัดจากคนที่เลี้ยงแมว
  11. เจ้าของที่ดูแลม้าอยู่ข้างๆผู้ที่สูบ Dunhill
  12. เจ้าของที่สูบบุหรี่ Bluemasters ดื่มเบียร์
  13. เจ้าชายเยอรมันสูบบุหรี่
  14. ชาวนอร์เวย์อาศัยอยู่ข้างๆบ้านสีน้ำเงิน
  15. เจ้าของที่สูบบุหรี่ Blends ชีวิตถัดจากคนที่ดื่มน้ำ

ด้วยคำแนะนำเหล่านี้คุณสามารถแก้ไขปัญหาได้

งานของคุณ: สร้างโปรแกรมที่จะไขปริศนานี้ให้คุณ ไม่อนุญาตให้เข้ารหัสอย่างหนักโซลูชัน (duh)

มันได้รับอนุญาตให้ hardcode คำแนะนำในรูปแบบใด ๆ
รูปแบบตัวอย่าง:

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

== หมายถึงเท่ากับ
/ หมายถึงที่ด้านซ้ายของ
\ หมายถึงด้านขวาของ
/ \ หมายถึงซ้ายหรือขวาของ

เช่นฉันบอกว่ามันอนุญาตให้ทั้งคำแนะนำ hardcode หรือให้พวกเขาเป็นข้อมูล

ผลลัพธ์: ผลลัพธ์ควรอยู่ในรูปแบบต่อไปนี้ (ด้วยค่าที่ถูกต้องเพียงบอกว่าโทรลล์;)

 _____________    _____________    _____________    _____________    _____________
|   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 ได้ตราบใดที่กล่องเหล่านั้นไม่สำคัญว่าคุณจะใช้สัญลักษณ์ใด

ความรู้ใด ๆ เกี่ยวกับปริศนาและการแก้ปัญหานี้ไม่สามารถใช้ในโปรแกรม มันควรใช้ตรรกะที่บริสุทธิ์และการหักเงินเพื่อแก้ปริศนา

ติดแท็กสิ่งนี้เป็น codegolf แต่อาจเป็นรหัสที่ท้าทายไม่แน่ใจ ความคิดใด ๆ เกี่ยวกับเกณฑ์การชนะสำหรับการท้าทายรหัสสามารถแบ่งปันได้ :)

สำหรับตอนนี้นี่คือโค้ดกอล์ฟดังนั้นโปรแกรมที่มีจำนวนไบต์ต่ำสุดชนะ

ขอให้โชคดีและการเข้ารหัสที่มีความสุข :)


15
การอ้างสิทธิ์ในย่อหน้าแรกเกือบจะเป็นเท็จอย่างแน่นอน
Peter Taylor

1
@PeterTaylor คุณหมายถึงอะไร? 98% กับบิตของ einstein หรือไม่ จะไม่ทราบว่าแน่นอน interwebs พูดและไม่คำนึงถึงมันสนุกสำหรับพื้นหลัง :) ฉันรู้สึกพิเศษเมื่อฉันแก้ไข "yay im ใน 2% เหล่านั้น"
Teun Pronk

4
ทั้งสองชิ้นนั้น interwebs ยังมี Timecube ดังนั้นคุณจะไม่ควรเชื่อทุกสิ่งที่พวกเขาพูดโดยเฉพาะอย่างยิ่งเมื่อพวกเขาไม่ได้เพิ่มหลักฐานใด ๆ
Peter Taylor

1
@ PeterTaylor ใช่ฉันรู้ แต่ชอบฉันยังกล่าวว่ามันสนุกสำหรับพื้นหลัง แต่อย่างใดและไม่ชอบความจริงหรือนิยายเป็นสิ่งสำคัญสำหรับปัญหาที่กำหนด วิกิพีเดียยังไม่มีหลักฐานที่แท้จริงสำหรับมัน :)
Teun Pronk

3
ฉันคิดว่าวิธีแก้ปัญหาควรจะสามารถพูดคุยกับจำนวนบ้านคุณสมบัติและคำแนะนำโดยพลการ แล้วมันจะเป็นคำถามที่น่าสนใจอย่างแท้จริงซึ่งคุณไม่จำเป็นต้องเขียนในกฎ "no hardcoding" ซึ่งในประสบการณ์ของฉันมักจะนำไปสู่คำถามที่ว่าฮาร์ดโค้ดคือเท่าใด
Cruncher

คำตอบ:


24

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

สำคัญ:

  • คอลัมน์แรกคือหมายเลขบ้าน
  • คอลัมน์ที่สองคือสัญชาติ
  • คอลัมน์ที่สามเป็นสีโปรด
  • คอลัมน์ที่สี่คือสัตว์เลี้ยง
  • คอลัมน์ที่ห้าคือเครื่องดื่ม และ
  • คอลัมน์ที่หกคือบุหรี่ (r = prince, l = bluemaster)

9
นี่คือภาษาที่เหมาะสมสำหรับงาน
marinus

คุณไม่ควรสะกดชื่อเพื่อให้ตัวเลขกอล์ฟเปรียบเทียบได้หรือไม่
blabla999

5

Ruby 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ห้าครั้งเพื่อค้นหาวิธีการสั่งซื้ออาร์เรย์ของเชื้อชาติสีเครื่องดื่มสูบบุหรี่และสัตว์เลี้ยง การวนซ้ำที่ยาวนานคือc.product(*d){|a|...}เรียกArray#productซ้ำคำตอบที่เป็นไปได้เกือบ 25 พันล้านรายการ (f=eval ...)&&(...)ร่างกายห่วงมีรูปแบบ eval ...ส่วนประเมินการแสดงออกเชิงตรรกะ หากทั้งหมดเป็นจริงfหมายเลขบ้านของปลาและ&&(...)ส่วนจะพิมพ์คำตอบ หมายเลขบ้าน 0 เป็นจริงใน Ruby

รหัสกอล์ฟหมายถึงไม่เพิ่มรหัสสำหรับความเร็ว! แต่ฉันไม่มีเวลา 75 วันในการเปิดโปรแกรม!


1
แม้แต่การใช้เบาะแสหนึ่งหรือสองแทนที่จะใช้กำลังดุร้ายก็จะช่วยประหยัดการค้นหาได้มาก
qwr

ผมไม่ทราบว่าทับทิม แต่อินเทอร์เน็ตบอกว่ามันเป็นไปอย่างช้า ฉันเดิมพันว่าหากคุณแปลคำตอบของคุณเป็นภาษาที่คอมไพล์แล้วปรับให้เหมาะสมแม้แต่เล็กน้อยคุณอาจเข้าใกล้ (r?) ถึงความเร็ว 5000% ที่คุณต้องการเพื่อยืนยันความถูกต้องในเวลาที่เหมาะสม หากคุณเขียนแม้แต่การแปลภาษาที่หยาบคาย แต่มีเอกสารที่ดีแปลภาษา Java ฉันยินดีที่จะปรับให้เหมาะสมสำหรับคุณแล้วตรวจสอบความถูกต้อง
Rainbolt

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 อักขระ
ทำให้เสียชีวิต

1
ทดสอบกับ SWI-Prolog เพียงพิมพ์?
ej8000

3

ตัวอักษรขนาดเล็ก 1332 ตัวอักษร

ใช้ Smalltalk ธรรมดา:

|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

เนื่องจากเวอร์ชัน golfed เกือบอ่านไม่ได้นี่คือรหัสนักพัฒนาที่มีชื่อการเยื้องและช่องว่างที่เหมาะสมสำหรับการอ่าน:

|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 / Mozart ตามที่ฉันคาดหวังว่าคุณจะบ่นว่าสิ่งนี้ไม่ถูกต้องสำหรับการแข่งขัน ;-) มันถูกเพิ่มเข้ามาเพื่อความสนุกและแรงบันดาลใจของคุณเท่านั้น

|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

ตัวอักษร Ruby 1166

แก้ไข: อัปเดตสำหรับรูปแบบผลลัพธ์ที่ถูกต้อง

ทำงานอย่างสม่ำเสมอภายในไม่เกิน 0.2 วินาทีสำหรับ MBP i5

ที่มา: ดูบน 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}}}}}

บุด้วย newlines เพื่อให้พอดีกับหน้า:

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
เอาต์พุตไม่ตรงกับรูปแบบที่ต้องการ
Mego

ที่ PPCG ที่นี่ไม่เพียงพอที่จะมีทางออกที่ถูกต้อง คำตอบต้องตรงกับข้อกำหนดซึ่งรวมถึงการจัดรูปแบบให้ถูกวิธี
El'endia Starman

2
ตกลง. โพสต์ต้นฉบับอัปเดตด้วยรหัสการจัดรูปแบบเพื่อตอบสนองความท้าทาย
seanh
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.