16
\$\begingroup\$

Don't you hate it when you can't remember how to craft something in Minecraft? Well, time to remedy that!

Task

Your task is to take an input, either as a 1D or 2D list and output what item is a result of the crafting table!

Input

You may take input as either a string or a list of length 9 or a 2D nested array.

If you take input as a...

String

Each item in the table is 1 char in the printable ASCII character (0x20 to 0x7E). To represent an empty space, you use a - e.g. WWW-W-WWW would be the same as a crafting table like

+------+------+------+
| Wood | Wood | Wood |
+------+------+------+
|      | Wood |      |
+------+------+------+
| Wood | Wood | Wood |
+------+------+------+

You may also take input as a multiline string as long as all spaces are preserved e.g.

WWW
 W 
WWW

1D array

You would take the input as a char array where empty spaces on the crafting table would be an empty char e.g. the above table would be ['W','W','W','','W','','W','W','W']

2D array

This time, each list represents a line on the crafting table, where an empty space is an empty char e.g. [['W','W','W'],['','W',''],['W','W','W']]

You can assume that the input will always correspond to an item that can be crafted, and if using array input, you may replace the empty char with any character not used as an abbreviated name.

Output

Output will be the item crafted from the input, in any form you want, as long as it is obvious what it means. (Personally, I would use my 1 letter abbreviations of the items)

Ingredients

For this challenge, wood means wooden planks, not wooden logs.

You only have to handle the most common items when crafting. These are the ingredients for crafting but may also be products of crafting. The crafting recipe follows the format xxxxxxxxx like the string input above. If an item cannot be crafted, --------- is placed instead.

Some recipes are shapeless, meaning that just so long as all items are there, the item will be made. These are denoted be a *. An example of this would be pumpkin pie (not one in this challenge) that only needs pumpkin, sugar and egg to be made.

   item name    | abb. name | crafting recipe
