Skip to content

Instantly share code, notes, and snippets.

@pjt33
Last active August 29, 2015 14:00
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 pjt33/11357839 to your computer and use it in GitHub Desktop.
Save pjt33/11357839 to your computer and use it in GitHub Desktop.
Hunt the Wumpus; refactors of Dennis' code
##
0:DBGID;
{['"#{$stderr.puts Garray.new($stack).ginspect.to_s.slice(1..-2)#'DBGID):DBGID'
}"']''*~;}:DEBUG;
##
#[5:C,]{{.{[~@]}:>~.{-1%}:<~}%.&}8*({[.<><.<><]}:&~-{99rand}$~5,{.<[.>.>]{&~}%.3{$?~!!}:Q~"You smell a wumpus.\n"{*print}:,~.4Q"You feel a breeze.\n",'"#{'C):C';STDIN.gets()}"'++~~.10/3%@3%=\5%{&2Q"You killed the wumpus."{\<{>}3rand*\1"Your arrow didn't hit anything.\n",0}if}{\;.&3Q"You fell into the pit."*}if 1$&3Q{;"You were killed by the wumpus."}*:n!}do];
#[5:C,]{{.{[~@]}:>~.{-1%}:<~}%.&}8*({[.<><.<><]}:&~-{99rand}$~5,{.<[.>.>].{&3{$?~!!}:Q~'You smell a wumpus.\n'{*print}:,~}/.{&4Q'You feel a breeze.\n',}/'"#{'C):C';STDIN.gets()}"'++~~.10/3%@=\5%{&2Q'You killed the wumpus.'{\<{>}3rand*\1"Your arrow didn't hit anything.\n",0}if}{\;.&3Q'You fell into the pit.'*}if 1$&3Q{;'You were killed by the wumpus.'}*:n!}do];
#[5:C,]{{.{[~@]}:>~.{-1%}:<~}%.&}8*({[.<><.<><]}:&~-{99rand}$~5,{.<[.>.>].{3{$\&\?~!!}:Q~'You smell a wumpus.\n'{*print}:,~}/.{4Q'You feel a breeze.\n',}/'"#{'C):C';STDIN.gets()}"'++~~.10/3%@=\5%{2Q'You killed the wumpus.'{\<{>}3rand*\1"Your arrow didn't hit anything.\n",0}if}{\;.3Q'You fell into the pit.'*}if 1$3Q{;'You were killed by the wumpus.'}*:n!}do];
#[5:C,]{{.{[~@]}:>~.{-1%}:<~}%.&}8*({[.<><.<><]}:F~-{99rand}$~5,{<{>.2{$\F\?~!!}:Q~'You smell a wumpus.\n'{*print}:,~}3*{>.3Q'You feel a breeze.\n',}3*'"#{'C):C';STDIN.gets()}"'++~~.10/2$\{>}*\5%{2Q'You killed the wumpus.'{\<{>}3rand*\<1"Your arrow didn't hit anything.\n",0}if}{\;.3Q'You fell into the pit.'*}if 1$3Q{'You were killed by the wumpus.'}*:n!}do];
#[5:C,]{{.{[~@]}:>~.{-1%}:<~}%.&}8*({[.<><.<><]}:F~-{99rand}$~5,{<{>.2{$\F\?~!!}:Q~'You smell a wumpus.\n'{*print}:,~}3*.[{.>.3Q'You feel a breeze.\n',}3*]'"#{'C):C';STDIN.gets()}"'++~~.10/@=\5%{2Q'You killed the wumpus.'{\<{>}3rand*\<1"Your arrow didn't hit anything.\n",0}if}{\;.3Q'You fell into the pit.'*}if 1$3Q{'You were killed by the wumpus.'}*:n!}do];
#[5:C,]{{.{[~@]}:>~.{-1%}:<~}%.&}8*({[.<><.<><]}:F~-{99rand}$~5,{.<{>.3{$\F\?~!!}:Q~'You smell a wumpus.\n'{*print}:,~}3*{>.4Q'You feel a breeze.\n',}3*'"#{'C):C';STDIN.gets()}"'++~~:&10/{>}*&5%{2Q'You killed the wumpus.'{\<{>}3rand*\1"Your arrow didn't hit anything.\n",0}if}{\;.3Q'You fell into the pit.'*}if 1$3Q{'You were killed by the wumpus.'}*:n!}do];
#[5:C,]{{.{[~@]}:>~.{-1%}:<~}%.&}8*({[.<><.<><]}:F~-{99rand}$~5,{.<{>.3{$F\?~!!}:Q~'You smell a wumpus.\n'{*print}:,~}3*{>.4Q'You feel a breeze.\n',}3*'"#{'C):C';STDIN.gets()}"'++~~:&10/{>}*&5%{2Q'You killed the wumpus.'{\<{>}3rand*\1"Your arrow didn't hit anything.\n",0}if}{\;.3Q'You fell into the pit.'*}if 1$3Q{'You were killed by the wumpus.'}*:n!}do];
#[5:C,]{{.{[~@]}:>~.{-1%}:<~}%.&}8*({[.<><.<><]}:F~-{99rand}$~5,{.<{>.4'You smell a wumpus.\n'{@F@$?~!!*}:Q~{print}:,~}3*{>.5'You feel a breeze.\n'Q,}3*'"#{'C):C';STDIN.gets()}"'++~~:&10/{>}*&5%{3'You killed the wumpus.'Q{\<{>}3rand*\"Your arrow didn't hit anything.\n",0}or}{\;.4'You fell into the pit.'Q}if\.4'You were killed by the wumpus.'Q@or:n!}do];
#[5:C,]{{.{[~@]}:>~.{-1%}:<~}%.&}8*({[.<><.<><]}:F~-{99rand}$~5,{.<{>.'You smell a wumpus.\n'4{$F@?~!!*}:Q~{print}:,~}3*{>.'You feel a breeze.\n'5Q,}3*'"#{'C):C';STDIN.gets()}"'++~~:&10/{>}*&5%{'You killed the wumpus.'3Q{\<{>}3rand*\"Your arrow didn't hit anything.\n",0}or}{\;.'You fell into the pit.'4Q}if\.'You were killed by the wumpus.'4Q@or:n!}do];
#[5:C,]{{.{[~@]}:>~.{-1%}:<~}%.&}8*({[.<><.<><]}:F~-{99rand}$~5,{.<{>.'You smell a wumpus.\n'4{$F@?~!!*}:Q~{print}:,~}3*{>.'You feel a breeze.\n'5Q,}3*'"#{'C):C';STDIN.gets()}"'++~~:&9/{>}*&5%{'You killed the wumpus.'3Q{\<{>}3rand*\"Your arrow didn't hit anything.\n",0}or}{\;.'You fell into the pit.'4Q}if\.'You were killed by the wumpus.'4Q@or:n!}do];
# Generate the 60 permutations by repeated application of `3 1` and `3`
[5:C,]{{.{[~@]}:>~.{-1%}:<~}%.&}8*
# Remove [0 1 2 4 5] and its equivalence class (apply 3 (3 1)^n 3 for n in 0,1,2)
({[.<><.<><]}:F~-
# Shuffle the remaining 57 options to select random starting points for wumpus and pit
# Note that this introduces a slight bias against them being in the same room,
# but it's still possible.
{
##
;
##
99rand}$~
##
{;}55*
##
# Start player at [0 1 2 3 4]
5,
{
##
DEBUG
##
# Stack: Pit Wumpus Player
.<
# Find the adjacent rooms to the player: 3 (3 1)^n for n in 0,1,2
# If the wumpus is in one of those rooms, say so.
{>."You smell a wumpus.\n"4{
# ... A str off
$F@?~!!*
# ... str*(off$F A ?~!!)
}:Q~{print}:,~}3*
# Ditto for the pit
{>."You feel a breeze.\n"5Q,}3*
# Read one line from STDIN.
'"#{'C):C';STDIN.gets()}"'++~~
# Stack: Pit Wumpus Player Player< Input
# Find the room corresponding to the specified direction.
:&9/{>}*&
# Stack: Pit Wumpus Player TargetRoom Input
5%{
# Shoots:
"You killed the wumpus."3Q
{
\<{>}3rand*\ # Move the wumpus to an adjacent room
"Your arrow didn't hit anything.\n", # Inform
0 # Continue
}
or
}{
# Moves:
\;
# If player and pit share a room, say so.
."You fell into the pit."4Q
}if
# If player and wumpus share a room, say so.
\."You were killed by the wumpus."4Q@or
# Save the topmost element of the stack for output if we break the loop. Loop if it's falsy.
:n!
}do
# Ditch the junk.
];
# 386, straight refactoring
{]-1:&=}:E{puts}:J'npoemfsgnohtksblbtpckdpljqnriogelfhkbqrcaiadjhagimsmjtqecrdf'{97-}%3/:^{19rand}2*3:&19{.^=.+.3$?>.4$?~!!{"You smell a wumpus."J}*.5$?~!!{"You feel a breeze."J}*'"#{'&):&';STDIN.gets()}"'++~~.10/@=\5%{3$={"You killed the wumpus."E}{"Your arrow didn't hit anything."J@^=3rand=@@}if}{@;}if.3$={"You were killed by the wumpus."E}{.4$={"You fell into the pit."E}*}if&)}do
# 376, with crappier string formatting
{puts}:J'npoemfsgnohtksblbtpckdpljqnriogelfhkbqrcaiadjhagimsmjtqecrdf'{97-}%3/:^{19rand}2*3:&19{.^=.+.3$?>.4$?~!!"You smell a wumpus."*J.5$?~!!"You feel a breeze."*J'"#{'&):&';STDIN.gets()}"'++~~.10/@=\5%{3$="You killed the wumpus."{@^=3rand=@@"Your arrow didn't hit anything."}if}{@;.4$="You fell into the pit."*}if 1$4$={"You were killed by the wumpus."+}*.J"e"?0<}do];
# Ungolfed version of the latter
{puts}:J
'npoemfsgnohtksblbtpckdpljqnriogelfhkbqrcaiadjhagimsmjtqecrdf'{97-}%3/:^ # Adjacency lists
{19rand}2* # Place wumpus and pit in rooms different from 19
3:&19 # The player starts with his back to room 3 in room 19
## Pit Wumpus Prev Curr
{
.^=.+.3$?> # Double the adjacent rooms and remove all letters before first occurrence of the player's previous location.
# Pit Wumpus Prev Curr Adj'
.4$?~!!"You smell a wumpus."*J # If the wumpus is in one of those rooms, say so.
.5$?~!!"You feel a breeze."*J # If the pit is in one of those rooms, say so.
'"#{'&):&';STDIN.gets()}"'++~~. # Read one line from STDIN and duplicate it.
# Pit Wumpus Prev Curr Adj' Input Input
10/@=\ # Find the room corresponding to the specified direction
# Pit Wumpus Prev Curr TargetRoom Input
5%{
# If the player shoots an arrow
3$=
"You killed the wumpus." # Hit
{@^=3rand=@@"Your arrow didn't hit anything."} # Miss; wumpus is spooked
if
}{
@; # Move player to the selected room.
.4$="You fell into the pit."* # This is the only opportunity to fall into a pit.
}if
# Pit Wumpus Prev Curr Output
1$4$={"You were killed by the wumpus."+}* # If player and wumpus share a room, append to output.
.J
"e"?0< # Repeat if the letter "e" wasn't in the output string (which is a surprising and convenient test)
}do
]; # Ditch the stack
@pjt33
Copy link
Author

pjt33 commented Apr 29, 2014

Incidentally I'm pretty certain that using a matrix representation (e.g. a=[[1 0][2 1]], b=[[0 1][1 1]] over GF(2^2)) would be even worse. And I've completely given up on using words over the alphabet "ab": a full set of necessary reduction rules would take far too much space, as would deriving them in code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment