Cheryl's birthday: Difference between revisions
Content added Content deleted
m (→{{header|FORTRAN}}: RCuses Fortran, not FORTRAN) |
(Added Algol 68) |
||
Line 236: | Line 236: | ||
JULY, 16 |
JULY, 16 |
||
</pre> |
</pre> |
||
=={{header|ALGOL 68}}== |
|||
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}} |
|||
<syntaxhighlight lang="algol68"> |
|||
BEGIN # Cheryl's birthday puzzle # |
|||
[ 1 : 4, 1 : 6 ]INT dates # non-zero indicates a possible date # |
|||
:= ( ( 0, 15, 16, 0, 0, 19 ) # may # |
|||
, ( 0, 0, 0, 17, 18, 0 ) # june # |
|||
, ( 14, 0, 16, 0, 0, 0 ) # july # |
|||
, ( 14, 15, 0, 17, 0, 0 ) # august # |
|||
); |
|||
[]STRING month name = ( "May", "June", "July", "August" ); |
|||
print( ( "Cheryl tells Albert the month and Bernard the day", newline ) ); |
|||
print( ( "Albert doesn't know the date and knows Bernard doesn't either", newline ) ); |
|||
FOR d TO 2 UPB dates DO # elimiate the months with unique days # |
|||
INT day count := 0; |
|||
INT day := 0; |
|||
INT month := 0; |
|||
FOR m TO 1 UPB dates DO |
|||
IF dates[ m, d ] /= 0 THEN |
|||
day count +:= 1; |
|||
day := dates[ m, d ]; |
|||
month := m |
|||
FI |
|||
OD; |
|||
IF day count = 1 THEN |
|||
print( ( " Eliminating ", month name[ month ], ", ", whole( day, 0 ), "th is unique", newline ) ); |
|||
FOR p TO 2 UPB dates DO dates[ month, p ] := 0 OD |
|||
FI |
|||
OD; |
|||
print( ( "Bernard now knows the date", newline ) ); |
|||
FOR d TO 2 UPB dates DO # eliminate the days that aren't unique # |
|||
INT day count := 0; |
|||
INT day := 0; |
|||
INT month := 0; |
|||
FOR m TO 1 UPB dates DO |
|||
IF dates[ m, d ] /= 0 THEN |
|||
day count +:= 1; |
|||
day := dates[ m, d ]; |
|||
month := m |
|||
FI |
|||
OD; |
|||
IF day count > 1 THEN |
|||
print( ( " Eliminating ", whole( day, 0 ), "th, it is non-unique", newline ) ); |
|||
FOR p TO 1 UPB dates DO dates[ p, d ] := 0 OD |
|||
FI |
|||
OD; |
|||
print( ( "Albert now knows the date", newline ) ); |
|||
FOR m TO 1 UPB dates DO # eliminate months with non-unique days # |
|||
INT day count := 0; |
|||
INT day := 0; |
|||
INT month := 0; |
|||
FOR d TO 2 UPB dates DO |
|||
IF dates[ m, d ] /= 0 THEN |
|||
day count +:= 1; |
|||
day := dates[ m, d ]; |
|||
month := m |
|||
FI |
|||
OD; |
|||
IF day count > 1 THEN |
|||
print( ( " Eliminating ", month name[ m ], ", it has multiple days", newline ) ); |
|||
FOR p TO 2 UPB dates DO dates[ m, p ] := 0 OD |
|||
FI |
|||
OD; |
|||
print( ( "Cheryl's birthday: " ) ); # show the solution(s) # |
|||
FOR m TO 1 UPB dates DO |
|||
FOR d TO 2 UPB dates DO |
|||
IF dates[ m, d ] /= 0 THEN |
|||
print( ( " ", month name[ m ], " ", whole( dates[ m, d ], 0 ), "th" ) ) |
|||
FI |
|||
OD |
|||
OD; |
|||
print( ( newline ) ) |
|||
END |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Cheryl tells Albert the month and Bernard the day |
|||
Albert doesn't know the date and knows Bernard doesn't either |
|||
Eliminating June, 18th is unique |
|||
Eliminating May, 19th is unique |
|||
Bernard now knows the date |
|||
Eliminating 14th, it is non-unique |
|||
Albert now knows the date |
|||
Eliminating August, it has multiple days |
|||
Cheryl's birthday: July 16th |
|||
</pre> |
|||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
<syntaxhighlight lang="applescript">use AppleScript version "2.4" |
<syntaxhighlight lang="applescript">use AppleScript version "2.4" |