Skip to content

Instantly share code, notes, and snippets.

@codedot
Created March 3, 2012 20:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codedot/1968016 to your computer and use it in GitHub Desktop.
Save codedot/1968016 to your computer and use it in GitHub Desktop.
Pouring Tanks (the bc(1) version)
sh pour.sh 8 5 5
Filling right when left has 0
Immediate step #1
sh pour.sh 5 8 2
Filling left when right has 0
Pouring left to right gives 5
Filling left when right has 5
Pouring left to right gives 2
Solved at step #4
sh pour.sh 3 4 2
Filling left when right has 0
Pouring left to right gives 3
Filling left when right has 3
Pouring left to right gives 2
Solved at step #4
sh pour.sh 18 33 9
Filling right when left has 0
Pouring right to left gives 15
Emptying left when right is 15
Pouring right to left gives 15
Filling right when left has 15
Pouring right to left gives 30
Emptying left when right is 30
Pouring right to left gives 12
Emptying left when right is 12
Pouring right to left gives 12
Filling right when left has 12
Pouring right to left gives 27
Emptying left when right is 27
Pouring right to left gives 9
Solved at step #14
sh pour.sh 6 10 3
Filling right when left has 0
Pouring right to left gives 4
Emptying left when right is 4
Pouring right to left gives 4
Filling right when left has 4
Pouring right to left gives 8
Emptying left when right is 8
Pouring right to left gives 2
Emptying left when right is 2
Pouring right to left gives 2
Filling right when left has 2
Pouring right to left gives 6
Emptying left when right is 6
Pouring right to left gives 0
Filling right when left has 6
Emptying left when right is 10
Pouring right to left gives 4
Emptying left when right is 4
Failed at step #18
all: pour.sh
sh $< 8 5 5
sh $< 5 8 2
sh $< 3 4 2
sh $< 18 33 9
sh $< 6 10 3
clean:
-rm -f *.tmp
script='
define p(x) {
auto a, r
if (x) {
"Pouring left to right gives "
} else {
"Pouring right to left gives "
}
a = m[x] - t[x]
if (a > t[!x]) {
t[x] += t[!x]
t[!x] = 0
r = t[x]
} else {
t[!x] -= a
t[x] = m[x]
r = t[!x]
}
r
return (r)
}
define e(x) {
t[x] = 0
if (x) {
"Emptying right when left is "
} else {
"Emptying left when right is "
}
t[!x]
return (t[!x])
}
define f(x) {
t[x] = m[x]
if (x) {
"Filling right when left has "
} else {
"Filling left when right has "
}
t[!x]
return (t[x])
}
define s(l, r, q, d) {
auto i, c, u, v
m[0] = l
m[1] = r
while (1) {
if (!t[!d]) {
i += 1
v = f(!d)
if (q == v) {
"Immediate step #"
return (i)
}
}
if (t[d] == m[d]) {
i += 1
v = e(d)
if (c == v) {
"Failed at step #"
return (i)
}
if (!c) {
c = v
}
}
i += 1
v = p(d)
if (q == v) {
"Solved at step #"
return (i)
}
}
}
'
echo "$script s($1, $2, $3, 0)" | bc >left.tmp
echo "$script s($1, $2, $3, 1)" | bc >right.tmp
left=`wc -l <left.tmp`
right=`wc -l <right.tmp`
if [ "$left" -lt "$right" ]; then
cat left.tmp
else
cat right.tmp
fi
rm -f {left,right}.tmp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment