Hourglass puzzle: Difference between revisions
Content added Content deleted
m (used the plural form of "hourglass", added whitespace and highlighting.) |
m (moved perl/phix below logo/nim) |
||
Line 124: | Line 124: | ||
Use hourglasses from step 5 to step 17 (inclusive) to sum 36 using [5, 7, 31] |
Use hourglasses from step 5 to step 17 (inclusive) to sum 36 using [5, 7, 31] |
||
</pre> |
</pre> |
||
=={{header|Logo}}== |
|||
tested with FMSlogo |
|||
<lang logo> |
|||
to bb |
|||
Make "small_capacity 4 |
|||
Make "big_capacity 7 |
|||
make "small 0 |
|||
make "big 0 |
|||
make "t 0 |
|||
print "_____________decision_0_game_over |
|||
print "_________decision_1_start_timing |
|||
print "_______decision_2_flip_small |
|||
print "____decision_3_flip_big |
|||
print "__decision_4_flip_both |
|||
print "_________any_other_number________________wait |
|||
do.until [show list list :small :big :t print "your_decision_0_1_2_3_4 human_decision if :my_decision>1 [machine_computes] ] [:my_decision=0] |
|||
print list :t "minutes_passed |
|||
end |
|||
to human_decision |
|||
make "my_decision readword |
|||
if :my_decision=1 [print "reset_timer make "t 0] |
|||
if :my_decision=2 [print "flip_small make "small :small_capacity-:small] |
|||
if :my_decision=3 [print "flip_big make "big :big_capacity-:big] |
|||
if :my_decision=4 [print "flip_both make "small :small_capacity-:small make "big :big_capacity-:big ] |
|||
if :my_decision>4 [print "wait] |
|||
end |
|||
to machine_computes |
|||
ifelse :small>:big [make "my_selection :big] [make "my_selection :small] |
|||
if :small=0 [make "my_selection :big] |
|||
if :big=0 [make "my_selection :small] |
|||
make "small :small-:my_selection |
|||
make "big :big-:my_selection |
|||
make "t :t+:my_selection |
|||
if :small<0 [make "small 0] |
|||
if :big<0 [make "big 0] |
|||
end |
|||
to zzz |
|||
;A. 7 minutes with 4- and 5-minute timers |
|||
;B. 15 minutes with 7- and 11-minute timers |
|||
;C. 14 minutes with 5- and 8-minute timers |
|||
ifelse YesNoBox [Welcome] [run / show me the code] [bb] [edall] |
|||
;A is possible: Turn both the 5 and the 4. When the 4 runs out, flip it over.Now, when the 5 runs out, start timing. The 4 will run for three more minutes, after which, you can flip it over to reach 7. |
|||
;B is possible: Turn both the 7 and the 11. When the 7 runs out, start timing. The 11 will run for 4 more minutes, after which it can be flipped to reach 15. |
|||
;C is possible: Turn both the 5 and the 8. When the 5 runs out, flip it. The 8 will then run out after 3 minutes, leaving 2 minutes in the 5. Flip the 8 then. When the 5 runs out, start timing. There are now 6 minutes left in the 8, and flipping the 8 after those 6 minutes gives 6 + 8 = 14 minutes. |
|||
end |
|||
Make "big 0 |
|||
Make "big_capacity 5 |
|||
Make "my_decision " |
|||
Make "my_selection 4 |
|||
Make "small 0 |
|||
Make "small_capacity 4 |
|||
Make "startup [zzz] |
|||
Make "t 0 |
|||
</lang> |
|||
=={{header|Nim}}== |
|||
{{trans|Wren}} |
|||
<lang Nim>import math, strutils |
|||
func hourglassFlipper(hourglasses: openArray[int]; |
|||
target: int): tuple[start: int; series: seq[int]] = |
|||
var flippers = @hourglasses |
|||
for _ in 0..10_000: |
|||
let n = min(flippers) |
|||
result.series.add n |
|||
for i in 0..flippers.high: |
|||
dec flippers[i], n |
|||
if flippers[i] == 0: flippers[i] = hourglasses[i] |
|||
result.start = result.series.high |
|||
while result.start >= 0: |
|||
if sum(result.series[result.start..^1]) == target: return |
|||
dec result.start |
|||
raise newException(ValueError, "Unable to find an answer within 10_000 iterations.") |
|||
echo "Flip an hourglass every time it runs out of grains, " |
|||
echo "and note the interval in time." |
|||
const Tests = [(@[4, 7], 9), (@[5, 7, 31], 36)] |
|||
for test in Tests: |
|||
let |
|||
hourglasses = test[0] |
|||
target = test[1] |
|||
(start, series) = hourglassFlipper(hourglasses, target) |
|||
`end` = series.high |
|||
echo "\nSeries: ", series.join(" ") |
|||
echo "Use hourglasses from indices $1 to $2 (inclusive) to sum ".format(start, `end`), |
|||
"$1 using $2.".format(target, hourglasses.join(" "))</lang> |
|||
{{out}} |
|||
<pre>Flip an hourglass every time it runs out of grains, |
|||
and note the interval in time. |
|||
Series: 4 3 1 4 2 2 |
|||
Use hourglasses from indices 2 to 5 (inclusive) to sum 9 using 4 7. |
|||
Series: 5 2 3 4 1 5 1 4 3 2 1 4 5 2 3 4 1 |
|||
Use hourglasses from indices 4 to 16 (inclusive) to sum 36 using 5 7 31.</pre> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
Line 309: | Line 413: | ||
At t=19, stop timer |
At t=19, stop timer |
||
</pre> |
</pre> |
||
=={{header|Logo}}== |
|||
tested with FMSlogo |
|||
<lang logo> |
|||
to bb |
|||
Make "small_capacity 4 |
|||
Make "big_capacity 7 |
|||
make "small 0 |
|||
make "big 0 |
|||
make "t 0 |
|||
print "_____________decision_0_game_over |
|||
print "_________decision_1_start_timing |
|||
print "_______decision_2_flip_small |
|||
print "____decision_3_flip_big |
|||
print "__decision_4_flip_both |
|||
print "_________any_other_number________________wait |
|||
do.until [show list list :small :big :t print "your_decision_0_1_2_3_4 human_decision if :my_decision>1 [machine_computes] ] [:my_decision=0] |
|||
print list :t "minutes_passed |
|||
end |
|||
to human_decision |
|||
make "my_decision readword |
|||
if :my_decision=1 [print "reset_timer make "t 0] |
|||
if :my_decision=2 [print "flip_small make "small :small_capacity-:small] |
|||
if :my_decision=3 [print "flip_big make "big :big_capacity-:big] |
|||
if :my_decision=4 [print "flip_both make "small :small_capacity-:small make "big :big_capacity-:big ] |
|||
if :my_decision>4 [print "wait] |
|||
end |
|||
to machine_computes |
|||
ifelse :small>:big [make "my_selection :big] [make "my_selection :small] |
|||
if :small=0 [make "my_selection :big] |
|||
if :big=0 [make "my_selection :small] |
|||
make "small :small-:my_selection |
|||
make "big :big-:my_selection |
|||
make "t :t+:my_selection |
|||
if :small<0 [make "small 0] |
|||
if :big<0 [make "big 0] |
|||
end |
|||
to zzz |
|||
;A. 7 minutes with 4- and 5-minute timers |
|||
;B. 15 minutes with 7- and 11-minute timers |
|||
;C. 14 minutes with 5- and 8-minute timers |
|||
ifelse YesNoBox [Welcome] [run / show me the code] [bb] [edall] |
|||
;A is possible: Turn both the 5 and the 4. When the 4 runs out, flip it over.Now, when the 5 runs out, start timing. The 4 will run for three more minutes, after which, you can flip it over to reach 7. |
|||
;B is possible: Turn both the 7 and the 11. When the 7 runs out, start timing. The 11 will run for 4 more minutes, after which it can be flipped to reach 15. |
|||
;C is possible: Turn both the 5 and the 8. When the 5 runs out, flip it. The 8 will then run out after 3 minutes, leaving 2 minutes in the 5. Flip the 8 then. When the 5 runs out, start timing. There are now 6 minutes left in the 8, and flipping the 8 after those 6 minutes gives 6 + 8 = 14 minutes. |
|||
end |
|||
Make "big 0 |
|||
Make "big_capacity 5 |
|||
Make "my_decision " |
|||
Make "my_selection 4 |
|||
Make "small 0 |
|||
Make "small_capacity 4 |
|||
Make "startup [zzz] |
|||
Make "t 0 |
|||
</lang> |
|||
=={{header|Nim}}== |
|||
{{trans|Wren}} |
|||
<lang Nim>import math, strutils |
|||
func hourglassFlipper(hourglasses: openArray[int]; |
|||
target: int): tuple[start: int; series: seq[int]] = |
|||
var flippers = @hourglasses |
|||
for _ in 0..10_000: |
|||
let n = min(flippers) |
|||
result.series.add n |
|||
for i in 0..flippers.high: |
|||
dec flippers[i], n |
|||
if flippers[i] == 0: flippers[i] = hourglasses[i] |
|||
result.start = result.series.high |
|||
while result.start >= 0: |
|||
if sum(result.series[result.start..^1]) == target: return |
|||
dec result.start |
|||
raise newException(ValueError, "Unable to find an answer within 10_000 iterations.") |
|||
echo "Flip an hourglass every time it runs out of grains, " |
|||
echo "and note the interval in time." |
|||
const Tests = [(@[4, 7], 9), (@[5, 7, 31], 36)] |
|||
for test in Tests: |
|||
let |
|||
hourglasses = test[0] |
|||
target = test[1] |
|||
(start, series) = hourglassFlipper(hourglasses, target) |
|||
`end` = series.high |
|||
echo "\nSeries: ", series.join(" ") |
|||
echo "Use hourglasses from indices $1 to $2 (inclusive) to sum ".format(start, `end`), |
|||
"$1 using $2.".format(target, hourglasses.join(" "))</lang> |
|||
{{out}} |
|||
<pre>Flip an hourglass every time it runs out of grains, |
|||
and note the interval in time. |
|||
Series: 4 3 1 4 2 2 |
|||
Use hourglasses from indices 2 to 5 (inclusive) to sum 9 using 4 7. |
|||
Series: 5 2 3 4 1 5 1 4 3 2 1 4 5 2 3 4 1 |
|||
Use hourglasses from indices 4 to 16 (inclusive) to sum 36 using 5 7 31.</pre> |
|||
=={{header|Python}}== |
=={{header|Python}}== |