Jars Puzzle
Several variations of a puzzle involving filling jars with liquid.
Functions:
add(),
and(),
ansi(),
baseconv(),
cand(),
center(),
eq(),
extract(),
filter(),
gt(),
if(),
ifelse(),
inc(),
iter(),
lit(),
lnum(),
lte(),
max(),
min(),
neq(),
or(),
parent(),
rjust(),
set(),
setq(),
setr(),
space(),
sub(),
switch(),
u(),
v().
Features:
#lambda,
regexp commands.
Instructions
Copy and paste the below code into a compatible MUSH or MUX.
MUSHCode for Jars Puzzle
@create Jars
@lock Jars==me
@lset Jars/Basic=no_inherit
@set Jars = LINK_OK
@set Jars = VISUAL
@set Jars = !NO_COMMAND
&CMND Jars=
&CMND`J-EMPTY Jars=$^j/empty ([abc]): @assert [v(data`empty_ok)]=@pemit %#=This problem does not allow you to empty jars.; @pemit %#=[u(ufun`empty,%1)][u(ufun`display)]; @emit %n empties %1.
@set Jars/CMND`J-EMPTY=regexp
&CMND`J-FILL Jars=$^j/fill ([abc]): @assert [v(data`fill_ok)]=@pemit %#=This problem does not allow you to use the faucet.; @pemit %#=[u(ufun`fill,%1)][u(ufun`display)]; @emit %n fills %1 with water.
@set Jars/CMND`J-FILL=regexp
&CMND`J-POUR_X-X Jars=$^j/pour ([abc]) to ([abc]):@break [u(ufun`pour`error,%1,%2)]=@pemit %#=You can't do that.; @pemit %#=[u(ufun`pour,%1,%2)][u(ufun`display)]; @emit %n pours from %1 to %2.
@set Jars/CMND`J-POUR_X-X=regexp
&CMND`J-RESET Jars=$^j/reset ([1-8]):@emit %n resets the jars to puzzle %1.[u(ufun`reset,%1)]; @pemit %#=Objective: [u(data`objective)]%r[u(ufun`display)]
@set Jars/CMND`J-RESET=regexp
&DATA Jars=
&DATA`A Jars=0
&DATA`B Jars=0
&DATA`C Jars=0
&DATA`CAP Jars=
&DATA`CAP`A Jars=9
&DATA`CAP`B Jars=4
&DATA`CAP`C Jars=0
&DATA`EMPTY_OK Jars=1
&DATA`FILL_OK Jars=1
&DATA`GOAL Jars=
&DATA`GOAL`A Jars=6
&DATA`GOAL`B Jars=0
&DATA`GOAL`C Jars=0
&DATA`OBJECTIVE Jars=End up with 6 units in jar a
&DESCRIBE Jars=Jars puzzle by tramp and Javelin. [switch(parent(me),#-1,%rTo get your own copy\, make an object\, set it !no_command\, and @parent it to %!)]%rObjective: [u(data`objective)]%r%r[u(ufun`display)]%r%R[u(ufun`help)]
@set Jars/DESCRIBE=no_command visual prefixmatch public nearby
&PUZZLE Jars=
&PUZZLE`1 Jars=10 7 3 10 0 0 5 5 0 0 0 End up with 5 units in each of jars a and b
&PUZZLE`2 Jars=5 3 0 0 0 0 4 0 0 1 1 End up with 4 units in jar a
&PUZZLE`3 Jars=11 7 0 0 0 0 6 0 0 1 1 End up with 6 units in jar a
&PUZZLE`4 Jars=8 5 0 0 0 0 4 0 0 1 1 End up with 4 units in jar a
&PUZZLE`5 Jars=8 5 3 8 0 0 4 4 0 0 0 End up with 4 units in each of jars a and b
&PUZZLE`6 Jars=10 7 0 0 0 0 9 0 0 1 1 End up with 9 units in jar a
&PUZZLE`7 Jars=12 8 5 12 0 0 6 6 0 0 0 End up with 6 units in each of jars a and b
&PUZZLE`8 Jars=9 4 0 0 0 0 6 0 0 1 1 End up with 6 units in jar a
&PUZZLE`TEMPLATE Jars=a-cap b-cap c-cap a-start b-start c-start a-goal b-goal c-goal fill-ok empty-ok objective-string
&UFUN Jars=
&UFUN`DISPLAY Jars=[setq(j,filter(lit(#lambda/gt(v(data`cap`%0),0)),a b c))][iter(lnum(setr(m,inc(max(v(data`cap`a),v(data`cap`b),v(data`cap`c)))),0),ifelse(and(neq(%i0,%qm),neq(%i0,0)),rjust(%i0,2),space(2)) [iter(%qj,center(switch(1,[eq(%i1,0)],=====,[eq(%i1,inc(v(data`cap`%i0)))],\(\)%b\(%b,[lte(%i1,v(data`%i0))],|[ansi(hbB,~~~)]|,[lte(%i1,v(data`cap`%i0))],| %b |),9))],%b,%r)]%r[space(3)][iter(%qj,center(%i0,9))]%r[space(3)][iter(%qj,center(Cur/Cap,9))]%r[space(3)][iter(%qj,center(v(data`%i0)/[v(data`cap`%i0)],9))]%r[if(u(ufun`success),ansi(hg,You solved it!))]
&UFUN`EMPTY Jars=[set(me,data`%0:0)]
&UFUN`FILL Jars=[set(me,data`%0:[v(data`cap`%0)])]
&UFUN`HELP Jars=j/pour <source> to <destination>[if(v(data`fill_ok),%rj/fill <destination>)][if(v(data`empty_ok),%rj/empty <source>)]%rj/reset <puzzle #> (1-8)
&UFUN`POUR Jars=[setq(p, min(v(data`%0), sub(v(data`cap`%1), v(data`%1))))][set(me, data`%0:[sub(v(data`%0), %qp)])][set(me, data`%1:[add(v(data`%1), %qp)])]
&UFUN`POUR`ERROR Jars=[or(eq(v(data`%0),0),eq(v(data`%1),v(data`cap`%1)))]
&UFUN`RESET Jars=[iter(lnum(1,11),setq(baseconv(##,10,16),extract(v(puzzle`%0),##,1)))][set(me,data`cap`a:%q1)][set(me,data`cap`b:%q2)][set(me,data`cap`c:%q3)][set(me,data`a:%q4)][set(me,data`b:%q5)][set(me,data`c:%q6)][set(me,data`goal`a:%q7)][set(me,data`goal`b:%q8)][set(me,data`goal`c:%q9)][set(me,data`fill_ok:%qa)][set(me,data`empty_ok:%qb)][set(me,data`objective:[extract(v(puzzle`%0),12,9999)])]
&UFUN`SUCCESS Jars=[cand(eq(v(data`a),v(data`goal`a)),eq(v(data`b),v(data`goal`b)),eq(v(data`c),v(data`goal`c)))]
l Jars
@lock Jars==me
@lset Jars/Basic=no_inherit
@set Jars = LINK_OK
@set Jars = VISUAL
@set Jars = !NO_COMMAND
&CMND Jars=
&CMND`J-EMPTY Jars=$^j/empty ([abc]): @assert [v(data`empty_ok)]=@pemit %#=This problem does not allow you to empty jars.; @pemit %#=[u(ufun`empty,%1)][u(ufun`display)]; @emit %n empties %1.
@set Jars/CMND`J-EMPTY=regexp
&CMND`J-FILL Jars=$^j/fill ([abc]): @assert [v(data`fill_ok)]=@pemit %#=This problem does not allow you to use the faucet.; @pemit %#=[u(ufun`fill,%1)][u(ufun`display)]; @emit %n fills %1 with water.
@set Jars/CMND`J-FILL=regexp
&CMND`J-POUR_X-X Jars=$^j/pour ([abc]) to ([abc]):@break [u(ufun`pour`error,%1,%2)]=@pemit %#=You can't do that.; @pemit %#=[u(ufun`pour,%1,%2)][u(ufun`display)]; @emit %n pours from %1 to %2.
@set Jars/CMND`J-POUR_X-X=regexp
&CMND`J-RESET Jars=$^j/reset ([1-8]):@emit %n resets the jars to puzzle %1.[u(ufun`reset,%1)]; @pemit %#=Objective: [u(data`objective)]%r[u(ufun`display)]
@set Jars/CMND`J-RESET=regexp
&DATA Jars=
&DATA`A Jars=0
&DATA`B Jars=0
&DATA`C Jars=0
&DATA`CAP Jars=
&DATA`CAP`A Jars=9
&DATA`CAP`B Jars=4
&DATA`CAP`C Jars=0
&DATA`EMPTY_OK Jars=1
&DATA`FILL_OK Jars=1
&DATA`GOAL Jars=
&DATA`GOAL`A Jars=6
&DATA`GOAL`B Jars=0
&DATA`GOAL`C Jars=0
&DATA`OBJECTIVE Jars=End up with 6 units in jar a
&DESCRIBE Jars=Jars puzzle by tramp and Javelin. [switch(parent(me),#-1,%rTo get your own copy\, make an object\, set it !no_command\, and @parent it to %!)]%rObjective: [u(data`objective)]%r%r[u(ufun`display)]%r%R[u(ufun`help)]
@set Jars/DESCRIBE=no_command visual prefixmatch public nearby
&PUZZLE Jars=
&PUZZLE`1 Jars=10 7 3 10 0 0 5 5 0 0 0 End up with 5 units in each of jars a and b
&PUZZLE`2 Jars=5 3 0 0 0 0 4 0 0 1 1 End up with 4 units in jar a
&PUZZLE`3 Jars=11 7 0 0 0 0 6 0 0 1 1 End up with 6 units in jar a
&PUZZLE`4 Jars=8 5 0 0 0 0 4 0 0 1 1 End up with 4 units in jar a
&PUZZLE`5 Jars=8 5 3 8 0 0 4 4 0 0 0 End up with 4 units in each of jars a and b
&PUZZLE`6 Jars=10 7 0 0 0 0 9 0 0 1 1 End up with 9 units in jar a
&PUZZLE`7 Jars=12 8 5 12 0 0 6 6 0 0 0 End up with 6 units in each of jars a and b
&PUZZLE`8 Jars=9 4 0 0 0 0 6 0 0 1 1 End up with 6 units in jar a
&PUZZLE`TEMPLATE Jars=a-cap b-cap c-cap a-start b-start c-start a-goal b-goal c-goal fill-ok empty-ok objective-string
&UFUN Jars=
&UFUN`DISPLAY Jars=[setq(j,filter(lit(#lambda/gt(v(data`cap`%0),0)),a b c))][iter(lnum(setr(m,inc(max(v(data`cap`a),v(data`cap`b),v(data`cap`c)))),0),ifelse(and(neq(%i0,%qm),neq(%i0,0)),rjust(%i0,2),space(2)) [iter(%qj,center(switch(1,[eq(%i1,0)],=====,[eq(%i1,inc(v(data`cap`%i0)))],\(\)%b\(%b,[lte(%i1,v(data`%i0))],|[ansi(hbB,~~~)]|,[lte(%i1,v(data`cap`%i0))],| %b |),9))],%b,%r)]%r[space(3)][iter(%qj,center(%i0,9))]%r[space(3)][iter(%qj,center(Cur/Cap,9))]%r[space(3)][iter(%qj,center(v(data`%i0)/[v(data`cap`%i0)],9))]%r[if(u(ufun`success),ansi(hg,You solved it!))]
&UFUN`EMPTY Jars=[set(me,data`%0:0)]
&UFUN`FILL Jars=[set(me,data`%0:[v(data`cap`%0)])]
&UFUN`HELP Jars=j/pour <source> to <destination>[if(v(data`fill_ok),%rj/fill <destination>)][if(v(data`empty_ok),%rj/empty <source>)]%rj/reset <puzzle #> (1-8)
&UFUN`POUR Jars=[setq(p, min(v(data`%0), sub(v(data`cap`%1), v(data`%1))))][set(me, data`%0:[sub(v(data`%0), %qp)])][set(me, data`%1:[add(v(data`%1), %qp)])]
&UFUN`POUR`ERROR Jars=[or(eq(v(data`%0),0),eq(v(data`%1),v(data`cap`%1)))]
&UFUN`RESET Jars=[iter(lnum(1,11),setq(baseconv(##,10,16),extract(v(puzzle`%0),##,1)))][set(me,data`cap`a:%q1)][set(me,data`cap`b:%q2)][set(me,data`cap`c:%q3)][set(me,data`a:%q4)][set(me,data`b:%q5)][set(me,data`c:%q6)][set(me,data`goal`a:%q7)][set(me,data`goal`b:%q8)][set(me,data`goal`c:%q9)][set(me,data`fill_ok:%qa)][set(me,data`empty_ok:%qb)][set(me,data`objective:[extract(v(puzzle`%0),12,9999)])]
&UFUN`SUCCESS Jars=[cand(eq(v(data`a),v(data`goal`a)),eq(v(data`b),v(data`goal`b)),eq(v(data`c),v(data`goal`c)))]
l Jars