21
\$\begingroup\$

There are 21 items in Minecraft that you can craft using only wood and items crafted from wood:

axe
boat
bowl
button
chest
crafting table
door
fence
gate
hoe
ladder
pickaxe
planks
pressure plate
shovel
sign
slab
stairs
stick
sword
trapdoor

This list assumes that the 6 different types of wooden planks/slabs/doors/etc. all count as the same item. Another way to think of it is to assume you only have access to one type of wood.

Each of these 21 items has a different crafting recipe. We'll represent each of these recipes as a 2×2 or 3×3 grid of the characters .WPS. The . is an empty crafting slot, W is for wood, P is for wood planks, and S is for sticks. No other characters are needed for these particular items.

For example, this is the recipe for a chest:

PPP
P.P
PPP

Challenge

Write a program that takes in the name of one of our 21 items, exactly as it appears above, and prints a valid crafting recipe for that item.

Crafting recipes are translation invariant, so if the input is fence, both of these are valid:

PSP
PSP
...
...
PSP
PSP

If a recipe fits in a 2×2 grid, you can output it in a 2×2 or 3×3 grid. e.g. stick:

.P
.P
...
.P.
.P.

Recipes can also be mirrored horizontally (around a vertical line of symmetry), though this only makes a difference for the axe, hoe, and stairs. e.g. hoe:

.PP
.S.
.S.
PP.
.S.
.S.

So outputting any recipe that fits in the grid and has the correct shape (ignoring translation and mirroring) is what your program needs to do. These are all the recipes that the official game will recognize. (Note that recipes cannot be rotated or mirrored vertically.)

Details

  • Take input from stdin or the command line. You can assume the input is always valid. Requiring quotes around input (e.g. "chest") is fine.
  • Output to stdout (or closest alternative) with an optional trailing newline.
  • The shortest submission in bytes wins.

Example

Here's a list of all inputs and sample outputs:

axe
PP.
SP.
S..

boat
P.P
PPP
...

bowl
P.P
.P.
...

button
P.
..

chest
PPP
P.P
PPP

crafting table
PP
PP

door
PP.
PP.
PP.

fence
PSP
PSP
...

gate
SPS
SPS
...

hoe
PP.
S..
S..

ladder
S.S
SSS
S.S

pickaxe
PPP
.S.
.S.

planks
W.
..

pressure plate
PP
..

shovel
P..
S..
S..

sign
PPP
PPP
.S.

slab
PPP
...
...

stairs
P..
PP.
PPP

stick
P.
P.

sword
P..
P..
S..

trapdoor
PPP
PPP
...
\$\endgroup\$
5
  • 1
    \$\begingroup\$ Given your output samples, I don't see any exception. All can be mirrored, but most are symmetric. \$\endgroup\$
    – edc65
    Mar 21, 2015 at 11:58
  • \$\begingroup\$ Since "in bytes" links to the UTF-8 counter, does that mean we have to count bytes in UTF-8? \$\endgroup\$ Mar 21, 2015 at 13:03
  • \$\begingroup\$ Full programs only? No functions? \$\endgroup\$
    – Alex A.
    Mar 23, 2015 at 3:50
  • \$\begingroup\$ @Alex A. Right. \$\endgroup\$ Mar 23, 2015 at 5:00
  • \$\begingroup\$ @MartinBüttner Make that no. (You know why, though it probably doesn't matter anymore. :P ) \$\endgroup\$ Mar 25, 2015 at 6:52

3 Answers 3

15
\$\begingroup\$

CJam, 100 96 94 91 bytes

"+WcbKF AQH;_{GM8Lyf=_tmTn"141bDb3/l72b970%={Z"O>HVa=4a"98bZb+"P.SW"f=3/=}%N*

StackExchange mangles unprintables, so rather than copying and pasting here is the permalink. Additionally, here's the test program.

(Thanks to @Optimizer for telling me about f and @MartinBüttner for telling me about CJam's modulo indexing.)

Cygwin's hexdump:

0000000 0122 2b1e 571a 6308 621f 4b05 2046 5141
0000010 1608 1848 5f3b 1115 1d7b 4d47 4c38 6679
0000020 5f3d 6d74 0c54 226e 3431 6231 6244 2f33
0000030 376c 6232 3739 2530 7b3d 225a 1b4f 483e
0000040 6156 343d 2261 3839 5a62 2b62 5022 532e
0000050 2257 3d66 2f33 7d3d 4e25 002a          
000005b

Explanation

To construct the recipes, we use 13 different rows (also listed are explanations why this is minimal):

0    W..    (required by planks)
1    ...    (required by many)
2    .PP    (required by many)
3    PPP    (required by chest)
4    .P.    (required by bowl)
5    ..P    (required by stairs)
6    S.S    (required by ladder)
7    SSS    (required by ladder)
8    .S.    (required by sign)
9    .SP    (required by axe)
10   P.P    (required by bowl)
11   PSP    (required by fence)
12   SPS    (required by gate)

We encode the rows via Z"O>HVa=4a"98bZb+"P.SW"f=3/, which gives

["W.." "..." ".PP" "PPP" ".P." "..P" "S.S" "SSS" ".S." ".SP" "P.P" "PSP" "SPS"]

The first bit "+WcbKF AQH;_{GM8Lyf=_tmTn"141bDb3/ encodes the recipes, giving

[[3 3 8] [1 0 0] [1 0 0] [3 8 8] [1 3 3] [2 8 8] [6 7 6] [1 0 0] [1 10 3] [1 1 2] [2 2 2] [1 11 11] [1 1 4] [1 0 1] [1 10 4] [2 9 8] [3 10 3] [1 2 2] [4 8 8] [1 4 4] [1 1 3] [1 12 12] [4 4 8] [5 2 3]]

Note how the first entry is [3 3 8], which is the recipe for sign.

l72b970%= reads in the input, then applies some magic to work out which recipe to take from the list. Even though we only have 21 recipes there are 24 in the list — the extra few spots correspond to the [1 0 0]s.

After reading the input, picking the recipe and converting the recipe to rows, we put in some newlines N* and auto-print.


CJam, 89 86 83 bytes

'W"9=!%)S@*m:@DQzk?V$%qyR55AmVEpPNW}"132b3b"P.S"f=+3/3/l57b5342%24%(=N*

... it turns out that hardcoding all the outputs does a bit better in CJam. I'm pretty disappointed.

Once again we have some unprintables so here's the permalink and test program.

Cygwin's hexdump:

0000000 5727 0222 3d39 1021 2925 531e 2a40 6d0b
0000010 403a 1844 7a51 3f6b 2456 7125 5279 3506
0000020 4135 046d 1a56 7045 1550 164e 1057 7d01
0000030 3122 3233 3362 2262 2e50 2253 3d66 332b
0000040 332f 6c2f 3735 3562 3433 2532 3432 2825
0000050 4e3d 002a                              
0000053

Explanation

The outputs are encoded using base 3, with the lone W being tacked onto the front before the string is split into 3s to give rows, and the rows are split into groups of 3s to give recipes.

Like above, base converting and modulo magic is used to select the recipe. There are 22 recipes (one unused) but we need to take modulo 24, so we actually need to explicitly specify 24% this time rather than relying on modulo indexing.

\$\endgroup\$
2
  • \$\begingroup\$ Can you post a hexdump with xxd? The permalinks doesn't work on Firefox. \$\endgroup\$ Mar 23, 2015 at 11:26
  • \$\begingroup\$ @n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳ Unfortunately I'm on Windows, so I've put up the next best thing I could manage for now. \$\endgroup\$
    – Sp3000
    Mar 24, 2015 at 13:04
6
\$\begingroup\$

JavaScript (ES6), 235 241 262

Edit Abusing even more of the rule that input is always valid: there is just 1 item that requires W, and that can be special cased. So the output grid is encoded as 9 digit base 3 numbers.

235 bytes with I/O via popup.

w=prompt();
"926a722boa182bo2b19520ch224c6056d644f448g764h7651l2294pi8pr758sh2915si26sl19172sta56st785s728t"
.replace(/(\d+)(\D+)/g,(c,p,m)=>w.search(m+z)||[for(c of'11\n11\n111')z+='.SP'[~~p%3]+(p/=3,-c?'':c)],z='');
alert(&&z||'W.\n..')

221 bytes as a testable function.

F=w=>"926a722boa182bo2b19520ch224c6056d644f448g764h7651l2294pi8pr758sh2915si26sl19172sta56st785s728t"
.replace(/(\d+)(\D+)/g,(c,p,m)=>w.search(m+z)||[for(c of'11\n11\n111')z+='.SP'[~~p%3]+(p/=3,-c?'':c)],z='')&&z||'W.\n..'

Output always as 3x3 grid. With 4 available output symobols, the grid is encoded as a 3x3x2 (18) bit number. And as the input have to be always valid, the string are stored truncated to the bare minimum.

Test In Firefox/FireBug console

;["axe", "boat", "bowl", "button", "chest", "crafting table", "door", 
 "fence", "gate", "hoe", "ladder", "pickaxe", "planks", "pressure plate", 
 "shovel", "sign", "slab", "stairs", "stick", "sword", "trapdoor"]
.forEach(v=>console.log(v+'\n'+F(v)))

Output

axe
PP.
SP.
S..

boat
P.P
PPP
...

bowl
P.P
.P.
...

button
P..
...
...

chest
PPP
P.P
PPP

crafting table
PP.
PP.
...

door
PP.
PP.
PP.

fence
PSP
PSP
...

gate
SPS
SPS
...

hoe
PP.
S..
S..

ladder
S.S
SSS
S.S

pickaxe
PPP
.S.
.S.

planks
W.
..

pressure plate
PP.
...
...

shovel
P..
S..
S..

sign
PPP
PPP
.S.

slab
PPP
...
...

stairs
P..
PP.
PPP

stick
P..
P..
...

sword
P..
P..
S..

trapdoor
PPP
PPP
...
\$\endgroup\$
2
\$\begingroup\$

Python, 305 bytes

n=2**(23-hash(raw_input())/535366%24);print "W..\n...\n..." if n==1024 else "\n".join(["".join(['P' if [16706041,9740732,7635081,7399784,5267501,7372808,57344,57344,49152][j+i*3]&n==n else 'S' if [6,2097152,6,131078,10748162,6,131138,9699584,2][j+i*3]&n==n else '.' for j in range(3)]) for i in range(3)])

Explanation

# Compute a unique number for each item.
# 535366 and 24 are precalculated values that were bruteforced.
n = 23 - hash(raw_input()) / 535366 % 24

# Use precalculated tables that represent which ingredient in this recipe of
# an item. The nth bit of p[0] will be set if the first ingredient of the item
# associated with the unique number n is some planks. It works the same for s.
p = [16706041,9740732,7635081,7399784,5267501,7372808,57344,57344,49152]
s = [6,2097152,6,131078,10748162,6,131138,9699584,2]

# Handle planks differently, as it is the only one using wood.
if n == 10:
    print "W..\n...\n..."
else:
    for i in xrange(3):
        line = ""
        for j in xrange(3):
            # Now we can check if the ingredient is some planks...
            if p[j + i * 3] & 1 << n == 1 << n:
                line += 'P'
            # ...or if it is some sticks...
            elif s[j + i * 3] & 1 << n == 1 << n:
                line += 'S'
            # ...or if it is simply empty.
            else:
                line += '.'
        print line

Comment

This code surely isn't the tiniest but it works just fine. I'm satisfied. :)

Python, 282 bytes

n=hash(raw_input())/808789215%21;print "\n".join(["P.PPPP...P..S..S..PP.......PP.PP....P........SPSSPS...PPPPPP.S.PP.PS..S.W........PPPPPP...PP..S..S.P..PP.PPPS.SSSSS.SPPP.S..S.PPP......P..P..S..PPPP.PPPPPSPPSP...PP.PP.PP.P..P.....P.P.P...."[9*n+i*3:9*n+(i+1)*3] for i in range(3)])

Using the same technique to generate an unique identifier but directly looking up the recipe in an array. It is a lot simpler and a little more compact than my first code.

\$\endgroup\$
1
  • \$\begingroup\$ Having a special case for 'planks' you can shorten the string to W.\n.. \$\endgroup\$
    – edc65
    Mar 22, 2015 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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