| Bytes | Lang | Time | Link |
|---|---|---|---|
| 036 | Uiua 0.11.0 | 240529T174541Z | RomanPro |
| 014 | Stax | 210412T170318Z | Razetime |
| 025 | RProgN | 170131T042623Z | ATaco |
| 176 | Python | 151228T234558Z | TanMath |
| 141 | JavaScript | 170130T190620Z | Alvaro A |
| 5278 | Unicorn ES6 | 151229T024505Z | ETHprodu |
| 200 | Racket | 170130T172415Z | Rodrigo |
| 125 | SmileBASIC | 170128T174424Z | 12Me21 |
| 067 | Perl 6 | 170128T163029Z | smls |
| 086 | Python 3 | 151229T151616Z | Sherlock |
| 3379 | Unicorn ES6 Invalid | 160102T185750Z | user4616 |
| 075 | Ruby | 151229T161016Z | MegaTom |
| 083 | PHP | 151228T230002Z | insertus |
| 426 | Rust | 151229T142533Z | Kroltan |
| 078 | Ruby | 151229T135315Z | BDA |
| 118 | Mathematica | 151229T015944Z | LegionMa |
| 017 | Pyth | 151228T222457Z | Maltysen |
Uiua 0.11.0, 39 36 bytes
(transpiling part - 28 25 bytes)
- 3 bytes thanks to noodle man
Works only with string literals in the code, doesn't work with strings evaluated at runtime.
U! β^ +@\0Γβ:" π¦π"βββ’β§»-@ .β2βββ’
U!$ <your Unicorn code here>
RProgN, 25 bytes
~`π'π¦π¦'R'(π¦+)%s*'{Lc}R do
Explained
~ # Zero Space Segment
`π # The String literal "π"
'π¦π¦' # The String literal "π¦π¦"
R # Replace, turning all goats into twonicorns
'(π¦+)%s*' # The pattern string "(π¦+)%s*", which is "A fewnicorns, then as much whitespace as possible, or none.
{Lc}R # Replace with the result of the function, which converts the captured subgroup to it's Length, then to a char.
do # Straight up do it.
Once again, RProgN falls into the trap of being consistently okay.
Python 179 176 bytes
EDIT: I just learnt s.split(' ')=s.split()
Here is the second "actual" programming language Unicorn interpreter. I call this version of Unicorn "UnicornPy" pronounced as "unicorn pie". I am making this much too official!
s=raw_input()
s=s.replace('π','π¦π¦')
s=s.replace('π¦','u')
for i in s:
if i not in "ug ":
s=s.replace(i,'')
s=s.split()
for i in s:
s[s.index(i)]=chr(len(i))
exec(''.join(s))
For some reason, it needs me to convert the unicorn and goat emojis to u and g. I do not know why.
JavaScript, 140 154 141 bytes
var s='';i.split(" ").forEach((e)=>{s+=String.fromCharCode((e.split("π¦").length-1)+2*(e.split("π").length-1));});console.log(eval(s));
It splits the input string in an array of strings, using space as a needle. It then proceeds to count the amount of unicorns and goats*2 and concatenate the summed result to what will be evaluated.
More readable:
var s = '';
i.split(" ").forEach((e) => {
s+=String.fromCharCode((e.split("π¦").length-1)+2*(e.split("π").length-1));
});
console.log(eval(s));
Edit:
Updated code to accept an argument from CLI, use:
node unicorn.js "ππ¦ π¦π¦"
var s='';process.argv[2].split(' ').forEach((e)=>{s+=String.fromCharCode((e.split('π¦').length-1)+2*(e.split('π').length-1));});console.log(eval(s));
Ungolfed:
var s = '';
process.argv[2].split(' ').forEach((e) => {
s+=String.fromCharCode((e.split('π¦').length-1)+2*(e.split('π').length-1));
});
console.log(eval(s));
Edit 2:
Edited to accept input as a function parameter, use node unicorn.js
i=>{var s='';i.split(' ').forEach((e)=>{s+=String.fromCharCode((e.split('π¦').length-1)+2*(e.split('π').length-1));});return eval(s);};
Ungolfed:
i =>{
var s = '';
i.split(' ').forEach((e) => {
s+=String.fromCharCode((e.split('π¦').length-1)+2*(e.split('π').length-1));
});
return eval(s);
};
Unicorn (ES6), 5934 5278 bytes
Under the custom encoding, this is 5278 bytes (1 byte per char); but with UTF-8, it would 4 bytes per char (though only 1 for a space), or 20869 total.
(too many Unicorns and Goats to reasonably display here)
Instead, here's a pastebin. This Unicorn code transpiles to this JS snippet:
s=>eval(s.replace(/\S+ ?/g,c=>String.fromCharCode(c.length>>1<<c.charCodeAt()%2)))
Now, this isn't the shortest possible version; this is shorter:
s=>eval(s.replace(/\S+ ?/g,c=>String.fromCharCode(c.length>>1<<("π¦">c))))
However, the one unicorn in there would transpile to 56034 goats, thus multiplying the score by roughly 11.
Here's the function I used to transpile to Unicorn:
function g(s){return s.replace(/./g,function(c){i=c.charCodeAt();return(i%2?"π¦".repeat(i):"π".repeat(i/2))+" "}).slice(0,-1)}
<textarea id=O cols=100></textarea><button id=P onclick="Q.value=g(O.value);R.innerHTML=(Q.value.length+Q.value.split(' ').length-1)/2">Run</button><br><textarea id=Q rows=10 cols=100>Output appears here.</textarea><br><p>Length: <span id=R>0</span><br></p>
Note: I haven't actually tested the program, as there isn't an online interpreter that I could find (although I suppose I could hook up the .js file to HTML...?)
Racket, 200 bytes
(define(f s)(eval(read(open-input-string(list->string(map integer->char(map(Ξ»(x)(foldl(Ξ»(y a)(case y[(#\u)(add1 a)][(#\g)(+ a 2)]))0 x))(map string->list(string-split s)))))))(make-base-namespace)))
Ungolfed:
(define(f s)
(eval(read(open-input-string
(list->string ;;back to string
(map integer->char ;;back to char
(map(Ξ»(x)
(foldl(Ξ»(y a)
(case y
[(#\u)(add1 a)]
[(#\g)(+ a 2)]))
0 x))
(map string->list (string-split s))))))) ;;splitting by space, converting to list of chars
(make-base-namespace)))
It starts by splitting the string into list of strings by spaces, then creates a list of chars per splitted string. Then each list of chars is reduced into a number by adding 1 if unicorn, 2 if goat. Finally, each list containing the sum is made a list of chars, then a string which is passed to eval.
SmileBASIC, 125 bytes
INPUT S$WHILE LEN(S$)R=ASC(POP(S$))IF R-32THEN C=C+!(R-117)+2*!(R-103)ELSE O$=CHR$(C)+O$C=0
WEND
SAVE"TXT:@",CHR$(C)+O$EXEC"@
Using PRGEDIT to execute the code without saving would have been a lot nicer, but also a lot longer.
code:
INPUT CODE$ 'making the user type 1000s of characters just so I could save 2 or 3
WHILE LEN(CODE$)
CHAR$=POP(CODE$) 'I convert the code backwards, since POP( is shorter than SHIFT(
IF CHAR$==" " THEN
UNSHIFT OUT$,CHR$(CHAR)
CHAR=0
ELSE
INC CHAR,(CHAR$=="u")+(CHAR$=="g")*2
ELSE
WEND
SAVE "TXT:@",CHR$(CHAR)+OUT$
EXEC "TXT:@"
Perl 6, 67 bytes
{use MONKEY-SEE-NO-EVAL;EVAL [~] .wordsΒ».&{chr m:g/\π¦/+2*m:g/\π/}}
Python 3, 94 86 bytes
This is a simple parser that works even if you mix u and g in one section.
s=input().split();exec(''.join(chr(sum([[0,2][j<"u"],1][j>"g"]for j in i))for i in s))
As an example (using u and g in separate sections):
gggggggggggggggggggggggggggggggggggggggggggggggggggggggg ggggggggggggggggggggggggggggggggggggggggggggggggggggggggg uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu ggggggggggggggggggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggg uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
should parse to
print(1)
Unicorn ES6 (Invalid), 3379 bytes
This is invalid because it uses the latest version of Unicorn with rainbows, sun with clouds, and sparkles. Thanks to @ETHproductions for the JS code to interpret unicorn.
code is in the paste bin below
Pastebin: http://pastebin.com/raw/Q9Kd4ixA
This is only 3379 bytes if sparkles, sun/clouds, and rainbows also are 1 byte.
Ruby, 75 characters
A nifty ruby interpreter that replaces all π¦ with ' ' (a space) and all π with ' ' (two spaces), and gets the length of each segment.
I call this version of Unicorn RubyUnicorn Rubycorn.
->s{eval s.split(a=' ').map{|r|r.gsub('π¦',a).gsub('π',a*2).size.chr}.join}
PHP, 83 80 86 87 bytes
Now Unicorn-ready
For the cost of 3 bytes I made this unicorn ready:
$a=mb_substr_count;foreach(explode(" ",$argv[1])as$b)echo chr($a($b,π¦)+2*$a($b,π));
Takes an input from command line, like:
$ unciorns.php "π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦π¦"
This will output 32.
Demo
Unfortunately OS X 10.10.5 doesn't support is hiding Unicorns. Here's the alternative ug-approach (80 bytes):
$s=substr_count;foreach(explode(' ',$argv[1])as$c)echo chr($s($c,u)+2*$s($c,g));
Takes an argument from command line, like:
$ php unicorns.php "uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuug uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu"
Edits
- Saved 1 byte due to massive refactoring. This version is already discarded again, since I managed to golf the original even further:
Demo discarded version (86 bytes)
for($_=$argv[1].' ';$c=$_[$i++];)$t+=u==$c?1:(g==$c?2:(' '==$c?-$t+!print chr($t):0));
- Saved 6 bytes by replacing
forwithforeach - Added 3 bytes making it Unicorn-ready.
Rust, 426 bytes
use std::io::Write;macro_rules!c{($n:expr,$a:expr)=>(println!("{}",std::str::from_utf8(&std::process::Command::new($n).arg($a).output().unwrap().stdout).unwrap());)}fn main(){let d:String=std::env::args().skip(1).next().unwrap().split(' ').map(|s|s.chars().fold(0u8,|a,c|a+match c as char{'π¦'=>1,'π'=>2,_=>0}) as char).collect();std::fs::File::create("o").unwrap().write_all(d.as_bytes()).unwrap();c!("rustc","o");c!("o","");}
This probably can be golfed the hell down, but type safety and checked errors are quite verbose.
Since Rust is a compiled language, this program outputs the decoded program to a file and invokes the compiler on said file, then executes the resulting binary.
Ungolfed:
use std::io::Write;
macro_rules! command {
($name:expr,$argument:expr) => (println!("{}", std::str::from_utf8(
std::process::Command::new($name)
.arg($argument)
.output()
.unwrap()
));)
}
fn main() {
let decoded: String = std::env::args()
.skip(1) //ignore program name
.next().unwrap().split(' ') //get first arg split on spaces
//transform every section in a char
.map(|section| section.chars()
.fold(0u8, |accumulator, chr| accumulator + match chr as char {
'π¦' => 1,
'π' => 2,
_ => 0
}) as char)
//convert iterator to string
.collect();
std::fs::File::create("o").unwrap()
.write_all(decoded.as_bytes()).unwrap();
command!("rustc", "o");
command!("o", "");
}
Ruby, 78 Bytes
eval ARGV[0].gsub(/[^ug ]/,'').split.map{|b|(b.size*(b[0]=='u'?1:2)).chr}.join
It basically reads the first command line argument, splits it at every space character, maps the size of the block to the appropriate character and joins it all together.
Edit: Forgot the requirement that all other characters should be ignored
Mathematica, 118 bytes
a=StringCount;ToExpression@FromCharacterCode[If[#~StringTake~1=="u",#~a~"u",2IntegerLength[#~a~"g"]]&/@StringSplit@#]&
Performs exactly as described in the specification. I couldn't use emoji in Mathematica string literals without the interpreter exploding, so I used u and g.
Pyth - 23 17 bytes
.vsmC+/d\π¦y/d\πcz
It works by splitting the input by spaces, then for each section counting the number of unicorns and number of goats * 2 then adding them, then taking the char at the code point. It finished by summing the char array and pyth-evaling.