Hourglass puzzle: Difference between revisions

m
(Added 11l)
m (→‎{{header|Wren}}: Minor tidy)
 
(5 intermediate revisions by 5 users not shown)
Line 15:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">V t4 = 0
L t4 < 10'000
V t7_left = 7 - t4 % 7
Line 29:
t4 += 4
L.was_no_break
print(‘Not found’)</langsyntaxhighlight>
 
{{out}}
Line 40:
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>
 
=={{header|FreeBASIC}}==
{{trans|Phyton}}
<langsyntaxhighlight lang="freebasic">
Sub Hourglass_puzzle()
Dim As Uinteger t4 = 0, limite = 1000, t7_left
Line 68 ⟶ 121:
Hourglass_puzzle()
Sleep
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 84 ⟶ 137:
=={{header|Go}}==
{{trans|Julia}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 146 ⟶ 199:
fmt.Println(ts[i], "using", hgs[i])
}
}</langsyntaxhighlight>
 
{{out}}
Line 161 ⟶ 214:
=={{header|Julia}}==
Implemented as a game solver rather than as a game with user input.
<langsyntaxhighlight lang="julia">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[]
Line 187 ⟶ 240:
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]")
</langsyntaxhighlight>{{out}}
<pre>
Flip an hourglass every time it runs out of grains, and note the interval in time.
Line 198 ⟶ 251:
=={{header|Logo}}==
tested with FMSlogo
<langsyntaxhighlight lang="logo">
to bb
Make "small_capacity 4
Line 255 ⟶ 308:
 
 
</syntaxhighlight>
</lang>
 
=={{header|Nim}}==
{{trans|Wren}}
<langsyntaxhighlight Nimlang="nim">import math, strutils
 
func hourglassFlipper(hourglasses: openArray[int];
Line 288 ⟶ 341:
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(" "))</langsyntaxhighlight>
 
{{out}}
Line 302 ⟶ 355:
=={{header|Perl}}==
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 feature 'bitwise';
 
findinterval( $_, 4, 7 ) for 1 .. 20;
Line 312 ⟶ 364:
{
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";
}</langsyntaxhighlight>
{{out}}
<pre>
Line 343 ⟶ 395:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Hourglass_puzzle.exw</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
Line 437 ⟶ 489:
<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>
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 488 ⟶ 540:
There isn't much of a task description as I write this, but, here goes...
 
<langsyntaxhighlight lang="python">def hourglass_puzzle():
t4 = 0
while t4 < 10_000:
Line 507 ⟶ 559:
""")
hourglass_puzzle()</langsyntaxhighlight>
 
{{out}}
Line 518 ⟶ 570:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line># 20201230 Raku programming solution
 
my @hourglasses = 4, 7;
Line 549 ⟶ 601:
}
 
.say if .defined for @output</langsyntaxhighlight>
{{out}}
<pre>At time t = 4 , flip hourglass 4
Line 560 ⟶ 612:
=={{header|REXX}}==
{{trans|Python}}
<langsyntaxhighlight lang="rexx">/*REXX program determines if there is a solution to measure 9 minutes using a */
/*──────────────────────────────────── four and seven minute sandglasses. */
t4= 0
Line 584 ⟶ 636:
say "glass when the four-minute glass ends."
say
exit 0</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the internal default input:}}
<pre>
Line 597 ⟶ 649:
glass when the four-minute glass ends.
</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}}==
{{trans|Julia}}
{{libheader|Wren-math}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Nums
 
var hourglassFlipper = Fn.new { |hourglasses, target|
Line 635 ⟶ 731:
System.write("Use hourglasses from indices %(start) to %(end) (inclusive) to sum ")
System.print("%(target) using %(hourglasses)")
}</langsyntaxhighlight>
 
{{out}}
9,485

edits