Do you want to code a snowman?
JavaScript ES6, 210 208 202 bytes
s=>` 0
8(213)9
4(6)5
(7)`.replace(/\d/g,p=>`_===_1 ___
.....1 _
/_\\1 ___
(_*_)1,1.1_11.1o101-1.1o101-1<11/11>11\\11 : 1] [1> <1 1 : 1" "1___1 11\\11 11/11 `.split(1)[s[p>7?p-4:p]-1+p*4]||' ')
This is an anonymous function; you use it by executing ([function code])('42232124')
. The most aggravating part of this was the arms, which take up 2 lines, so I had to include code for both top and bottom.
The Stack Snippet below has ungolfed, un-ES6-ified, commented code. And you can use it to easily test the code and try out different combinations. Edit: I'm having way too much fun with this. I've added several new features, including a way to generate a random snowman.
Thanks to Yair Rand for saving six bytes.
var f=function(s){
return' 0\n8(213)9\n4(6)5\n (7)' // Start with a placeholder string with all the static components
.replace(/\d/g,function(p){ // Go through each placeholder number to replace it with its value
// The massive string below holds all the possible body parts, separated by 1 for easy splitting.
// The two at the end are for the top of the arms
return'_===_1 ___\n .....1 _\n /_\\1 ___\n (_*_)1,1.1_11.1o101-1.1o101\
-1<11/11>11\\11 : 1] [1> <1 1 : 1" "1___1 11\\11 11/11 '.split(1)
[s[p>7?p-4:p]-1 // Get the value from the input string. If the current body part
// is the top of the two-line arms (8 or 9), drop it down to 4 or 5
// Subtract 1 to account for the 0-indexed array.
+p*4] // multiply by 4 to skip to the relevant code
||' ' // To save bytes in the above string, spaces are empty strings, so replace them here
})
}
// Code for the interactive version follows
// http://codepen.io/hsl/pen/bdEgej
function updateRadios(){$('input[type="radio"]').each(function(){if($(this).is(":checked")){var t=$(this).data("p"),i=$(this).data("v");input[t]=i}}),inputS=input.join(""),update()}var input=[],inputS=$("#code").val(),update=function(){$("#p").text(f(inputS)),$("#code").val(inputS)};$('input[type="radio"]').change(updateRadios),$("#code").keyup(function(){inputS=$(this).val(),update()}),updateRadios(),$("#random").click(function(){for(var t=0;8>t;t++)$("div:eq("+t+") input:eq("+Math.floor(4*Math.random())+")").prop("checked",!0);updateRadios()});
body{font-family:sans-serif}h2{font-size:18px;font-weight:400}label{display:block}div{display:inline-block;margin:0 10px}#code{width:70px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><div><h2>Hat</h2><label><input type="radio" name="p1" data-p="1" data-v="1"> Straw hat</label><label><input type="radio" name="p1" data-p="1" data-v="2"> Mexican hat</label><label><input type="radio" name="p1" data-p="1" data-v="3"> Fez</label><label><input type="radio" name="p1" data-p="1" data-v="4" checked> Russian hat</label></div><div><h2>Nose/mouth</h2><label><input type="radio" name="p2" data-p="2" data-v="1"> Normal</label><label><input type="radio" name="p2" data-p="2" data-v="2" checked> Dot</label><label><input type="radio" name="p2" data-p="2" data-v="3"> Line</label><label><input type="radio" name="p2" data-p="2" data-v="4"> None</label></div><div><h2>Left eye</h2><label><input type="radio" name="p3" data-p="3" data-v="1"> Dot</label><label><input type="radio" name="p3" data-p="3" data-v="2" checked> Bigger dot</label><label><input type="radio" name="p3" data-p="3" data-v="3"> Biggest dot</label><label><input type="radio" name="p3" data-p="3" data-v="4"> Closed</label></div><div><h2>Right eye</h2><label><input type="radio" name="p4" data-p="4" data-v="1"> Dot</label><label><input type="radio" name="p4" data-p="4" data-v="2"> Bigger dot</label><label><input type="radio" name="p4" data-p="4" data-v="3" checked> Biggest dot</label><label><input type="radio" name="p4" data-p="4" data-v="4"> Closed</label></div><div><h2>Left arm</h2><label><input type="radio" name="p5" data-p="5" data-v="1"> Normal</label><label><input type="radio" name="p5" data-p="5" data-v="2" checked> Upwards</label><label><input type="radio" name="p5" data-p="5" data-v="3"> Downwards</label><label><input type="radio" name="p5" data-p="5" data-v="4"> None</label></div><div><h2>Right arm</h2><label><input type="radio" name="p6" data-p="6" data-v="1" checked> Normal</label><label><input type="radio" name="p6" data-p="6" data-v="2"> Upwards</label><label><input type="radio" name="p6" data-p="6" data-v="3"> Downwards</label><label><input type="radio" name="p6" data-p="6" data-v="4"> None</label></div><div><h2>Torso</h2><label><input type="radio" name="p7" data-p="7" data-v="1"> Buttons</label><label><input type="radio" name="p7" data-p="7" data-v="2" checked> Vest</label><label><input type="radio" name="p7" data-p="7" data-v="3"> Inward arms</label><label><input type="radio" name="p7" data-p="7" data-v="4"> None</label></div><div><h2>Base</h2><label><input type="radio" name="p8" data-p="8" data-v="1"> Buttons</label><label><input type="radio" name="p8" data-p="8" data-v="2"> Feet</label><label><input type="radio" name="p8" data-p="8" data-v="3"> Flat</label><label><input type="radio" name="p8" data-p="8" data-v="4" checked> None</label></div><br><button id="random">Randomize</button><pre id="p"></pre><input type="text" id="code">
CJam, 135 134 132 130 126 125 bytes
0000000: 4e22285b200a5c225f2a295c2d2e2f6f2c3e4f3a3c3d5d225f N"([ .\"_*)\-./o,>O:<=]"_
0000019: 2422dd7382d6bfab28707190992f240c362ee510262bd07a77 $".s....(pq../$.6...&+.zw
0000032: 08556de9dcdb566c676817c2b87f5ecb8bab145dc2f2f76e07 .Um...Vlgh....^....]...n.
000004b: 22323536624b623224663d4e2f7b5f2c342f2f7d25723a7e2e "256bKb2$f=N/{_,4//}%r:~.
0000064: 3d2828342423346222205f0a20222e2a6f6f736572372f4e2a =((4$#4b" _. ".*ooser7/N*
To create the file on your machine, execute xxd -r > snowman.cjam
, paste the reversible hexdump from above, press Enter and finally Ctrl + D.
Alternatively, you can try the code online using the CJam interpreter.
Bonus
My favorite snowman is Olaf:
$ LANG=en_US cjam snowman.cjam <<< 12222212
_===_
\(o.o)/
( : )
(" ")
Winter's a good time to stay in and cuddle, but put me in summer and I'll be a… happy snowman!
Idea
The hex string
dd7382d6bfab28707190992f240c362ee510262bd07a7708
556de9dcdb566c676817c2b87f5ecb8bab145dc2f2f76e07
encodes the possible choices for all parts of the snowman, including the fixed ones. Let's call this string P.
To decode it, we convert P (here treated as an array of integers) from base 256 to base 20 and replace each of the resulting integers by the corresponding character of the string M:
([
"_*)\-./o,>O:<=]
This results in the string T:
/(_*_)"_===_/....., /_\
,._
-.oO
-.oO
<\ /
>/ \
: ] [> <
: " "___
((()
The first line encodes all hat choices, the last all fixed body parts. The other lines contain the 28 variable body parts.
We split T at linefeeds and divide the strings of the resulting array into four parts of equal length. Then, we read the input from STDIN, push the array of its digits in base 10 and select the corresponding elements of the split strings. We take advantage of the fact that arrays wrap around in CJam, so the element at index 4 of an array of length 4 is actually the first element. The last divided string does not correspond to any input, so it will get selected entirely.
We handle the hat by shifting the first element out of the resulting array. The index in M of first character, read as a base 4 number, reveals the number of spaces and underscores in the first line of the hat. We print those characters, a linefeed, a space and the remainder of the shifted string. Then, we push an additional linefeed on the bottom of the stack.
For the body parts, we concatenate the string corresponding to all of them. Let's call this string S. To assemble the body parts, we perform transliteration: we take each character of the string M, compute its index in sort(M) and replace it by the corresponding character of S. We take advantage of the fact that the transliteration operator automatically pads S to match the length of sort(M) by repeating the last character of S as many times as necessary.
Finally, we divide the resulting string into substrings of length 7 and place a linefeed between each pair of substrings.
Code
Suppose that the variables M
and P
contain the strings M and P.
N e# Push a linefeed.
M_$ e# Push M and a sorted copy.
P256bKb e# Push P and convert it from base 256 to base 20.
2$ e# Push a copy of M.
f= e# Compute T by retrieving the proper chars from M.
N/ e# Split T at linefeeds.
{_,4//}% e# Divide each string into four substrings of equal length.
r:~ e# Read a number from STDIN and push the array of its digits in base 10.
.= e# Get the corresponding chunks from T.
(( e# Shift out the first string and that string's first character.
4$# e# Find its index in M.
4b e# Compute its digits in base 4.
" _
".* e# Repeat the space and underscore that many times in place.
oo e# Print the result and the shifted string.
s e# Flatten the remainder of the array. This pushes S.
er e# Perform transliteration.
7/ e# Split into chunks of length 7.
N* e# Join using linefeeds.
CJam, 164 bytes
Generates the snowman left-to-right, top-to-bottom. This eliminates the need for any kind of string joining or repositioning operations, as I just leave every piece of the snowman on the stack. And then, due to the automatic stack dump at the end of programs:
♫ CJam wants to build a snowman! ♫
q:Q;SS"
_===_,___
....., _
/_\,___
(_*_)"',/0{Q=~(=}:G~N" \ "4G'(".oO-"_2G",._ "1G@3G')" / "5GN"< / "4G'(" : ] [> < "3/6G')"> \ "5GNS'(" : \" \"___ "3/7G')
Try it online.
Bonus
Thinking outside the box! 32443333
gives a snow(wo)man bride. You've gotta try a bit to see it, but there are the inward arms, fez + downwards arms = veil, and the head is actually in the fez/veil. The generally large form is the billowy dress, and the "eyes" and "nose" are folds in the dress.
_
/_\
(-.-)
/(> <)\
(___)
Other "eye" choices are a bit risqué...