----------------+-----------+------------------
wood            |     W     | ---------
cobblestone     |     C     | ---------
sticks          |     S     | ----W--W-
iron            |     I     | ---------
gold            |     G     | ---------
diamond         |     D     | ---------
redstone        |     R     | ---------
string          |     N     | ---------
coal            |     Y     | ---------
sugar cane      |     U     | ---------
redstone torch  |     E     | -R--S----
paper           |     P     | ---------
book            |     B     | ---PL-PP-
wool            |     M     | ---NN-NN-
obsidian        |     O     | ---------
gunpowder       |     X     | ---------
sand            |     A     | ---------
glass           |     H     | ---------
feather         |     F     | ---------
flint           |     K     | ---------
torch           |     T     | ---------
leather         |     L     | ---------
material blocks |     Z     | QQQQQQQQQ (here Q represents any in `[G, I, R, D, Y]`
compass         |     V     | -I-IRI-I-

Products

This is a list of all the products that you could handle for crafting. if the item is also an ingredient, it will not be included here but you must still be able to handle it. Each one can be crafted using only the ingredients above and is designated a unique, lowercase letter to identify it. Armour (+) can use any ingredient in [G, I, L, D]. Weapons ($) can be made from [W, C, I, G, D]. As this is a site, however, we need to make these lists shorter. [G, I, L, D] is denoted by a Q and [W, C, I, G, D] is denoted by a J.

   item name    | abb. name | crafting recipe
----------------+-----------+------------------
crafting table  |     a     | -----WWWW
boots +         |     b     | ---Q-QQ-Q
pants +         |     c     | QQQQ-QQ-Q
chestplate +    |     d     | Q-QQQQQQQ
helmet +        |     e     | QQQQ-Q---
bed             |     f     | ---MMMWWW
chest           |     g     | WWWW-WWWW
wood door       |     h     | WW-WW-WW-
iron door       |     i     | II-II-II-
furnace         |     j     | CCCC-CCCC
trap door       |     k     | ---WW-WW-
TNT             |     l     | XAXAXAXAX
anvil           |     m     | ZZZ-I-III
axe $           |     n     | -JJ-SJ-S-
sword $         |     o     | -J--J--S-
pickaxe $       |     p     | JJJ-S--S-
hoe $           |     q     | JJ--S--S-
shovel $        |     r     | -J--S--S-
arrow           |     s     | -K--S--F-
bow             |     t     | SN-S-NSN-
bowl            |     u     | ---W-W-W-
gold nugget *   |     v     | ----G----
bucket          |     w     | ---I-I-I-
clock           |     y     | -G-GRG-G-
map             |     z     | PPPPVPPPP
fishing rod     |     1     | --S-SNS-N
flint and steel |     2     | ----I---K
shears          |     3     | ---I---I-
wood button *   |     4     | ----W----
dropper         |     5     | CCCC-CCRC
stone button *  |     6     | ----C----
jukebox         |     7     | WWWWDWWWW
lever           |     8     | ----S--C-
noteblock       |     9     | WWWWRWWWW
piston          |     0     | WWWCICCRC
pressure plate  |     !     | ------WW- (W can also be replaced with C/I/G)
repeater        |     @     | ---ERECCC
tripwire hook   |     #     | -I--S--W-
activator rail  |     (     | ISIIEIISI
boat            |     %     | ---W-WWWW
minecart        |     ^     | ---I-IIII
powered rail    |     &     | G-GGSGGRG
rail            |     )     | I-IISII-I
stone wall      |     {     | ---CCCCCC
fence gate      |     }     | ---SWSSWS
fence panel     |     [     | ---SSSSSS
glass pane      |     ]     | ---HHHHHH
iron bars       |     ;     | ---IIIIII
item frame      |     :     | SSSSLSSSS
ladder          |     '     | S-SSSSS-S
sandstone       |     "     | -----AAAA
slabs           |     |     | ---CCC---
stairs          |     <     | W--WW-WWW
bookshelves     |     >     | WWWBBBWWW
carpet          |     .     | -------MM
painting        |     ,     | SSSSMSSSS
sign            |     ?     | WWWWWW-S-
cauldron        |     /     | I-II-IIII
enchant. table  |     `     | -B-DODOOO
glass bottle    |     ~     | ---H-H-H-
rocket *        |     _     | -----PXXX

Scoring

As it would be unreasonable to ask you to do all of these recipes, you only have to do the ones that you want! But of course, the more that you do, the better your score.

Score is defined as

score = length of program in bytes / number of working recipes squared

For example, this could be a submission.

input()
print('S')

As you can assume that the input passed will be one that it can handle (----W--W-), it will always output S, which is equivalent to sticks. This would score 18 / 1 = 18.

You must take input to be a valid program and you must be able to handle at least 5 different inputs.

The person with the lowest score wins.

Rules

  • Lowest score wins
  • You may take input using any accepted method (function parameters, STDIN etc)
  • You can only take input in one of the above forms. It isn't too restrictive and should be workable
  • Standard loopholes are disallowed
  • You must take input
  • You must output at least 5 correct results to qualify as competing.
  • For recipes that are shapeless, e.g. gold nuggets, different combinations do not count as different recipes. ----W---- is the same (recipe-wise) as --W------ and is only 1 recipe.
  • On the flip side of this, for recipes that use more than one material, such as material blocks, each different material counts as a different recipe, meaning that IIIIIIIII is not the same (recipe-wise) as RRRRRRRRR.

Good luck, Minecrafters!

\$\endgroup\$
11
  • 1
    \$\begingroup\$ sticks may be crafted by W--W-----, -W--W----, --W--W---, ---W--W--, ----W--W-, or, -----W--W. do we need handle only one of these or all of these? \$\endgroup\$
    – tsh
    Jul 17, 2017 at 7:37
  • \$\begingroup\$ may I take input as an array of strings? (e.g. ["WWW", "CIC", "CRC"]) \$\endgroup\$
    – dzaima
    Jul 17, 2017 at 11:35
  • 1
    \$\begingroup\$ Answers in Minecraft Code, 5 bytes \$\endgroup\$ Jul 17, 2017 at 14:15
  • 1
    \$\begingroup\$ @MagicOctopusUrn Minecraft is written in Java so no way that's possible :p \$\endgroup\$
    – dzaima
    Jul 17, 2017 at 14:22
  • 1
    \$\begingroup\$ Minecraft Code: Input is as a configuration on a crafting table, and output is the output from the crafting table. Easy. :p /s \$\endgroup\$
    – hyper-neutrino
    Jul 17, 2017 at 22:29

4 Answers 4

6
\$\begingroup\$

Python 2, Score: 0.0636347

715 bytes, all 106 recipes

import zlib,base64
i=input()
x=`sorted(i)`[2::5]
W=zlib.decompress(base64.b64decode('eNpVUglywyAMfJEeEUPqaoKPQTPFzf8fUq0O6iyyLJsFLUhERMOM6Gx0ngj2HWYYijXBiZ6oid8EM7nZYhhW0orvTiS2qxK6PhHCmO+B527UYK3dA5+qVGG13gOf0lyr5YyAUjxN+SZDKPZsLXawXXxKsW3bcNB8wYrC3PWI8X6/7RiMaRlYrhQpCPSnMAIVIisCzSEVgV87yGYgm4FsBrKZJFOSKcmUZEoy1V7mv5KjaxeqRz2Og+i/rE7GaSyAfgtwcxbgOvI2DOB+/gH39ue8rkt2/drF+mfY8CpbVYVk19lT8QOHavXoDo7H/QsSa1Rg9HgXLqWXKEEvRHknIW4ebyqOgHkqhn/2p/JZmJ+s3qV6ledxvGHQP7KqyI9GlejS4lqIvm1AzhCYopmn8OAYTIx3oeKh0KzLsnjy2W1RIMe2YRNzSGH4AwHh7JI='))
d={W[l:l+9]:'SBMaZZZZZikeEVbcebcebcebcddqddqfghjlmnnnnnooooorrrrrs#`4ppppp6qvq_tuw1zy3279058|!!!!@(%q^&/){~}:\'];"><?.,['[l/9]for l in range(0,len(W),9)}
if i in d:x=i
print d[x]

Try it online!

Handles all the specified recipes, including material variations.

Creates a dictionary of all working recipes (with length 9).

The shapeless recipes are included by sorting the input string, and checking those against the dictionary.

Edit: Switched to string compression, and saved 7 bytes in the code thanks to notjagan

\$\endgroup\$
4
  • \$\begingroup\$ I count 110 recipes \$\endgroup\$
    – dzaima
    Jul 17, 2017 at 14:00
  • \$\begingroup\$ -7 bytes. \$\endgroup\$
    – notjagan
    Jul 17, 2017 at 14:06
  • \$\begingroup\$ @dzaima Armor only has 4 vairants (GILD) so i count 106 \$\endgroup\$
    – TFeld
    Jul 17, 2017 at 14:18
  • 1
    \$\begingroup\$ @TFeld oh yeah, coal armor doesn't exist :| \$\endgroup\$
    – dzaima
    Jul 17, 2017 at 14:19
3
\$\begingroup\$

Mathematica, score: 0.0482378 0.0540228 0.0543788

542 607 611 bytes, all 106 recipes

-4 bytes for rearranging compressed data

-65 bytes for losing the ability to deal with invalid inputs

"4/a[:chwpb?_oqjB%',>!6aM7^s<S@1|dfi~g38y&.nz}25#0r;]`mVe{v)b\"l9(tZE"~StringTake~{#&@@Join@@StringPosition[Uncompress@"1:eJw9UVuOgzAMrHoS9yglsGnUJkAsLYj92wv0/n+dsU3HZoAwfnL7f3f9u14uEtiKFDrIXgHc9IsXaQWEjo/uQDHjFxU6gt0MfiKy7Psuq9CVKVycACcoXrIsEQ6oF3euJBwOw+CNAUwSSNZx9NManSnGmCLmelrRH+R38ebFxO5Tn6wNFGwqjUmtK85rwHOtFVElNsWoh5mV+dbyy2ohQZbcs5hlzXx21MoBlEtbgF8SgzbMqRJJnrGfnviqsZpU7OTcIbUlftzDIIOM8zjP83EcPjy5x29aJRaXUmzekClUk/rUIbsD+2kcspOonKjXhhYaeD0hPbr6AOvmjtk=",#<>""&/@{v=#/.""->" ",Sort@v,v/.(#|##2&@@Characters@"GLIDWCYR"->"Q")}]/9+8/9}&

Take input as 1D-list {"W", "W", "", "", ...}

Expanded & clear version:

TableOfNames~StringTake~{# & @@ 
      Join @@ StringPosition[
        Uncompress@CompressedRecipeTable,
        # <> "" & /@ {v = # /. "" -> " ", Sort@v, 
          v /. (# | ##2 & @@ Characters@"GLIDWCYR" -> "Q")}]/9 + 8/9} 
 &

The big Uncompress is a string table of all combinations, joined together.

"        WI II IIII     WWWW   SSSSSSSSSSLSSSSQQQQ QQ QWW WW WW    I \
I I QQQ S  S    W W W WWWWWW S      PXXX Q  Q  S QQ  S  S CCCC CCCC   \
PL PP    W WWWWS SSSSS SSSSSMSSSSWWWBBBWWW      QQ         C   WW WW  \
  NN NN WWWWDWWWW   I IIII K  S  F W  WW WWW    W  W    ERECCC  S SNS \
N   CCC   Q QQQQQQQ   MMMWWWII II II    H H H WWWW WWWW   I   I     S \
 C  G GRG G G GGSGGRG       MM QQ SQ S PPPPVPPPP   SWSSWS    I   \
KCCCC CCRC I  S  W WWWCICCRC Q  S  S    IIIIII   HHHHHH B DODOOOZZZ I \
III I IRI I QQQQ Q      CCCCCC        GI IISII I   Q QQ Q     \
AAAAXAXAXAXAXWWWWRWWWWISIIEIISISN S NSN QQQQQQQQQ R  S    "

StringPosition looks for matching in a order of: input itself, sorted input (for shapeless recipe), armour, weapon, pressure plate.

# & @@ Join @@ returns the position of first occurence.

/9+8/9 calculates the index in result table and StringTake takes the character at that position.

\$\endgroup\$
2
\$\begingroup\$

SOGL V0.12, 325 322 317 316 bytes / 106^2 = score 0.0281238875

3∫HA"-?%mΛe*▓╔Υι§ā⅜β¬kēōΠ»t ‰CV↓ZΟΚΨpΝ∫3υ≤↕‰č⅛c╚≤Æ⁷/←;[piυ¦╗⌠⁄⁸qη╔@O;┐√)cR█9ιZ׀l»░(■DΛQ-╥76PT─ō4ο3ū^╝/9»¾κλCβ׀+!'▼vw-№█⁷$▒d`Σ⅟Ιž⁴n≡,`!m≤Σ═╥∫Κ‽∆Q>+g=¼⁾⁽D┐?─D○≠6τ╝ZTΞķ⅜∑²V=]4Æ⁴℮lT$¡sψī1 ◄δ)⅞/Σ/Δō»ņe#≥ζz⅛yB÷B⅞⁵Kβn┘g⁵ķ»<§└≡↓θ○‼¼ņΔε⁄z‼Ζ∙Φ6β⅜c≈Νycm!=V○Jεκ~  :I)ΩS‘U9ndW:? ~Δ" $*+-=\x”Z+čøŗ"SEBMZV”+W←,a‽"9╚πw⁽νQσ_‘č┌ŗD}a?□D

Explanation:

3∫                    3 times repeat (on each it'll try something else)
  HA                    save 1-indexed iteration - 1 on variable A
    "..‘                push a string of the recipes
        U               uppercase it (as lowercase was ~7 bytes shorter)
         9n             split into and array of strings of length 9
           d            load the variable D (by default string input)
            W           get the variables 1-based index in that array, 0 if not found
:?                  ←   if [it isn't 0], leaves the input on the stack
   ~Δ                     get the ascii characters from space to ~ (inclusive)
     "..”                 push " $*+-=\x", the characters that are in the ASCII but not used
         Z+               add the uppercase alphabet to that (as most of it can't be outputted)
           čøŗ            filter those out
              "..”+       append to it the uppercase characters that are used - "SEBMZV"
                   W      get in the finished string the character at the index gotten before the if
                    ←     exit, outputting that
       ,                push the input
        a‽        }     if the 0-based index is not [0] (aka if this is the 1st time in the loop)
          "..‘            push "RDYWCDCIGL" - characters that are either of the groups of Q, J or the pressure plate
              č┌ŗ         replace [in the pushed input, each of those characters, with a dash]
                 D        save on variable D - used in the IF above
                   a?   if the 0-based index [is == 0] (aka if this is the 2st time in the loop, soon-to-be 3rd/last)
                     □    sort [the previously pushed input]
                      D   save on the variable D

Try it Here! or test by running the following (which will say what's wrong):

var arr = `      WW  !\n      CC  !\n      II  !\n      GG  !\n     AAAA "\n I  S  W  #\n   W WWWW %\nG GGSGGRG &\nS SSSSS S '\nISIIEIISI (\nI IISII I )\nSSSSMSSSS ,\n       MM .\nI II IIII /\nWWWCICCRC 0\n  S SNS N 1\n    I   K 2\n   I   I  3\n        W 4\nW         4\n    W     4\n      W   4\nCCCC CCRC 5\n        C 6\n     C    6\nC         6\nWWWWDWWWW 7\n    S  C  8\nWWWWRWWWW 9\nSSSSLSSSS :\n   IIIIII ;\nW  WW WWW <\nWWWBBBWWW >\nWWWWWW S  ?\n   ERECCC @\n   SSSSSS [\n   HHHHHH ]\n   I IIII ^\n     PXXX _\n XP    XX _\nX   XP  X _\nXXXP      _\n B DODOOO \`\n   G GG G b\nGGGG GG G c\nG GGGGGGG d\nGGGG G    e\n   I II I b\nIIII II I c\nI IIIIIII d\nIIII I    e\n   L LL L b\nLLLL LL L c\nL LLLLLLL d\nLLLL L    e\n   D DD D b\nDDDD DD D c\nD DDDDDDD d\nDDDD D    e\n   MMMWWW f\nWWWW WWWW g\nWW WW WW  h\nII II II  i\nCCCC CCCC j\n   WW WW  k\nXAXAXAXAX l\nZZZ I III m\n WW SW S  n\n W  W  S  o\nWWW S  S  p\nWW  S  S  q\n W  S  S  r\n CC SC S  n\n C  C  S  o\nCCC S  S  p\nCC  S  S  q\n C  S  S  r\n II SI S  n\n I  I  S  o\nIII S  S  p\nII  S  S  q\n I  S  S  r\n GG SG S  n\n G  G  S  o\nGGG S  S  p\nGG  S  S  q\n G  S  S  r\n DD SD S  n\n D  D  S  o\nDDD S  S  p\nDD  S  S  q\n D  S  S  r\n K  S  F  s\nSN S NSN  t\n   W W W  u\n        G v\n      G   v\n   G      v\nG         v\n   I I I  w\n G GRG G  y\nPPPPVPPPP z\n   CCCCCC {\n   CCC    |\n   SWSSWS }\n   H H H  ~\n    W  W  S\n R  S     E\n   PL PP  B\n   NN NN  M\nGGGGGGGGG Z\nIIIIIIIII Z\nLLLLLLLLL Z\nDDDDDDDDD Z\n I IRI I  V`.split("\n");
toLog = "";
arr.forEach(f=>{
  inputs.value = f.substring(0,9);
  runClicked();
  correct = f.charAt(10);
  got = output.value;
  if (got != correct)
    toLog+= "\""+ inputs.value +"\": expected \""+ correct +"\", got \""+ got +"\".";
})
console.log(toLog);
\$\endgroup\$
1
\$\begingroup\$

Jelly, score 0.00173611111

4/(48^2) = 0.00173611111 score

OṢḄỌ

Takes input as string with hyphens for empty space, e.g. -----WWWW.

I think this is valid because the challenge says

Output will be the item crafted from the input, in any form you want, as long as it is obvious what it means. (Personally, I would use my 1 letter abbreviations of the items)

The 1 letter abbreviations are about as confusing as what this outputs, so this output is obvious enough what it means.

This assumes the input can be one the program can handle as specified by

you can assume that the input passed will be one that it can handle

Try it online!

Test Suite (Header and footer prettify the output) This does all 48 recipes and gives their names.

How it Works

This is really just a really poor hash function.

OṢḄỌ - main link, input e.g. ZZZ-I-III
O      - character codes e.g. [90, 90, 90, 45, 73, 45, 73, 73, 73]
 Ṣ     - sort. This helps shapeless recipes and keeps recipes such as TNT in CJK 
           e.g. [45, 45, 73, 73, 73, 73, 90, 90, 90]
  Ḅ    - convert from binary to integer e.g. 26670
   Ọ   - chr: convert from integer to character e.g.栮
\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.