Skip to content
Snippets Groups Projects
Verified Commit 16664664 authored by mirabilos's avatar mirabilos Committed by mirabilos
Browse files

slightly better abort condition in s2 than s1c though

parent e074b301
No related branches found
No related tags found
No related merge requests found
#!/bin/mksh
die() {
print -ru2 -- "E: $*"
exit 1
}
bc |&
function tobc {
print -pr -- "$@" || die coprocess died trying to write
}
function frombc {
read -p "$@" || die coprocess died trying to read
}
function quitbc {
trap - HUP INT QUIT TRAP PIPE TERM
exec 3>&p
print -ru3 -- quit
exec 3>&-
:
}
function trapbc {
local i=$1
exec 3>&p
exec 3>&-
wait
print -u2
print -ru2 -- "E: killed by signal $i"
exit $((128 + i))
}
trap 'trapbc 1' HUP
trap 'trapbc 2' INT
trap 'trapbc 3' QUIT
trap 'trapbc 5' TRAP
trap 'trapbc 13' PIPE
trap 'trapbc 15' TERM
tobc 'scale = 30'
tobc 'define r(x) {'
tobc ' auto o'
tobc ' o = scale'
tobc ' scale = 0'
tobc ' x /= 1'
tobc ' scale = o'
tobc ' return (x)'
tobc '}'
tobc 'define i(x) {'
tobc ' if (x == r(x)) return (1)'
tobc ' return (0)'
tobc '}'
tobc 'define s() {'
tobc ' auto a, b, i, j, l;'
tobc ' l = 9999999'
tobc ' for (b = 0; b < 101; b++) {'
tobc ' a = (g - b * e) / c'
tobc ' i = (h - b * f) / d'
tobc ' if (a != i) continue'
tobc ' if (i(a) != 1) continue'
tobc ' j = r(3 * a + b)'
tobc ' if (j < l) l = j'
tobc ' }'
tobc ' if (l < 9999999) {'
tobc ' print "winner: ", l, "\n";'
tobc ' return (l)'
tobc ' }'
tobc ' print "no winner\n";'
tobc ' return (0)'
tobc '}'
tobc 'z = 0'
while :; do
read -r b a x y || die 'cannot read line 1'
[[ $b$a = ButtonA: ]] || die 'wrong line 1'
x=${x#X+}
tobc "c = ${x%,}"
tobc "d = ${y#Y+}"
read -r b a x y || die 'cannot read line 2'
[[ $b$a = ButtonB: ]] || die 'wrong line 2'
x=${x#X+}
tobc "e = ${x%,}"
tobc "f = ${y#Y+}"
read -r b x y || die 'cannot read line 3'
[[ $b = Prize: ]] || die 'wrong line 3'
x=${x#X=}
tobc "g = ${x%,}"
tobc "h = ${y#Y=}"
tobc "z += s()"
frombc r
print -r -- "round $((++rnd)): $r"
read -r r || break
[[ -z $r ]] || die 'line 4 not empty'
done <inputfile
tobc 'print "z = ", z, "\n"'
frombc r
print -r -- ":: $r"
quitbc
#!/bin/mksh
# s2 without the 10000000000000 factor, to validate
die() {
print -ru2 -- "E: $*"
exit 1
}
bc |&
function tobc {
print -pr -- "$@" || die coprocess died trying to write
}
function frombc {
read -p "$@" || die coprocess died trying to read
}
function quitbc {
trap - HUP INT QUIT TRAP PIPE TERM
exec 3>&p
print -ru3 -- quit
exec 3>&-
:
}
function trapbc {
local i=$1
exec 3>&p
exec 3>&-
wait
print -u2
print -ru2 -- "E: killed by signal $i"
exit $((128 + i))
}
trap 'trapbc 1' HUP
trap 'trapbc 2' INT
trap 'trapbc 3' QUIT
trap 'trapbc 5' TRAP
trap 'trapbc 13' PIPE
trap 'trapbc 15' TERM
tobc 'scale = 30'
tobc 'define r(x) {'
tobc ' auto o'
tobc ' o = scale'
tobc ' scale = 0'
tobc ' x /= 1'
tobc ' scale = o'
tobc ' return (x)'
tobc '}'
tobc 'define i(x) {'
tobc ' if (x == r(x)) return (1)'
tobc ' return (0)'
tobc '}'
tobc 'define s() {'
tobc ' auto a, b, i, j, l, q, r, s;'
tobc ' l = 9999999999999999999999999999999999'
tobc ' b = -1'
tobc ' while (1) {'
tobc ' b = b + 1'
tobc ' if (((b * e) > g) || ((b * f) > h)) break'
tobc ' a = (g - b * e) / c'
tobc ' i = (h - b * f) / d'
tobc ' if (a != i) continue'
tobc ' if (i(a) != 1) continue'
tobc ' j = r(3 * a + b)'
tobc ' if (j < l) l = j'
tobc ' }'
tobc ' if (l < 9999999999999999999999999999999999) {'
tobc ' print "winner: ", l, "\n";'
tobc ' return (l)'
tobc ' }'
tobc ' print "no winner\n";'
tobc ' return (0)'
tobc '}'
tobc 'z = 0'
while :; do
read -r b a x y || die 'cannot read line 1'
[[ $b$a = ButtonA: ]] || die 'wrong line 1'
x=${x#X+}
tobc "c = ${x%,}"
tobc "d = ${y#Y+}"
read -r b a x y || die 'cannot read line 2'
[[ $b$a = ButtonB: ]] || die 'wrong line 2'
x=${x#X+}
tobc "e = ${x%,}"
tobc "f = ${y#Y+}"
read -r b x y || die 'cannot read line 3'
[[ $b = Prize: ]] || die 'wrong line 3'
x=${x#X=}
tobc "g = ${x%,}"
tobc "h = ${y#Y=}"
tobc "z += s()"
frombc r
# while [[ $r != *winner* ]]; do
# print -r -- "D: $r"
# frombc r
# done
print -r -- "round $((++rnd)): $r"
read -r r || break
[[ -z $r ]] || die 'line 4 not empty'
done <inputfile
tobc 'print "z = ", z, "\n"'
frombc r
print -r -- ":: $r"
quitbc
#!/bin/mksh
# s2 without the 10000000000000 factor, to validate
die() {
print -ru2 -- "E: $*"
exit 1
}
bc |&
function tobc {
print -pr -- "$@" || die coprocess died trying to write
}
function frombc {
read -p "$@" || die coprocess died trying to read
}
function quitbc {
trap - HUP INT QUIT TRAP PIPE TERM
exec 3>&p
print -ru3 -- quit
exec 3>&-
:
}
function trapbc {
local i=$1
exec 3>&p
exec 3>&-
wait
print -u2
print -ru2 -- "E: killed by signal $i"
exit $((128 + i))
}
trap 'trapbc 1' HUP
trap 'trapbc 2' INT
trap 'trapbc 3' QUIT
trap 'trapbc 5' TRAP
trap 'trapbc 13' PIPE
trap 'trapbc 15' TERM
tobc 'scale = 30'
tobc 'define r(x) {'
tobc ' auto o'
tobc ' o = scale'
tobc ' scale = 0'
tobc ' x /= 1'
tobc ' scale = o'
tobc ' return (x)'
tobc '}'
tobc 'define i(x) {'
tobc ' if (x == r(x)) return (1)'
tobc ' return (0)'
tobc '}'
tobc 'define s() {'
tobc ' auto a, b, i, j, l, q, r, s;'
tobc ' l = 9999999999999999999999999999999999'
tobc ' b = (3*h*c - 3*g*d) / ((c - 3*e)*d - (d - 3*f)*c) - 20'
tobc ' while (1) {'
tobc ' b = b + 1'
tobc ' if (((b * e) > g) || ((b * f) > h)) break'
tobc ' a = (g - b * e) / c'
tobc ' i = (h - b * f) / d'
tobc ' if (a != i) continue'
tobc ' if (i(a) != 1) continue'
tobc ' j = r(3 * a + b)'
tobc ' if (j < l) l = j'
tobc ' }'
tobc ' if (l < 9999999999999999999999999999999999) {'
tobc ' print "winner: ", l, "\n";'
tobc ' return (l)'
tobc ' }'
tobc ' print "no winner\n";'
tobc ' return (0)'
tobc '}'
tobc 'z = 0'
while :; do
read -r b a x y || die 'cannot read line 1'
[[ $b$a = ButtonA: ]] || die 'wrong line 1'
x=${x#X+}
tobc "c = ${x%,}"
tobc "d = ${y#Y+}"
read -r b a x y || die 'cannot read line 2'
[[ $b$a = ButtonB: ]] || die 'wrong line 2'
x=${x#X+}
tobc "e = ${x%,}"
tobc "f = ${y#Y+}"
read -r b x y || die 'cannot read line 3'
[[ $b = Prize: ]] || die 'wrong line 3'
x=${x#X=}
tobc "g = ${x%,}"
tobc "h = ${y#Y=}"
tobc "z += s()"
frombc r
# while [[ $r != *winner* ]]; do
# print -r -- "D: $r"
# frombc r
# done
print -r -- "round $((++rnd)): $r"
read -r r || break
[[ -z $r ]] || die 'line 4 not empty'
done <inputfile
tobc 'print "z = ", z, "\n"'
frombc r
print -r -- ":: $r"
quitbc
#!/bin/mksh
# s2 without the 10000000000000 factor, to validate
die() {
print -ru2 -- "E: $*"
exit 1
}
bc |&
function tobc {
print -pr -- "$@" || die coprocess died trying to write
}
function frombc {
read -p "$@" || die coprocess died trying to read
}
function quitbc {
trap - HUP INT QUIT TRAP PIPE TERM
exec 3>&p
print -ru3 -- quit
exec 3>&-
:
}
function trapbc {
local i=$1
exec 3>&p
exec 3>&-
wait
print -u2
print -ru2 -- "E: killed by signal $i"
exit $((128 + i))
}
trap 'trapbc 1' HUP
trap 'trapbc 2' INT
trap 'trapbc 3' QUIT
trap 'trapbc 5' TRAP
trap 'trapbc 13' PIPE
trap 'trapbc 15' TERM
tobc 'scale = 30'
tobc 'define r(x) {'
tobc ' auto o'
tobc ' o = scale'
tobc ' scale = 0'
tobc ' x /= 1'
tobc ' scale = o'
tobc ' return (x)'
tobc '}'
tobc 'define i(x) {'
tobc ' if (x == r(x)) return (1)'
tobc ' return (0)'
tobc '}'
tobc 'define s() {'
tobc ' auto a, b, i, j, l, n, q, r, s;'
tobc ' l = 9999999999999999999999999999999999'
tobc ' b = (3*h*c - 3*g*d) / ((c - 3*e)*d - (d - 3*f)*c) - 100'
tobc ' for (n = 0; n < 250; ++n) {'
tobc ' b = b + 1'
tobc ' if (((b * e) > g) || ((b * f) > h)) break'
tobc ' a = (g - b * e) / c'
tobc ' i = (h - b * f) / d'
tobc ' if (a != i) continue'
tobc ' if (i(a) != 1) continue'
tobc ' j = r(3 * a + b)'
tobc ' if (j < l) l = j'
tobc ' }'
tobc ' if (l < 9999999999999999999999999999999999) {'
tobc ' print "winner: ", l, "\n";'
tobc ' return (l)'
tobc ' }'
tobc ' print "no winner\n";'
tobc ' return (0)'
tobc '}'
tobc 'z = 0'
while :; do
read -r b a x y || die 'cannot read line 1'
[[ $b$a = ButtonA: ]] || die 'wrong line 1'
x=${x#X+}
tobc "c = ${x%,}"
tobc "d = ${y#Y+}"
read -r b a x y || die 'cannot read line 2'
[[ $b$a = ButtonB: ]] || die 'wrong line 2'
x=${x#X+}
tobc "e = ${x%,}"
tobc "f = ${y#Y+}"
read -r b x y || die 'cannot read line 3'
[[ $b = Prize: ]] || die 'wrong line 3'
x=${x#X=}
tobc "g = ${x%,} + 10000000000000"
tobc "h = ${y#Y=} + 10000000000000"
tobc "z += s()"
frombc r
# while [[ $r != *winner* ]]; do
# print -r -- "D: $r"
# frombc r
# done
print -r -- "round $((++rnd)): $r"
read -r r || break
[[ -z $r ]] || die 'line 4 not empty'
done <inputfile
tobc 'print "z = ", z, "\n"'
frombc r
print -r -- ":: $r"
quitbc
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment