Skip to content

Instantly share code, notes, and snippets.

@cryks
Created October 19, 2010 14:53
Show Gist options
  • Save cryks/634321 to your computer and use it in GitHub Desktop.
Save cryks/634321 to your computer and use it in GitHub Desktop.
結局ただの backtrack
/*
http://www.shido.info/lisp/scheme_amb.html
SICP の問題 4.42. を解いてみましょう。問題は以下の通りです:
5人の女子生徒が試験を受けた。彼女らの両親は結果に対し過度の関心を持っている、と彼女らは考えている。 そこで彼女らは自宅へ試験についての手紙を書くのに、誰もが1つの正しい情報と1つのうその情報を書こうと 約束した。以下は彼女らの手紙の関係する部分である。
Betty: 「Kitty は試験が2番で私は3番でした。」
Ethel: 「私がトップと聞いてうれしいでしょう。Joan が2ばんでした。」
Joan: 「私は3番でした。可哀想な Ethel はビリでした。」
Kitty: 「私は2番になりました。Mary は4番でしかありませんでした。」
Mary: 「私は4番でした。トップの座は Betty がとりました。」
5人の女子生徒の本当の順番はどうなっているのか。
Exercise 4.42. Solve the following ``Liars'' puzzle (from Phillips 1934):
Five schoolgirls sat for an examination. Their parents -- so they thought -- showed an undue degree of interest in the result. They therefore agreed that, in writing home about the examination, each girl should make one true statement and one untrue one. The following are the relevant passages from their letters:
* Betty: ``Kitty was second in the examination. I was only third.''
* Ethel: ``You'll be glad to hear that I was on top. Joan was second.''
* Joan: ``I was third, and poor old Ethel was bottom.''
* Kitty: ``I came out second. Mary was only fourth.''
* Mary: ``I was fourth. Top place was taken by Betty.''
What in fact was the order in which the five girls were placed?
*/
var range = Enumerable.Range(1, 5);
range.SelectMany(Betty =>
range.SelectMany(Ethel =>
range.SelectMany(Joan =>
range.SelectMany(Kitty =>
range.Select(Mary =>
new { Betty, Ethel, Joan, Kitty, Mary }
)))))
.Where(d => d.Kitty == 2 ^ d.Betty == 3)
.Where(d => d.Ethel == 1 ^ d.Joan == 2)
.Where(d => d.Joan == 3 ^ d.Ethel == 5)
.Where(d => d.Kitty == 2 ^ d.Mary == 4)
.Where(d => d.Mary == 4 ^ d.Betty == 1)
.Where(d => new[] { d.Betty, d.Ethel, d.Joan, d.Kitty, d.Mary }.Distinct().Count() == 5)
.Run(Console.WriteLine)
;
// { Betty = 3, Ethel = 5, Joan = 2, Kitty = 1, Mary = 4 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment