2424 <INDEX >nondeterministic programs<SUBINDEX >logic puzzles<OPEN /></SUBINDEX ></INDEX >
2525
2626 <TEXT >
27- The following puzzle (taken from
27+ The following puzzle (adapted from
2828 <INDEX >Dinesman, Howard P.</INDEX >
2929 <CITATION >Dinesman 1968</CITATION >)
3030 is typical of a large class of simple logic puzzles:
3131 <BLOCKQUOTE >
32+ The software company
33+ <INDEX >Microshaft</INDEX >
34+ Microshaft is expanding, and Alyssa, Ben, Cy, Lem, and Louis
35+ are moving into a row of five private offices in a
36+ new building. Alyssa does not move into the last office. Ben does not
37+ move into the first office. Cy takes neither the first nor the last office.
38+ Lem moves into an office after Ben's. Louis's office is not next to
39+ Cy's. Cy's office is not next to Ben's. Who moves into which office?
40+ <!--
41+ Baker => Alyssa
42+ Cooper => Ben
43+ Fletcher => Cy
44+ Miller => Lem
45+ Smith => Louis
3246 Baker, Cooper, Fletcher, Miller, and Smith live on different floors of
3347 an apartment house that contains only five floors. Baker does not
3448 live on the top floor. Cooper does not live on the bottom floor.
3549 Fletcher does not live on either the top or the bottom floor. Miller
3650 lives on a higher floor than does Cooper. Smith does not live on a
3751 floor adjacent to Fletcher<APOS/>s. Fletcher does not live on a floor
38- adjacent to Cooper<APOS />s. Where does everyone live?
52+ adjacent to Cooper<APOS/>s. Where does everyone live? -->
3953 </BLOCKQUOTE >
4054 </TEXT >
4155 <TEXT >
42- We can determine who lives on each floor in a straightforward way by
56+ We can determine who moves into which office in a straightforward way by
4357 enumerating all the possibilities and imposing the given
4458 restrictions:<FOOTNOTE >Our program uses the following
4559 <SPLITINLINE >
@@ -88,11 +102,11 @@ function distinct(items) {
88102 </SCHEME >
89103 </SPLITINLINE ></FOOTNOTE >
90104 <SNIPPET CHAP =" 3" VARIANT =" non-det" >
91- <INDEX ><DECLARATION >multiple_dwelling </DECLARATION ></INDEX >
92- <NAME >multiple_dwelling </NAME >
105+ <INDEX ><DECLARATION >office_move </DECLARATION ></INDEX >
106+ <NAME >office_move </NAME >
93107 <REQUIRES >distinct</REQUIRES >
94- <EXAMPLE >multiple_dwelling_example </EXAMPLE >
95- <EXPECTED >[ 'baker ', [ 3, null ] ]</EXPECTED >
108+ <EXAMPLE >office_move_example </EXAMPLE >
109+ <EXPECTED >[ 'alyssa ', [ 3, null ] ]</EXPECTED >
96110 <SCHEME >
97111(define (multiple-dwelling)
98112 (let ((baker (amb 1 2 3 4 5))
@@ -116,38 +130,38 @@ function distinct(items) {
116130 (list 'smith smith))))
117131 </SCHEME >
118132 <JAVASCRIPT >
119- function multiple_dwelling () {
120- const baker = amb(1, 2, 3, 4, 5);
121- const cooper = amb(1, 2, 3, 4, 5);
122- const fletcher = amb(1, 2, 3, 4, 5);
123- const miller = amb(1, 2, 3, 4, 5);
124- const smith = amb(1, 2, 3, 4, 5);
125- require(distinct(list(baker, cooper, fletcher, miller, smith )));
126- require(baker !== 5);
127- require(cooper !== 1);
128- require(fletcher !== 5);
129- require(fletcher !== 1);
130- require(miller > cooper );
131- require(math_abs(smith - fletcher ) !== 1);
132- require(math_abs(fletcher - cooper ) !== 1);
133- return list(list("baker ", baker ),
134- list("cooper ", cooper ),
135- list("fletcher ", fletcher ),
136- list("miller ", miller ),
137- list("smith ", smith ));
133+ function office_move () {
134+ const alyssa = amb(1, 2, 3, 4, 5);
135+ const ben = amb(1, 2, 3, 4, 5);
136+ const cy = amb(1, 2, 3, 4, 5);
137+ const lem = amb(1, 2, 3, 4, 5);
138+ const louis = amb(1, 2, 3, 4, 5);
139+ require(distinct(list(alyssa, ben, cy, lem, louis )));
140+ require(alyssa !== 5);
141+ require(ben !== 1);
142+ require(cy !== 5);
143+ require(cy !== 1);
144+ require(lem > ben );
145+ require(math_abs(louis - cy ) !== 1);
146+ require(math_abs(cy - ben ) !== 1);
147+ return list(list("alyssa ", alyssa ),
148+ list("ben ", ben ),
149+ list("cy ", cy ),
150+ list("lem ", lem ),
151+ list("louis ", louis ));
138152}
139153 </JAVASCRIPT >
140154 </SNIPPET >
141155 <SNIPPET HIDE =" yes" >
142- <NAME >multiple_dwelling_example </NAME >
156+ <NAME >office_move_example </NAME >
143157 <SCHEME >
144158(multiple-dwelling)
145159 </SCHEME >
146160 <JAVASCRIPT >
147- multiple_dwelling ();
161+ office_move ();
148162 </JAVASCRIPT >
149163 <JAVASCRIPT_TEST >
150- head(multiple_dwelling ());
164+ head(office_move ());
151165 </JAVASCRIPT_TEST >
152166 </SNIPPET >
153167 </TEXT >
@@ -156,7 +170,7 @@ head(multiple_dwelling());
156170 Evaluating the expression
157171 <SPLITINLINE ><SCHEME ><SCHEMEINLINE >(multiple-dwelling)</SCHEMEINLINE >
158172 </SCHEME >
159- <JAVASCRIPT ><JAVASCRIPTINLINE >multiple_dwelling ()</JAVASCRIPTINLINE >
173+ <JAVASCRIPT ><JAVASCRIPTINLINE >office_move ()</JAVASCRIPTINLINE >
160174 </JAVASCRIPT >
161175 </SPLITINLINE >
162176 produces the result
@@ -165,8 +179,8 @@ head(multiple_dwelling());
165179((baker 3) (cooper 2) (fletcher 4) (miller 5) (smith 1))
166180 </SCHEME >
167181 <JAVASCRIPT >
168- list(list("baker ", 3), list("cooper ", 2), list("fletcher ", 4),
169- list("miller ", 5), list("smith ", 1))
182+ list(list("alyssa ", 3), list("ben ", 2), list("cy ", 4),
183+ list("lem ", 5), list("louis ", 1))
170184 </JAVASCRIPT >
171185 </SNIPPET >
172186 Although this simple
@@ -175,27 +189,27 @@ list(list("baker", 3), list("cooper", 2), list("fletcher", 4),
175189 <JAVASCRIPT >function</JAVASCRIPT >
176190 </SPLITINLINE >
177191 works, it is very slow.
178- Exercises<SPACE /><REF NAME =" ex:better-multiple-dwelling1 " />
179- and<SPACE /><REF NAME =" ex:better-multiple-dwelling2 " /> discuss some possible
192+ Exercises<SPACE /><REF NAME =" ex:better-office-move1 " />
193+ and<SPACE /><REF NAME =" ex:better-office-move2 " /> discuss some possible
180194 improvements.
181195
182196 <INDEX >nondeterministic programs<SUBINDEX >logic puzzles<CLOSE /></SUBINDEX ></INDEX >
183197 </TEXT >
184198
185199 <EXERCISE >
186- <LABEL NAME =" ex:multiple_dwelling_1 " />
187- Modify the multiple-dwelling
200+ <LABEL NAME =" ex:office_move_1 " />
201+ Modify the office-move
188202 <SPLITINLINE >
189203 <SCHEME >procedure</SCHEME >
190204 <JAVASCRIPT >function</JAVASCRIPT >
191205 </SPLITINLINE >
192- to omit the requirement that Smith and Fletcher do not live on adjacent
193- floors. How many solutions are there to this modified puzzle?
206+ to omit the requirement that Louis's office is not next to Cy's.
207+ How many solutions are there to this modified puzzle?
194208 </EXERCISE >
195209
196210 <EXERCISE >
197- <LABEL NAME =" ex:better-multiple-dwelling1 " />
198- Does the order of the restrictions in the multiple-dwelling
211+ <LABEL NAME =" ex:better-office-move1 " />
212+ Does the order of the restrictions in the office-move
199213 <SPLITINLINE >
200214 <SCHEME >procedure</SCHEME >
201215 <JAVASCRIPT >function</JAVASCRIPT >
@@ -207,18 +221,18 @@ list(list("baker", 3), list("cooper", 2), list("fletcher", 4),
207221 </EXERCISE >
208222
209223 <EXERCISE >
210- <LABEL NAME =" ex:better-multiple-dwelling2 " />
211- In the multiple dwelling problem, how many sets of assignments are
212- there of people to floors , both before and after the requirement that
213- floor assignments be distinct? It is very inefficient to generate all
214- possible assignments of people to floors and then leave it to
224+ <LABEL NAME =" ex:better-office-move2 " />
225+ In the office move problem, how many sets of assignments are
226+ there of people to offices , both before and after the requirement that
227+ office assignments be distinct? It is very inefficient to generate all
228+ possible assignments of people to offices and then leave it to
215229 backtracking to eliminate them. For example, most of the restrictions
216- depend on only one or two of the person-floor
230+ depend on only one or two of the person-office
217231 <SPLITINLINE >
218232 <SCHEME >variables,</SCHEME >
219233 <JAVASCRIPT >names,</JAVASCRIPT >
220234 </SPLITINLINE >
221- and can thus be imposed before floors have been selected for all the people.
235+ and can thus be imposed before offices have been selected for all the people.
222236 Write and demonstrate a much more efficient nondeterministic
223237 <SPLITINLINE >
224238 <SCHEME >procedure</SCHEME >
@@ -235,22 +249,57 @@ list(list("baker", 3), list("cooper", 2), list("fletcher", 4),
235249 </EXERCISE >
236250
237251 <EXERCISE >
238- <LABEL NAME =" ex:multiple_dwelling_4 " />
252+ <LABEL NAME =" ex:office_move_4 " />
239253 Write an ordinary
240254 <INDEX >nondeterministic programming vs.<SPACE />JavaScript programming</INDEX >
241255 <SPLITINLINE >
242256 <SCHEME >Scheme</SCHEME >
243257 <JAVASCRIPT >JavaScript</JAVASCRIPT >
244258 </SPLITINLINE >
245- program to solve the multiple dwelling puzzle.
259+ program to solve the office move puzzle.
246260 </EXERCISE >
247261
248262 <EXERCISE >
249263 <LABEL NAME =" ex:liars" />
250- Solve the following <QUOTE >Liars</QUOTE > puzzle (from
264+ Solve the following <QUOTE >Liars</QUOTE > puzzle (adapted from
251265 <INDEX >Phillips, Hubert</INDEX >
252266 <CITATION >Phillips 1934</CITATION >):
253267 <BLOCKQUOTE >
268+ Alyssa, Cy, Eva, Lem, and Louis meet for a business lunch at SoSoService.
269+ Their meals arrive one after the other, a considerable time after they
270+ placed their orders. To entertain Ben, who expects them back at the office
271+ for a meeting, they decide to each make one true statement and one false
272+ statement about their orders:
273+ <UL >
274+ <LI >
275+ Alyssa:
276+ <QUOTE >Lem's meal arrived second. Mine arrived third.</QUOTE >
277+ </LI >
278+ <LI >
279+ Cy:
280+ <QUOTE >Mine arrived first. Eva's arrived second.</QUOTE >
281+ </LI >
282+ <LI >
283+ Eva:
284+ <QUOTE >Mine arrived third, and poor Cy's arrived last.</QUOTE >
285+ </LI >
286+ <LI >
287+ Lem:
288+ <QUOTE >Mine arrived second. Louis's arrived fourth.</QUOTE >
289+ </LI >
290+ <LI >
291+ Louis:
292+ <QUOTE >Mine arrived fourth. Alyssa's meal arrived first.</QUOTE >
293+ </LI >
294+ </UL >
295+ What was the real order in which the five diners received their meals?
296+ <!--
297+ Betty => Alyssa
298+ Ethel => Cy
299+ Joan => Eva
300+ Kitty => Lem
301+ Mary => Louis
302+
254303 Five schoolgirls sat for an examination. Their parents<EMDASH/>so they
255304 thought<EMDASH/>showed an undue degree of interest in the result. They
256305 therefore agreed that, in writing home about the examination, each
@@ -276,31 +325,67 @@ list(list("baker", 3), list("cooper", 2), list("fletcher", 4),
276325 Mary: <QUOTE>I was fourth. Top place was taken by Betty.</QUOTE>
277326</LI>
278327 </UL>
279- What in fact was the order in which the five girls were placed?
328+ What in fact was the order in which the five girls were placed? -->
280329 </BLOCKQUOTE >
281330 </EXERCISE >
282331
283332 <EXERCISE >
284- <LABEL NAME =" ex:yacht " />
333+ <LABEL NAME =" ex:checking " />
285334 Use the <SCHEMEINLINE >amb</SCHEMEINLINE > evaluator to solve the following
286- puzzle:<FOOTNOTE >This is taken from a booklet called <QUOTE >Problematical
287- Recreations,</QUOTE > published in the 1960s by Litton Industries, where it
288- is attributed to the <EM >Kansas State Engineer</EM >.</FOOTNOTE >
289- <BLOCKQUOTE >
290- Mary Ann Moore<APOS />s father has a yacht and so has each of his four
291- friends: Colonel Downing, Mr.<SPACE />Hall, Sir Barnacle Hood, and
292- Dr.<SPACE />Parker.
293- Each of the five also has one daughter and each has named his
294- yacht after a daughter of one of the others. Sir Barnacle<APOS />s yacht is
295- the Gabrielle, Mr.<SPACE />Moore owns the Lorna; Mr.<SPACE />Hall the Rosalind. The
296- Melissa, owned by Colonel Downing, is named after Sir Barnacle<APOS />s
297- daughter. Gabrielle<APOS />s father owns the yacht that is named after
298- Dr.<SPACE />Parker<APOS />s daughter. Who is Lorna<APOS />s father?
299- </BLOCKQUOTE >
300- Try to write the program so that it runs efficiently (see
301- exercise<SPACE /><REF NAME =" ex:better-multiple-dwelling2" />). Also determine
302- how many solutions there are if we are not told that Mary Ann<APOS />s last
303- name is Moore.
335+ puzzle (adapted from
336+ <INDEX >Phillips, Hubert</INDEX >
337+ <CITATION >Phillips 1961</CITATION >):
338+ <BLOCKQUOTE >
339+ Alyssa, Ben, Cy, Eva, and Louis each pick a different chapter of SICP JS
340+ and solve all the exercises in that chapter.
341+ Louis solves the exercises in the <QUOTE >Functions</QUOTE > chapter,
342+ Alyssa the ones in the <QUOTE >Data</QUOTE > chapter, and
343+ Cy the ones in the <QUOTE >State</QUOTE > chapter.
344+ They decide to check each other's work, and
345+ Alyssa volunteers to check the exercises in the <QUOTE >Meta</QUOTE > chapter.
346+ The exercises in the <QUOTE >Register Machines</QUOTE > chapter are solved by Ben
347+ and checked by Louis.
348+ The person who checks the exercises in the <QUOTE >Functions</QUOTE > chapter
349+ solves the exercises that are checked by Eva.
350+ Who checks the exercises in the <QUOTE >Data</QUOTE > chapter?
351+ </BLOCKQUOTE >
352+ Try to write the program so that it runs efficiently (see
353+ exercise<SPACE /><REF NAME =" ex:better-office-move2" />). Also determine
354+ how many solutions there are if we are not told that Alyssa checks the
355+ exercises in the <QUOTE >Meta</QUOTE > chapter.
356+ <!-- <BLOCKQUOTE>
357+ A father x owning a yacht called y.
358+ =>
359+ A person x solving the exercises in chapter y.
360+
361+ A father x having a daughter called y.
362+ =>
363+ A person x checking the exercises in chapter y.
364+
365+ Moore => Alyssa! Mary Ann => Meta!
366+ Sir Barnacle Hood => Louis! Gabrielle => Functions!
367+ Hall => Cy! Lorna => Data!
368+ Downing => Ben! Rosalind => State!
369+ Parker => Eva! Melissa => Register Machines!
370+
371+ Mary Ann Moore<APOS/>s father has a yacht and so has each of his four
372+ friends: Colonel Downing, Mr.<SPACE/>Hall, Sir Barnacle Hood, and
373+ Dr.<SPACE/>Parker.
374+ Each of the five also has one daughter and each has named his
375+ yacht after a daughter of one of the others.
376+ Sir Barnacle<APOS/>s yacht is the Gabrielle,
377+ Mr.<SPACE/>Moore owns the Lorna;
378+ Mr.<SPACE/>Hall the Rosalind.
379+ The Melissa, owned by Colonel Downing,
380+ is named after Sir Barnacle<APOS/>s daughter.
381+ Gabrielle<APOS/>s father owns the yacht that is named after
382+ Dr.<SPACE/>Parker<APOS/>s daughter.
383+ Who is Lorna<APOS/>s father?
384+ </BLOCKQUOTE>
385+ Try to write the program so that it runs efficiently (see
386+ exercise<SPACE/><REF NAME="ex:better-office-move2"/>). Also determine
387+ how many solutions there are if we are not told that Mary Ann<APOS/>s last
388+ name is Moore. -->
304389 </EXERCISE >
305390
306391 <INDEX >puzzles<SUBINDEX >logic puzzles<CLOSE /></SUBINDEX ></INDEX >
0 commit comments