Hourglass puzzle: Difference between revisions
Content deleted Content added
Alextretyak (talk | contribs) Added 11l |
m →{{header|Phix}}: use pygments |
||
(8 intermediate revisions by 7 users not shown) | |||
Line 15: | Line 15: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">V t4 = 0 |
||
L t4 < 10'000 |
L t4 < 10'000 |
||
V t7_left = 7 - t4 % 7 |
V t7_left = 7 - t4 % 7 |
||
Line 29: | Line 29: | ||
t4 += 4 |
t4 += 4 |
||
L.was_no_break |
L.was_no_break |
||
print(‘Not found’)</ |
print(‘Not found’)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 40: | Line 40: | ||
flipping the remaining sand in the 7 hour glass when the 4 hour glass ends. |
flipping the remaining sand in the 7 hour glass when the 4 hour glass ends. |
||
</pre> |
|||
=={{header|AWK}}== |
|||
<syntaxhighlight lang="awk"> |
|||
# syntax: GAWK -f HOURGLASS_PUZZLE.AWK |
|||
BEGIN { |
|||
limit = 100 |
|||
t4 = 0 |
|||
while (t4 < limit) { |
|||
t7_left = 7 - t4 % 7 |
|||
if (t7_left == 9 - 4) { |
|||
break |
|||
} |
|||
t4 += 4 |
|||
} |
|||
if (t4 > limit) { |
|||
printf("Unable to find an answer within %d iterations\n",limit) |
|||
exit(1) |
|||
} |
|||
str = sprintf("Turn over both hour glasses at the same time and continue flipping them each " \ |
|||
"when they individually run down until the 4 hour glass is flipped %d times, " \ |
|||
"wherupon the 7 hour glass is immediately placed on its side with %d minutes " \ |
|||
"of sand in it. " \ |
|||
"You can measure 9 minutes by flipping the 4 hour glass once, then " \ |
|||
"flipping the remaining sand in the 7 hour glass when the 4 hour glass ends.",t4/4,t7_left) |
|||
fold(str) |
|||
exit(0) |
|||
} |
|||
function fold(rec, chars_printed,indx,text) { |
|||
line_length = 80 |
|||
while (1) { |
|||
indx = match(rec," ") |
|||
if (indx == 0) { |
|||
printf("%s\n",rec) |
|||
break |
|||
} |
|||
text = substr(rec,1,indx) |
|||
printf("%s",text) |
|||
rec = substr(rec,RSTART+1) |
|||
chars_printed += length(text) |
|||
if (chars_printed > line_length) { |
|||
printf("\n") |
|||
chars_printed = 0 |
|||
} |
|||
} |
|||
} |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Turn over both hour glasses at the same time and continue flipping them each when |
|||
they individually run down until the 4 hour glass is flipped 4 times, wherupon the |
|||
7 hour glass is immediately placed on its side with 5 minutes of sand in it. You |
|||
can measure 9 minutes by flipping the 4 hour glass once, then flipping the remaining |
|||
sand in the 7 hour glass when the 4 hour glass ends. |
|||
</pre> |
</pre> |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
{{trans|Phyton}} |
{{trans|Phyton}} |
||
< |
<syntaxhighlight lang="freebasic"> |
||
Sub Hourglass_puzzle() |
Sub Hourglass_puzzle() |
||
Dim As Uinteger t4 = 0, limite = 1000, t7_left |
Dim As Uinteger t4 = 0, limite = 1000, t7_left |
||
Line 68: | Line 121: | ||
Hourglass_puzzle() |
Hourglass_puzzle() |
||
Sleep |
Sleep |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 80: | Line 133: | ||
volteando la arena restante en el reloj de 7 minutos cuando termine |
volteando la arena restante en el reloj de 7 minutos cuando termine |
||
el reloj de 4 minutos. |
el reloj de 4 minutos. |
||
</pre> |
|||
=={{header|FutureBasic}}== |
|||
<syntaxhighlight lang="futurebasic"> |
|||
void local fn HourglassPuzzle |
|||
int fourHourGlass = 0, sevenHourGlass |
|||
while ( fourHourGlass < 10000 ) |
|||
sevenHourGlass = 7 - fourHourGlass % 7 |
|||
if ( sevenHourGlass == 9 - 4 ) then break |
|||
fourHourGlass += 4 |
|||
wend |
|||
if ( fourHourGlass >= 10000 ) then printf @"Not found\n" : exit fn |
|||
CFStringRef rules = @"\n¬ |
|||
Simultaneously flip both hour glasses. When they individually\n¬ |
|||
run down, continue flipping them until the 4-hour glass\n¬ |
|||
is flipped %d times.\n\n¬ |
|||
After the fourth inversion cycle, place the 7-hour glass,\n¬ |
|||
which will still have %d hours of sand left in it, on its side.\n\n¬ |
|||
You can measure 9 hours by flipping the 4-hour glass\n¬ |
|||
once, then flipping the 7-hour glass when the 4-hour glass\n¬ |
|||
runs down.\n" |
|||
printf rules, fourHourGlass / 4, sevenHourGlass |
|||
end fn |
|||
fn HourglassPuzzle |
|||
HandleEvents |
|||
</syntaxhighlight> |
|||
{{output}} |
|||
<pre> |
|||
Simultaneously flip both hour glasses. When they individually |
|||
run down, continue flipping them until the 4-hour glass |
|||
is flipped 4 times. |
|||
After the fourth inversion cycle, place the 7-hour glass, |
|||
which will still have 5 hours of sand left in it, on its side. |
|||
You can measure 9 hours by flipping the 4-hour glass |
|||
once, then flipping the 7-hour glass when the 4-hour glass |
|||
runs down. |
|||
</pre> |
</pre> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
{{trans|Julia}} |
{{trans|Julia}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 146: | Line 245: | ||
fmt.Println(ts[i], "using", hgs[i]) |
fmt.Println(ts[i], "using", hgs[i]) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 161: | Line 260: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
Implemented as a game solver rather than as a game with user input. |
Implemented as a game solver rather than as a game with user input. |
||
< |
<syntaxhighlight lang="julia">function euclidean_hourglassflipper(hourglasses, target::Integer) |
||
gcd(hourglasses) in hourglasses && !(1 in hourglasses) && throw("Hourglasses fail sanity test (not relatively prime enough)") |
gcd(hourglasses) in hourglasses && !(1 in hourglasses) && throw("Hourglasses fail sanity test (not relatively prime enough)") |
||
flippers, series = deepcopy(hourglasses), Int[] |
flippers, series = deepcopy(hourglasses), Int[] |
||
Line 187: | Line 286: | ||
i, j = euclidean_hourglassflipper([5, 7, 31], 36) |
i, j = euclidean_hourglassflipper([5, 7, 31], 36) |
||
println("Use hourglasses from step $i to step $j (inclusive) to sum 36 using [5, 7, 31]") |
println("Use hourglasses from step $i to step $j (inclusive) to sum 36 using [5, 7, 31]") |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
Flip an hourglass every time it runs out of grains, and note the interval in time. |
Flip an hourglass every time it runs out of grains, and note the interval in time. |
||
Line 198: | Line 297: | ||
=={{header|Logo}}== |
=={{header|Logo}}== |
||
tested with FMSlogo |
tested with FMSlogo |
||
< |
<syntaxhighlight lang="logo"> |
||
to bb |
to bb |
||
Make "small_capacity 4 |
Make "small_capacity 4 |
||
Line 255: | Line 354: | ||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|Wren}} |
{{trans|Wren}} |
||
< |
<syntaxhighlight lang="nim">import math, strutils |
||
func hourglassFlipper(hourglasses: openArray[int]; |
func hourglassFlipper(hourglasses: openArray[int]; |
||
Line 288: | Line 387: | ||
echo "\nSeries: ", series.join(" ") |
echo "\nSeries: ", series.join(" ") |
||
echo "Use hourglasses from indices $1 to $2 (inclusive) to sum ".format(start, `end`), |
echo "Use hourglasses from indices $1 to $2 (inclusive) to sum ".format(start, `end`), |
||
"$1 using $2.".format(target, hourglasses.join(" "))</ |
"$1 using $2.".format(target, hourglasses.join(" "))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 302: | Line 401: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
Flip each hourglass when it runs out and note the time for each. |
Flip each hourglass when it runs out and note the time for each. |
||
<syntaxhighlight lang="perl">use strict; |
|||
<lang perl>#!/usr/bin/perl |
|||
use strict; # https://rosettacode.org/wiki/Hourglass_puzzle |
|||
use warnings; |
use warnings; |
||
use feature 'bitwise'; |
|||
findinterval( $_, 4, 7 ) for 1 .. 20; |
findinterval( $_, 4, 7 ) for 1 .. 20; |
||
Line 312: | Line 410: | ||
{ |
{ |
||
my ($want, $hour1, $hour2) = @_; |
my ($want, $hour1, $hour2) = @_; |
||
local $_ = (('1' | ' ' x $hour1) x $hour2 | ('2' | ' ' x $hour2) x $hour1) x $want; |
local $_ = (('1' |. ' ' x $hour1) x $hour2 | ('2' |. ' ' x $hour2) x $hour1) x $want; |
||
print /(?=\d).{$want}(?=\d)/ |
print /(?=\d).{$want}(?=\d)/ |
||
? "To get $want minute@{[$want == 1 ? '' : 's' |
? "To get $want minute@{[$want == 1 ? '' : 's' |
||
]}, Start at time $-[0] and End at time $+[0]\n" |
]}, Start at time $-[0] and End at time $+[0]\n" |
||
: "$want is not possible\n"; |
: "$want is not possible\n"; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 343: | Line 441: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!-- |
<!--(phixonline)--> |
||
<syntaxhighlight lang="phix"> |
|||
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Hourglass_puzzle.exw</span> |
|||
-- demo\rosetta\Hourglass_puzzle.exw |
|||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
|||
with javascript_semantics |
|||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">print_solution</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">eggtimers</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">tries</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">target</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">pdx</span><span style="color: #0000FF;">)</span> |
|||
procedure print_solution(sequence eggtimers, tries, integer target, pdx) |
|||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">soln</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">tries</span><span style="color: #0000FF;">[$]},</span> <span style="color: #000000;">remain</span> |
|||
sequence soln = {tries[$]}, remain |
|||
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">eggtimers</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">tdx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tries</span><span style="color: #0000FF;">[$][</span><span style="color: #000000;">3</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">flipbits</span> |
|||
integer n = length(eggtimers), tdx = tries[$][3], t, flipbits |
|||
<span style="color: #004080;">string</span> <span style="color: #000000;">et</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span> |
|||
string et = "" |
|||
<span style="color: #008080;">for</span> <span style="color: #000000;">timer</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span> |
|||
for timer=1 to n do |
|||
<span style="color: #008080;">if</span> <span style="color: #000000;">timer</span><span style="color: #0000FF;">=</span><span style="color: #000000;">n</span> <span style="color: #008080;">then</span> <span style="color: #000000;">et</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">" and "</span> |
|||
if timer=n then et &= " and " |
|||
<span style="color: #008080;">elsif</span> <span style="color: #000000;">timer</span><span style="color: #0000FF;">></span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> <span style="color: #000000;">et</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">", "</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
elsif timer>1 then et &= ", " end if |
|||
<span style="color: #000000;">et</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">eggtimers</span><span style="color: #0000FF;">[</span><span style="color: #000000;">timer</span><span style="color: #0000FF;">])</span> |
|||
et &= sprintf("%d",eggtimers[timer]) |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
|||
end for |
|||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\nSolution for %d minutes with %s minute eggtimers:\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">target</span><span style="color: #0000FF;">,</span><span style="color: #000000;">et</span><span style="color: #0000FF;">})</span> |
|||
printf(1,"\nSolution for %d minutes with %s minute eggtimers:\n",{target,et}) |
|||
<span style="color: #008080;">while</span> <span style="color: #000000;">tdx</span> <span style="color: #008080;">do</span> |
|||
while tdx do |
|||
<span style="color: #008080;">if</span> <span style="color: #000000;">tdx</span><span style="color: #0000FF;">=</span><span style="color: #000000;">pdx</span> <span style="color: #008080;">then</span> <span style="color: #000000;">soln</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
if tdx=pdx then soln &= 0 end if |
|||
<span style="color: #000000;">soln</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">soln</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tries</span><span style="color: #0000FF;">[</span><span style="color: #000000;">tdx</span><span style="color: #0000FF;">])</span> |
|||
soln = append(soln,tries[tdx]) |
|||
<span style="color: #000000;">tdx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tries</span><span style="color: #0000FF;">[</span><span style="color: #000000;">tdx</span><span style="color: #0000FF;">][</span><span style="color: #000000;">3</span><span style="color: #0000FF;">]</span> |
|||
tdx = tries[tdx][3] |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span> |
|||
end while |
|||
<span style="color: #000000;">soln</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">reverse</span><span style="color: #0000FF;">(</span><span style="color: #000000;">soln</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..$-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span> |
|||
soln = reverse(soln[1..$-1]) |
|||
<span style="color: #004080;">integer</span> <span style="color: #000000;">tp</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ro</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span> |
|||
integer tp = 0, ro = 0 |
|||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">premain</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> |
|||
sequence premain = repeat(0,n) |
|||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">soln</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span> |
|||
for i=1 to length(soln) do |
|||
<span style="color: #008080;">if</span> <span style="color: #000000;">soln</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> |
|||
if soln[i]=0 then |
|||
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"start timer\n"</span><span style="color: #0000FF;">)</span> |
|||
puts(1,"start timer\n") |
|||
else |
|||
<span style="color: #0000FF;">{</span><span style="color: #000000;">remain</span><span style="color: #0000FF;">,</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,?,</span><span style="color: #000000;">flipbits</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">soln</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> |
|||
{remain,t,?,flipbits} = soln[i] |
|||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">flip</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">int_to_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">flipbits</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> |
|||
sequence flip = int_to_bits(flipbits,n) |
|||
<span style="color: #004080;">string</span> <span style="color: #000000;">fs</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">lv</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span> |
|||
string fs = "", lv = "" |
|||
<span style="color: #008080;">for</span> <span style="color: #000000;">timer</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span> |
|||
for timer=1 to n do |
|||
<span style="color: #008080;">if</span> <span style="color: #000000;">flip</span><span style="color: #0000FF;">[</span><span style="color: #000000;">timer</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span> |
|||
if flip[timer] then |
|||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fs</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #000000;">fs</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">" and "</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
if length(fs) then fs &= " and " end if |
|||
<span style="color: #000000;">fs</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">eggtimers</span><span style="color: #0000FF;">[</span><span style="color: #000000;">timer</span><span style="color: #0000FF;">])</span> |
|||
fs &= sprintf("%d",eggtimers[timer]) |
|||
<span style="color: #008080;">if</span> <span style="color: #000000;">premain</span><span style="color: #0000FF;">[</span><span style="color: #000000;">timer</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span> |
|||
if premain[timer] then |
|||
<span style="color: #000000;">fs</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">" (leaving %d)"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">eggtimers</span><span style="color: #0000FF;">[</span><span style="color: #000000;">timer</span><span style="color: #0000FF;">]-</span><span style="color: #000000;">premain</span><span style="color: #0000FF;">[</span><span style="color: #000000;">timer</span><span style="color: #0000FF;">])</span> |
|||
fs &= sprintf(" (leaving %d)",eggtimers[timer]-premain[timer]) |
|||
end if |
|||
end if |
|||
<span style="color: #008080;">if</span> <span style="color: #000000;">remain</span><span style="color: #0000FF;">[</span><span style="color: #000000;">timer</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> |
|||
if remain[timer]=0 then |
|||
<span style="color: #008080;">if</span> <span style="color: #000000;">flip</span><span style="color: #0000FF;">[</span><span style="color: #000000;">timer</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">or</span> <span style="color: #000000;">premain</span><span style="color: #0000FF;">[</span><span style="color: #000000;">timer</span><span style="color: #0000FF;">]!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> |
|||
if flip[timer] or premain[timer]!=0 then |
|||
<span style="color: #000000;">ro</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">eggtimers</span><span style="color: #0000FF;">[</span><span style="color: #000000;">timer</span><span style="color: #0000FF;">]</span> |
|||
ro = eggtimers[timer] |
|||
end if |
|||
else |
|||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lv</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #000000;">lv</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">" and "</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
if length(lv) then lv &= " and " end if |
|||
<span style="color: #000000;">lv</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d in %d"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">remain</span><span style="color: #0000FF;">[</span><span style="color: #000000;">timer</span><span style="color: #0000FF;">],</span><span style="color: #000000;">eggtimers</span><span style="color: #0000FF;">[</span><span style="color: #000000;">timer</span><span style="color: #0000FF;">]})</span> |
|||
lv &= sprintf("%d in %d",{remain[timer],eggtimers[timer]}) |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
end if |
|||
end for |
|||
<span style="color: #000000;">lv</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lv</span><span style="color: #0000FF;">)?</span><span style="color: #008000;">" (leaving "</span><span style="color: #0000FF;">&</span><span style="color: #000000;">lv</span><span style="color: #0000FF;">&</span><span style="color: #008000;">")"</span><span style="color: #0000FF;">:</span><span style="color: #008000;">""</span><span style="color: #0000FF;">)</span> |
|||
lv = iff(length(lv)?" (leaving "&lv&")":"") |
|||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"At t=%d, flip %s, then when %d runs out%s...\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">tp</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fs</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ro</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lv</span><span style="color: #0000FF;">})</span> |
|||
printf(1,"At t=%d, flip %s, then when %d runs out%s...\n",{tp,fs,ro,lv}) |
|||
<span style="color: #000000;">tp</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">t</span> |
|||
tp = t |
|||
<span style="color: #000000;">premain</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">remain</span> |
|||
premain = remain |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
end if |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
|||
end for |
|||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"At t=%d, stop timer\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">tp</span><span style="color: #0000FF;">})</span> |
|||
printf(1,"At t=%d, stop timer\n",{tp}) |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span> |
|||
end procedure |
|||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">solve</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">eggtimers</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">target</span><span style="color: #0000FF;">)</span> |
|||
procedure solve(sequence eggtimers, integer target) |
|||
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">eggtimers</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">tdx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">pdx</span> |
|||
integer n = length(eggtimers), tdx = 1, t, pdx |
|||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">remain</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">),</span> |
|||
sequence remain = repeat(0,n), |
|||
<span style="color: #000000;">tries</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">remain</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">}}</span> <span style="color: #000080;font-style:italic;">-- {{remain,t,link,flip}}</span> |
|||
tries = {{remain,0,0,0}} -- {Remain,t,link,flip} |
|||
<span style="color: #008080;">while</span> <span style="color: #000000;">tdx</span><span style="color: #0000FF;"><=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tries</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span> |
|||
while tdx<=length(tries) do |
|||
<span style="color: #008080;">for</span> <span style="color: #000000;">flipbits</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span> |
|||
for flipbits=1 to power(2,n)-1 do |
|||
<span style="color: #0000FF;">{</span><span style="color: #000000;">remain</span><span style="color: #0000FF;">,</span><span style="color: #000000;">t</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tries</span><span style="color: #0000FF;">[</span><span style="color: #000000;">tdx</span><span style="color: #0000FF;">])</span> |
|||
{remain,t} = deep_copy(tries[tdx]) |
|||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">flip</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">int_to_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">flipbits</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> |
|||
sequence flip = int_to_bits(flipbits,n) |
|||
<span style="color: #008080;">for</span> <span style="color: #000000;">timer</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span> |
|||
for timer=1 to n do |
|||
<span style="color: #008080;">if</span> <span style="color: #000000;">flip</span><span style="color: #0000FF;">[</span><span style="color: #000000;">timer</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span> |
|||
if flip[timer] then |
|||
<span style="color: #000000;">remain</span><span style="color: #0000FF;">[</span><span style="color: #000000;">timer</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">eggtimers</span><span style="color: #0000FF;">[</span><span style="color: #000000;">timer</span><span style="color: #0000FF;">]-</span><span style="color: #000000;">remain</span><span style="color: #0000FF;">[</span><span style="color: #000000;">timer</span><span style="color: #0000FF;">]</span> |
|||
remain[timer] = eggtimers[timer]-remain[timer] |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
end if |
|||
end for |
|||
<span style="color: #004080;">integer</span> <span style="color: #000000;">mr</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #000000;">remain</span><span style="color: #0000FF;">,</span><span style="color: #008000;">">"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">))</span> |
|||
integer mr = min(filter(remain,">",0)) |
|||
<span style="color: #000000;">remain</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sq_max</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_sub</span><span style="color: #0000FF;">(</span><span style="color: #000000;">remain</span><span style="color: #0000FF;">,</span><span style="color: #000000;">mr</span><span style="color: #0000FF;">),</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span> |
|||
remain = sq_max(sq_sub(remain,mr),0) |
|||
<span style="color: #000000;">mr</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">t</span> |
|||
mr += t |
|||
<span style="color: #000000;">tries</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tries</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">remain</span><span style="color: #0000FF;">,</span><span style="color: #000000;">mr</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tdx</span><span style="color: #0000FF;">,</span><span style="color: #000000;">flipbits</span><span style="color: #0000FF;">})</span> |
|||
tries = append(tries,{remain,mr,tdx,flipbits}) |
|||
<span style="color: #000000;">pdx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tdx</span> |
|||
pdx = tdx |
|||
<span style="color: #008080;">while</span> <span style="color: #000000;">pdx</span> <span style="color: #008080;">do</span> |
|||
while pdx do |
|||
<span style="color: #000000;">mr</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">tries</span><span style="color: #0000FF;">[</span><span style="color: #000000;">pdx</span><span style="color: #0000FF;">][</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> |
|||
mr -= tries[pdx][2] |
|||
<span style="color: #008080;">if</span> <span style="color: #000000;">mr</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">target</span> <span style="color: #008080;">then</span> |
|||
if mr>=target then |
|||
<span style="color: #008080;">if</span> <span style="color: #000000;">mr</span><span style="color: #0000FF;">></span><span style="color: #000000;">target</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
if mr>target then exit end if |
|||
<span style="color: #000000;">print_solution</span><span style="color: #0000FF;">(</span><span style="color: #000000;">eggtimers</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">tries</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">target</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">pdx</span><span style="color: #0000FF;">)</span> |
|||
print_solution(eggtimers, tries, target, pdx) |
|||
return |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
end if |
|||
<span style="color: #000000;">mr</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">tries</span><span style="color: #0000FF;">[</span><span style="color: #000000;">pdx</span><span style="color: #0000FF;">][</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> |
|||
mr += tries[pdx][2] |
|||
<span style="color: #000000;">pdx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tries</span><span style="color: #0000FF;">[</span><span style="color: #000000;">pdx</span><span style="color: #0000FF;">][</span><span style="color: #000000;">3</span><span style="color: #0000FF;">]</span> |
|||
pdx = tries[pdx][3] |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span> |
|||
end while |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
|||
end for |
|||
<span style="color: #000000;">tdx</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span> |
|||
tdx += 1 |
|||
<span style="color: #000080;font-style:italic;">-- totally arbitrary sanity crash:</span> |
|||
-- totally arbitrary sanity crash: |
|||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tries</span><span style="color: #0000FF;">)></span><span style="color: #000000;">20000</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">crash</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"no solution"</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
if length(tries)>20000 then crash("no solution") end if |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span> |
|||
end while |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span> |
|||
end procedure |
|||
<span style="color: #000000;">solve</span><span style="color: #0000FF;">({</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">},</span><span style="color: #000000;">9</span><span style="color: #0000FF;">)</span> |
|||
solve({4,7},9) |
|||
<span style="color: #000000;">solve</span><span style="color: #0000FF;">({</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">},</span><span style="color: #000000;">15</span><span style="color: #0000FF;">)</span> |
|||
solve({4,7},15) |
|||
<span style="color: #000000;">solve</span><span style="color: #0000FF;">({</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">31</span><span style="color: #0000FF;">},</span><span style="color: #000000;">36</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (slightly better output than Julia, I think...)</span> |
|||
solve({5,7,31},36) -- (slightly better output than Julia, I think...) |
|||
<span style="color: #000000;">solve</span><span style="color: #0000FF;">({</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">},</span><span style="color: #000000;">7</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (logo solution stops timer at t=12, this manages t=11)</span> |
|||
solve({4,5},7) -- (logo solution stops timer at t=12, this manages t=11) |
|||
<span style="color: #000000;">solve</span><span style="color: #0000FF;">({</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">11</span><span style="color: #0000FF;">},</span><span style="color: #000000;">15</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (logo solution stops timer at t=22, this manages t=15)</span> |
|||
solve({7,11},15) -- (logo solution stops timer at t=22, this manages t=15) |
|||
<span style="color: #000000;">solve</span><span style="color: #0000FF;">({</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">},</span><span style="color: #000000;">14</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (logo solution stops timer at t=24, this manages t=19)</span> |
|||
solve({5,8},14) -- (logo solution stops timer at t=24, this manages t=19) |
|||
<!--</lang>--> |
|||
</syntaxhighlight> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 488: | Line 587: | ||
There isn't much of a task description as I write this, but, here goes... |
There isn't much of a task description as I write this, but, here goes... |
||
< |
<syntaxhighlight lang="python">def hourglass_puzzle(): |
||
t4 = 0 |
t4 = 0 |
||
while t4 < 10_000: |
while t4 < 10_000: |
||
Line 507: | Line 606: | ||
""") |
""") |
||
hourglass_puzzle()</ |
hourglass_puzzle()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 518: | Line 617: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
<lang |
<syntaxhighlight lang="raku" line># 20201230 Raku programming solution |
||
my @hourglasses = 4, 7; |
my @hourglasses = 4, 7; |
||
Line 549: | Line 648: | ||
} |
} |
||
.say if .defined for @output</ |
.say if .defined for @output</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>At time t = 4 , flip hourglass 4 |
<pre>At time t = 4 , flip hourglass 4 |
||
Line 560: | Line 659: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="rexx">/*REXX program determines if there is a solution to measure 9 minutes using a */ |
||
/*──────────────────────────────────── four and seven minute sandglasses. */ |
/*──────────────────────────────────── four and seven minute sandglasses. */ |
||
t4= 0 |
t4= 0 |
||
Line 584: | Line 683: | ||
say "glass when the four-minute glass ends." |
say "glass when the four-minute glass ends." |
||
say |
say |
||
exit 0</ |
exit 0</syntaxhighlight> |
||
{{out|output|text= when using the internal default input:}} |
{{out|output|text= when using the internal default input:}} |
||
<pre> |
<pre> |
||
Line 597: | Line 696: | ||
glass when the four-minute glass ends. |
glass when the four-minute glass ends. |
||
</pre> |
</pre> |
||
=={{header|V (Vlang)}}== |
|||
{{trans|go}} |
|||
<syntaxhighlight lang="v (vlang)">import arrays {sum, min} |
|||
fn hourglass_flipper(hourglasses []int, target int) (int, []int) { |
|||
mut flippers := hourglasses.clone() |
|||
mut series := []int{} |
|||
for _ in 0..10000 { |
|||
n := min<int>(flippers) or {flippers[0]} |
|||
series << n |
|||
for i in 0..flippers.len { |
|||
flippers[i] -= n |
|||
} |
|||
for i, flipper in flippers { |
|||
if flipper == 0 { |
|||
flippers[i] = hourglasses[i] |
|||
} |
|||
} |
|||
for start := series.len - 1; start >= 0; start-- { |
|||
if sum<int>(series[start..]) or {-1} == target { |
|||
return start, series |
|||
} |
|||
} |
|||
} |
|||
return 0, []int{} |
|||
} |
|||
fn main() { |
|||
print("Flip an hourglass every time it runs out of grains, ") |
|||
println("and note the interval in time.") |
|||
hgs := [[4, 7], [5, 7, 31]] |
|||
ts := [9, 36] |
|||
for i in 0..hgs.len { |
|||
start, series := hourglass_flipper(hgs[i], ts[i]) |
|||
end := series.len - 1 |
|||
println("\nSeries: $series") |
|||
print("Use hourglasses from indices $start to $end (inclusive) to sum ") |
|||
println("${ts[i]} using ${hgs[i]}") |
|||
} |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Same as Go entry</pre> |
|||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{trans|Julia}} |
{{trans|Julia}} |
||
{{libheader|Wren-math}} |
{{libheader|Wren-math}} |
||
< |
<syntaxhighlight lang="wren">import "./math" for Nums |
||
var hourglassFlipper = Fn.new { |hourglasses, target| |
var hourglassFlipper = Fn.new { |hourglasses, target| |
||
Line 635: | Line 778: | ||
System.write("Use hourglasses from indices %(start) to %(end) (inclusive) to sum ") |
System.write("Use hourglasses from indices %(start) to %(end) (inclusive) to sum ") |
||
System.print("%(target) using %(hourglasses)") |
System.print("%(target) using %(hourglasses)") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
Latest revision as of 11:15, 13 July 2024
Hourglass puzzle is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
- Task
Given two hourglasses of 4 minutes and 7 minutes, the task is to measure 9 minutes.
- Notes
Implemented as a 1-player game.
11l
V t4 = 0
L t4 < 10'000
V t7_left = 7 - t4 % 7
I t7_left == 9 - 4
print(|‘Turn over both hour glasses at the same time and continue flipping them each
when they individually run down until the 4 hour glass is flipped #. times,
wherupon the 7 hour glass is immediately placed on its side with #. hours
of sand in it.
You can measure 9 hours by flipping the 4 hour glass once, then
flipping the remaining sand in the 7 hour glass when the 4 hour glass ends.
’.format(t4 I/ 4, t7_left))
L.break
t4 += 4
L.was_no_break
print(‘Not found’)
- Output:
Turn over both hour glasses at the same time and continue flipping them each when they individually run down until the 4 hour glass is flipped 4 times, wherupon the 7 hour glass is immediately placed on its side with 5 hours of sand in it. You can measure 9 hours by flipping the 4 hour glass once, then flipping the remaining sand in the 7 hour glass when the 4 hour glass ends.
AWK
# syntax: GAWK -f HOURGLASS_PUZZLE.AWK
BEGIN {
limit = 100
t4 = 0
while (t4 < limit) {
t7_left = 7 - t4 % 7
if (t7_left == 9 - 4) {
break
}
t4 += 4
}
if (t4 > limit) {
printf("Unable to find an answer within %d iterations\n",limit)
exit(1)
}
str = sprintf("Turn over both hour glasses at the same time and continue flipping them each " \
"when they individually run down until the 4 hour glass is flipped %d times, " \
"wherupon the 7 hour glass is immediately placed on its side with %d minutes " \
"of sand in it. " \
"You can measure 9 minutes by flipping the 4 hour glass once, then " \
"flipping the remaining sand in the 7 hour glass when the 4 hour glass ends.",t4/4,t7_left)
fold(str)
exit(0)
}
function fold(rec, chars_printed,indx,text) {
line_length = 80
while (1) {
indx = match(rec," ")
if (indx == 0) {
printf("%s\n",rec)
break
}
text = substr(rec,1,indx)
printf("%s",text)
rec = substr(rec,RSTART+1)
chars_printed += length(text)
if (chars_printed > line_length) {
printf("\n")
chars_printed = 0
}
}
}
- Output:
Turn over both hour glasses at the same time and continue flipping them each when they individually run down until the 4 hour glass is flipped 4 times, wherupon the 7 hour glass is immediately placed on its side with 5 minutes of sand in it. You can measure 9 minutes by flipping the 4 hour glass once, then flipping the remaining sand in the 7 hour glass when the 4 hour glass ends.
FreeBASIC
Sub Hourglass_puzzle()
Dim As Uinteger t4 = 0, limite = 1000, t7_left
While t4 < limite
t7_left = 7 - t4 Mod 7
If t7_left = 9 - 4 Then Exit While
t4 += 4
Wend
If t4 > limite Then
Print "No encontrado"
Return
End If
Print Using !"\nVoltee al mismo tiempo ambos relojes de arena y contin£e volte ndolos \n" + _
!"cuando los relojes de arena se agoten individualmente, hasta que el \n" + _
!"reloj de arena de 4 minutos se voltee & veces, despu‚s de lo cual el \n" + _
!"reloj de 7 minutos se coloca inmediatamente de lado con & minutos de \n" + _
!"arena en ‚l. \n" + _
!"\nPuede medir 9 minutos volteando el reloj de 4 minutos una vez, luego \n" + _
!"volteando la arena restante en el reloj de 7 minutos cuando termine \n" + _
!"el reloj de 4 minutos."; t4/4; t7_left
End Sub
Hourglass_puzzle()
Sleep
- Output:
Voltee al mismo tiempo ambos relojes de arena y continúe volteándolos cuando los relojes de arena se agoten individualmente, hasta que el reloj de arena de 4 minutos se voltee 4 veces, después de lo cual el reloj de 7 minutos se coloca inmediatamente de lado con 5 minutos de arena en él. Puede medir 9 minutos volteando el reloj de 4 minutos una vez, luego volteando la arena restante en el reloj de 7 minutos cuando termine el reloj de 4 minutos.
FutureBasic
void local fn HourglassPuzzle
int fourHourGlass = 0, sevenHourGlass
while ( fourHourGlass < 10000 )
sevenHourGlass = 7 - fourHourGlass % 7
if ( sevenHourGlass == 9 - 4 ) then break
fourHourGlass += 4
wend
if ( fourHourGlass >= 10000 ) then printf @"Not found\n" : exit fn
CFStringRef rules = @"\n¬
Simultaneously flip both hour glasses. When they individually\n¬
run down, continue flipping them until the 4-hour glass\n¬
is flipped %d times.\n\n¬
After the fourth inversion cycle, place the 7-hour glass,\n¬
which will still have %d hours of sand left in it, on its side.\n\n¬
You can measure 9 hours by flipping the 4-hour glass\n¬
once, then flipping the 7-hour glass when the 4-hour glass\n¬
runs down.\n"
printf rules, fourHourGlass / 4, sevenHourGlass
end fn
fn HourglassPuzzle
HandleEvents
- Output:
Simultaneously flip both hour glasses. When they individually run down, continue flipping them until the 4-hour glass is flipped 4 times. After the fourth inversion cycle, place the 7-hour glass, which will still have 5 hours of sand left in it, on its side. You can measure 9 hours by flipping the 4-hour glass once, then flipping the 7-hour glass when the 4-hour glass runs down.
Go
package main
import (
"fmt"
"log"
)
func minimum(a []int) int {
min := a[0]
for i := 1; i < len(a); i++ {
if a[i] < min {
min = a[i]
}
}
return min
}
func sum(a []int) int {
s := 0
for _, i := range a {
s = s + i
}
return s
}
func hourglassFlipper(hourglasses []int, target int) (int, []int) {
flippers := make([]int, len(hourglasses))
copy(flippers, hourglasses)
var series []int
for iter := 0; iter < 10000; iter++ {
n := minimum(flippers)
series = append(series, n)
for i := 0; i < len(flippers); i++ {
flippers[i] -= n
}
for i, flipper := range flippers {
if flipper == 0 {
flippers[i] = hourglasses[i]
}
}
for start := len(series) - 1; start >= 0; start-- {
if sum(series[start:]) == target {
return start, series
}
}
}
log.Fatal("Unable to find an answer within 10,000 iterations.")
return 0, nil
}
func main() {
fmt.Print("Flip an hourglass every time it runs out of grains, ")
fmt.Println("and note the interval in time.")
hgs := [][]int{{4, 7}, {5, 7, 31}}
ts := []int{9, 36}
for i := 0; i < len(hgs); i++ {
start, series := hourglassFlipper(hgs[i], ts[i])
end := len(series) - 1
fmt.Println("\nSeries:", series)
fmt.Printf("Use hourglasses from indices %d to %d (inclusive) to sum ", start, end)
fmt.Println(ts[i], "using", hgs[i])
}
}
- Output:
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]
Julia
Implemented as a game solver rather than as a game with user input.
function euclidean_hourglassflipper(hourglasses, target::Integer)
gcd(hourglasses) in hourglasses && !(1 in hourglasses) && throw("Hourglasses fail sanity test (not relatively prime enough)")
flippers, series = deepcopy(hourglasses), Int[]
for i in 1:typemax(target)
n = minimum(flippers)
push!(series, n)
flippers .-= n
for (i, n) in enumerate(flippers)
if n == 0
flippers[i] = hourglasses[i]
end
end
for startpoint in length(series):-1:1
if sum(series[startpoint:end]) == target
println("Series: $series")
return startpoint, length(series)
end
end
end
end
println("Flip an hourglass every time it runs out of grains, and note the interval in time.")
i, j = euclidean_hourglassflipper([4, 7], 9)
println("Use hourglasses from step $i to step $j (inclusive) to sum 9 using [4, 7]")
i, j = euclidean_hourglassflipper([5, 7, 31], 36)
println("Use hourglasses from step $i to step $j (inclusive) to sum 36 using [5, 7, 31]")
- Output:
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 step 3 to step 6 (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 step 5 to step 17 (inclusive) to sum 36 using [5, 7, 31]
Logo
tested with FMSlogo
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
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(" "))
- Output:
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.
Perl
Flip each hourglass when it runs out and note the time for each.
use strict;
use warnings;
use feature 'bitwise';
findinterval( $_, 4, 7 ) for 1 .. 20;
sub findinterval
{
my ($want, $hour1, $hour2) = @_;
local $_ = (('1' |. ' ' x $hour1) x $hour2 | ('2' |. ' ' x $hour2) x $hour1) x $want;
print /(?=\d).{$want}(?=\d)/
? "To get $want minute@{[$want == 1 ? '' : 's'
]}, Start at time $-[0] and End at time $+[0]\n"
: "$want is not possible\n";
}
- Output:
To get 1 minute, Start at time 7 and End at time 8 To get 2 minutes, Start at time 12 and End at time 14 To get 3 minutes, Start at time 4 and End at time 7 To get 4 minutes, Start at time 0 and End at time 4 To get 5 minutes, Start at time 7 and End at time 12 To get 6 minutes, Start at time 8 and End at time 14 To get 7 minutes, Start at time 0 and End at time 7 To get 8 minutes, Start at time 0 and End at time 8 To get 9 minutes, Start at time 7 and End at time 16 To get 10 minutes, Start at time 4 and End at time 14 To get 11 minutes, Start at time 21 and End at time 32 To get 12 minutes, Start at time 0 and End at time 12 To get 13 minutes, Start at time 7 and End at time 20 To get 14 minutes, Start at time 0 and End at time 14 To get 15 minutes, Start at time 20 and End at time 35 To get 16 minutes, Start at time 0 and End at time 16 To get 17 minutes, Start at time 4 and End at time 21 To get 18 minutes, Start at time 14 and End at time 32 To get 19 minutes, Start at time 16 and End at time 35 To get 20 minutes, Start at time 0 and End at time 20
Phix
-- demo\rosetta\Hourglass_puzzle.exw
with javascript_semantics
procedure print_solution(sequence eggtimers, tries, integer target, pdx)
sequence soln = {tries[$]}, remain
integer n = length(eggtimers), tdx = tries[$][3], t, flipbits
string et = ""
for timer=1 to n do
if timer=n then et &= " and "
elsif timer>1 then et &= ", " end if
et &= sprintf("%d",eggtimers[timer])
end for
printf(1,"\nSolution for %d minutes with %s minute eggtimers:\n",{target,et})
while tdx do
if tdx=pdx then soln &= 0 end if
soln = append(soln,tries[tdx])
tdx = tries[tdx][3]
end while
soln = reverse(soln[1..$-1])
integer tp = 0, ro = 0
sequence premain = repeat(0,n)
for i=1 to length(soln) do
if soln[i]=0 then
puts(1,"start timer\n")
else
{remain,t,?,flipbits} = soln[i]
sequence flip = int_to_bits(flipbits,n)
string fs = "", lv = ""
for timer=1 to n do
if flip[timer] then
if length(fs) then fs &= " and " end if
fs &= sprintf("%d",eggtimers[timer])
if premain[timer] then
fs &= sprintf(" (leaving %d)",eggtimers[timer]-premain[timer])
end if
end if
if remain[timer]=0 then
if flip[timer] or premain[timer]!=0 then
ro = eggtimers[timer]
end if
else
if length(lv) then lv &= " and " end if
lv &= sprintf("%d in %d",{remain[timer],eggtimers[timer]})
end if
end for
lv = iff(length(lv)?" (leaving "&lv&")":"")
printf(1,"At t=%d, flip %s, then when %d runs out%s...\n",{tp,fs,ro,lv})
tp = t
premain = remain
end if
end for
printf(1,"At t=%d, stop timer\n",{tp})
end procedure
procedure solve(sequence eggtimers, integer target)
integer n = length(eggtimers), tdx = 1, t, pdx
sequence remain = repeat(0,n),
tries = {{remain,0,0,0}} -- {Remain,t,link,flip}
while tdx<=length(tries) do
for flipbits=1 to power(2,n)-1 do
{remain,t} = deep_copy(tries[tdx])
sequence flip = int_to_bits(flipbits,n)
for timer=1 to n do
if flip[timer] then
remain[timer] = eggtimers[timer]-remain[timer]
end if
end for
integer mr = min(filter(remain,">",0))
remain = sq_max(sq_sub(remain,mr),0)
mr += t
tries = append(tries,{remain,mr,tdx,flipbits})
pdx = tdx
while pdx do
mr -= tries[pdx][2]
if mr>=target then
if mr>target then exit end if
print_solution(eggtimers, tries, target, pdx)
return
end if
mr += tries[pdx][2]
pdx = tries[pdx][3]
end while
end for
tdx += 1
-- totally arbitrary sanity crash:
if length(tries)>20000 then crash("no solution") end if
end while
end procedure
solve({4,7},9)
solve({4,7},15)
solve({5,7,31},36) -- (slightly better output than Julia, I think...)
solve({4,5},7) -- (logo solution stops timer at t=12, this manages t=11)
solve({7,11},15) -- (logo solution stops timer at t=22, this manages t=15)
solve({5,8},14) -- (logo solution stops timer at t=24, this manages t=19)
- Output:
Solution for 9 minutes with 4 and 7 minute eggtimers: start timer At t=0, flip 4 and 7, then when 4 runs out (leaving 3 in 7)... At t=4, flip 4, then when 7 runs out (leaving 1 in 4)... At t=7, flip 7, then when 4 runs out (leaving 6 in 7)... At t=8, flip 7 (leaving 1), then when 7 runs out... At t=9, stop timer Solution for 15 minutes with 4 and 7 minute eggtimers: start timer At t=0, flip 4, then when 4 runs out... At t=4, flip 4, then when 4 runs out... At t=8, flip 7, then when 7 runs out... At t=15, stop timer Solution for 36 minutes with 5, 7 and 31 minute eggtimers: start timer At t=0, flip 5, then when 5 runs out... At t=5, flip 31, then when 31 runs out... At t=36, stop timer Solution for 7 minutes with 4 and 5 minute eggtimers: At t=0, flip 4 and 5, then when 4 runs out (leaving 1 in 5)... start timer At t=4, flip 4, then when 5 runs out (leaving 3 in 4)... At t=5, flip 4 (leaving 1), then when 4 runs out... At t=6, flip 5, then when 5 runs out... At t=11, stop timer Solution for 15 minutes with 7 and 11 minute eggtimers: start timer At t=0, flip 7 and 11, then when 7 runs out (leaving 4 in 11)... At t=7, flip 7, then when 11 runs out (leaving 3 in 7)... At t=11, flip 7 (leaving 4), then when 7 runs out... At t=15, stop timer Solution for 14 minutes with 5 and 8 minute eggtimers: At t=0, flip 5 and 8, then when 5 runs out (leaving 3 in 8)... start timer At t=5, flip 5, then when 8 runs out (leaving 2 in 5)... At t=8, flip 5 (leaving 3), then when 5 runs out... At t=11, flip 8, then when 8 runs out... At t=19, stop timer
Python
There isn't much of a task description as I write this, but, here goes...
def hourglass_puzzle():
t4 = 0
while t4 < 10_000:
t7_left = 7 - t4 % 7
if t7_left == 9 - 4:
break
t4 += 4
else:
print('Not found')
return
print(f"""
Turn over both hour glasses at the same time and continue flipping them each
when they individually run down until the 4 hour glass is flipped {t4//4} times,
wherupon the 7 hour glass is immediately placed on its side with {t7_left} hours
of sand in it.
You can measure 9 hours by flipping the 4 hour glass once, then
flipping the remaining sand in the 7 hour glass when the 4 hour glass ends.
""")
hourglass_puzzle()
- Output:
Turn over both hour glasses at the same time and continue flipping them each when they individually run down until the 4 hour glass is flipped 4 times, wherupon the 7 hour glass is immediately placed on its side with 5 hours of sand in it. You can measure 9 hours by flipping the 4 hour glass once, then flipping the remaining sand in the 7 hour glass when the 4 hour glass ends.
Raku
# 20201230 Raku programming solution
my @hourglasses = 4, 7;
my $target = 9;
my @output = [];
my %elapsed = 0 => 1 ;
my $done = False ;
for 1 .. ∞ -> $t {
my $flip-happened = False;
for @hourglasses -> $hg {
unless $t % $hg {
%elapsed{$t} = 1 unless %elapsed{$t};
with @output[$t] { $_ ~= "\t, flip hourglass $hg " } else {
$_ = "At time t = $t , flip hourglass $hg" }
$flip-happened = True
}
}
if $flip-happened {
for %elapsed.keys.sort -> $t1 {
if ($t - $t1) == $target {
@output[$t1] ~= "\tbegin = 0";
@output[$t] ~= "\tend = $target";
$done = True
}
%elapsed{$t} = 1 unless %elapsed{$t} ;
}
}
last if $done
}
.say if .defined for @output
- Output:
At time t = 4 , flip hourglass 4 At time t = 7 , flip hourglass 7 begin = 0 At time t = 8 , flip hourglass 4 At time t = 12 , flip hourglass 4 At time t = 14 , flip hourglass 7 At time t = 16 , flip hourglass 4 end = 9
REXX
/*REXX program determines if there is a solution to measure 9 minutes using a */
/*──────────────────────────────────── four and seven minute sandglasses. */
t4= 0
mx= 10000
do t4=0 by 4 to mx
t7_left= 7 - t4 % 7
if t7_left==9-4 then leave
end /*t4*/
say
if t4>mx then do
say 'Not found.'
exit 4
end
say "Turn over both sandglasses (at the same time) and continue"
say "flipping them each when the sandglasses individually run down"
say "until the four-minute glass is flipped " t4%4 ' times,'
say "whereupon the seven-minute glass is immediately placed on its"
say "side with " t7_left ' minutes of sand in it.'
say
say "You can measure 9 minutes by flipping the four-minute glass"
say "once, then flipping the remaining sand in the seven-minute"
say "glass when the four-minute glass ends."
say
exit 0
- output when using the internal default input:
Turn over both sandglasses (at the same time) and continue flipping them each when the sandglasses individually run down until the four-minute glass is flipped 4 times, whereupon the seven-minute glass is immediately placed on its side with 5 minutes of sand in it. You can measure 9 minutes by flipping the four-minute glass once, then flipping the remaining sand in the seven-minute glass when the four-minute glass ends.
V (Vlang)
import arrays {sum, min}
fn hourglass_flipper(hourglasses []int, target int) (int, []int) {
mut flippers := hourglasses.clone()
mut series := []int{}
for _ in 0..10000 {
n := min<int>(flippers) or {flippers[0]}
series << n
for i in 0..flippers.len {
flippers[i] -= n
}
for i, flipper in flippers {
if flipper == 0 {
flippers[i] = hourglasses[i]
}
}
for start := series.len - 1; start >= 0; start-- {
if sum<int>(series[start..]) or {-1} == target {
return start, series
}
}
}
return 0, []int{}
}
fn main() {
print("Flip an hourglass every time it runs out of grains, ")
println("and note the interval in time.")
hgs := [[4, 7], [5, 7, 31]]
ts := [9, 36]
for i in 0..hgs.len {
start, series := hourglass_flipper(hgs[i], ts[i])
end := series.len - 1
println("\nSeries: $series")
print("Use hourglasses from indices $start to $end (inclusive) to sum ")
println("${ts[i]} using ${hgs[i]}")
}
}
- Output:
Same as Go entry
Wren
import "./math" for Nums
var hourglassFlipper = Fn.new { |hourglasses, target|
var flippers = hourglasses.toList
var series = []
for (iter in 0...10000) {
var n = Nums.min(flippers)
series.add(n)
for (i in 0...flippers.count) flippers[i] = flippers[i] - n
var i = 0
for (flipper in flippers) {
if (flipper == 0) flippers[i] = hourglasses[i]
i = i + 1
}
for (start in series.count-1..0) {
if (Nums.sum(series[start..-1]) == target) return [start, series]
}
}
Fiber.abort("Unable to find an answer within 10,000 iterations.")
}
System.write("Flip an hourglass every time it runs out of grains, ")
System.print("and note the interval in time.")
var tests = [ [[4, 7], 9], [[5, 7, 31], 36] ]
for (test in tests) {
var hourglasses = test[0]
var target = test[1]
var res = hourglassFlipper.call(hourglasses, target)
var start = res[0]
var series = res[1]
var end = series.count - 1
System.print("\nSeries: %(series)")
System.write("Use hourglasses from indices %(start) to %(end) (inclusive) to sum ")
System.print("%(target) using %(hourglasses)")
}
- Output:
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]