Source code ecological footprint

102

4

You've just been hired by a German car manufacturing company. Your first task, as an engineer, is to write a program that computes the ecological footprint of ASCII strings.

The ecological footprint of character is computed as follows:

Write the character's ASCII code in binary, and count the number of 1's.

For example, A has a footprint of 2, but O is dirtier with a footprint of 5.

The global footprint of a string is the sum of the footprints of its characters. An empty string has a footprint of zero.

Your program must accept an ASCII string as parameter (through command line or input), compute its ecological footprint, and output it. The program itself must be ASCII encoded.

There is a hiccup though. As your company wishes to enter a new market with stricter environmental rules, you need to tune your program so that it behaves differently in "test mode". Thus:

The program should output 0 when it receives the string test as parameter.

Scoring

The source code with the smallest ecological footprint wins (and yes, the answer test is forbidden!)

Arnaud

Posted 2015-09-25T11:08:49.607

Reputation: 8 231

When you said 'parameter', did you mean command line input or function argument or stdin? – Blue – 2015-09-25T11:18:45.033

36I'm sorry, I haven't been keeping up with the news, but I've just read it. Can we assume the German car company is definitely NOT called Volkswagen? – Level River St – 2015-09-25T11:18:47.600

@muddyfish either command line or stdin, up to you – Arnaud – 2015-09-25T11:20:45.350

Could you give some testcases? – Caridorc – 2015-09-25T11:51:05.860

7For reference, most costly to less costly characters: \x7F}~_?{ow7yvu/s\x1F;=znm>k|OW[]^gc\x1Ex\x1D\eef\\'ZY+-VU.St\x173iNM5K6r\x0FG9:q<ljQ\x15\x13pC\aEF8IJL4\x0E21\x16RTh,X*)\x19\v&%\x1A#d\x1C\rab\!"$(\x180\x05A\x14B\x12\x11DHP\x03\f\x06\n\t\x80\x10\x01@\x04\b\x02 \x00` – Caridorc – 2015-09-25T11:59:59.957

19@steveverrill It is a fictional company, but its name indeed starts with V and has a W somewhere in the middle. But any similarities with reality is mere coincidence, least someone sues us. – Mindwin – 2015-09-25T12:40:16.650

@alexanderbird: see last sentence of the question - scoring is based on ecological footprint, not length – Jacob Krall – 2015-09-25T18:14:55.973

@JacobKrall - thanks, and oops. – alexanderbird – 2015-09-25T18:16:02.410

"Ecological Footprint" – PyRulez – 2015-09-25T19:56:31.057

1Functions allowed (instead of programs)? – Luis Mendo – 2015-09-25T20:58:11.730

1Hoping to see an x86 answer with POPCNT! – wchargin – 2015-09-26T06:19:10.797

@Mindwin How about this for the company name: http://www.collinsdictionary.com/dictionary/german-english/verwackeln

– Level River St – 2015-09-26T20:52:12.980

I will not embarass the company in question so I'll only tell you the initials: ~IBM~VW – user45891 – 2015-09-27T10:32:48.713

12They're lying to you! 1's are much more ecologically friendly than 0's. Want proof? Print out your source code in binary. 0's use nearly twice as much ink as 1's. And if you code with a dark background, they also waste more electricity to display on your screen. (If you code with a white background, you're already wasting electrons rendering all that white, so clearly any environmentally conscious programmer should be using a black background in their editor.) To be extra eco-friendly, we should all be writing in Whitespace... – Darrel Hoffman – 2015-09-27T18:03:56.090

1I actually have a concept for a language where the program would just be one character, but unfortunately I haven't made an interpreter yet. – DanTheMan – 2015-09-28T04:13:51.880

@DarrelHoffman that doesn't make sense with constantly backlit LCDs. – Ruslan – 2015-09-28T06:11:43.810

Answers

45

CJam, 33 31

"",AA#b:c~

There are 11300000000950000000034000000011600000001010000000115000000011600000000340000000061000000003300000000420000000058000000010500000000500000000102000000009800000000490000000102000000009800000000490000000098 null bytes between the double quotes.

The code is equivalent to

11300000000950000000034000000011600000001010000000115000000011600000000340000000061000000003300000000420000000058000000010500000000500000000102000000009800000000490000000102000000009800000000490000000098
AA#b:c~

which can be tested online.

How this works

"",  e# Push the length of the string.
AA#  e# Push 10000000000.
b    e# Turn the length into the array of its base-10000000000 digits.
:c   e# Cast each digit to character. This pushes the following:
     e# q_"test"=!*:i2fb1fb1b
~    e# Evaluate the string.

How that works

q_     e# Read all input and push a copy.
"test" e# Push the string "test".
=!*    e# Check for inequality and repeat the string 0 or 1 times.
       e# This replaces input "test" with the empty string.
:i     e# Cast each character to integer
2fb    e# Replace each integer by the array of its base-2 digits.
1fb    e# Replace each array of base-2 digits by the sum of its digits.
1b     e# Add the sums of digits.

This source code has an ecological footprint of 75.

Dennis

Posted 2015-09-25T11:08:49.607

Reputation: 196 637

3Wow, that's clever. I guess the caveat is that you could never run it, since all the memory in the world couldn't hold the entire code. – Reto Koradi – 2015-09-25T15:33:57.407

51That's a minor inconvenience. You can't put a price on nature. – Dennis – 2015-09-25T15:56:22.940

That's a nice trick, but it is invalid if can't be run – edc65 – 2015-09-25T18:02:42.890

@edc65 You are forgetting moore's law. It could be run, just not now. – PyRulez – 2015-09-25T19:58:04.803

5@PyRulez : Not by anything unless it will be built out of something other than matter and occupy something other than space. – vsz – 2015-09-25T21:00:50.020

5Why not just to use Lenguage? – jimmy23013 – 2015-09-25T22:08:02.220

@jimmy23013 I had only a few minutes to spare, and I've never written a BF program. If nobody beats me to it, I will. – Dennis – 2015-09-25T23:16:31.820

40

Lenguage, 0


Output is in unary, since Lenguage/Brainfuck has no sane way of printing integers in base 10.

The actual source code contains

22360559967824444567791709913713659826044558304969374451791514225490473373040212332757409553558758107085015797320276213515502796255082717802632399123502087743818475438512153373406931103005017157351410347278489842099128517039634739852783737052963203448945756470632484148121769939122103257063633371522287190530269279693540898545359211009781370158317748609540216376596783541124510013448091325488601732964773653391702083563797082990404753843419895799343996435988722965711513708742853668363743953430527328863418281733901770990932025503662188187254784985474815936854540100376410040743052620419372327997519047616042603909398552951490180076364164838561112002025592431155898041427468731461614504254168899805662501979953318388813759833797929243626668399650485310047043700001093878284174322463350892654886806075148010832042248607926124030339950499631072150856939786062937034833055717723216663269161130154002679878012158315587925933383341827053312086716181702533743607685576475754259877651521989944802973721727159955208722180232955193930065862370838526521351991966172723976565264862909528310162816593997640732796289501819499741414526385058421824690665542546821941125191276568479078107133076037506211133628962099403163812267452274532219562823184225236020523509355625620557197876838014050964240952738109101849512504021041103516630358995290177306585560988278630098667702211916671663291473843258785929522017507744814910480115446168939335008597569919072874897148594826036210511162928991890818427747059833051607455121463371211282760364668765311589329918870071117807132901910082663054895226456039171170783440772764031568108965851688162729239711772886386306884508520204834432674839183166053019421652064937613583258148354531835035461504442885024563141848164279928769795684221364984104923764359842286827870778678989243517189772102669283996930513577004801536579491093711362942690905779844535371088542020595945700544234301668098553671685123172583259206072965508639556627967633275762621813851479909708616154198658896714629908456913467267354690109885368211752176196164620615081464122410029328694509842558492529684841818953632659248840216891072110853731776562597900145806210691868173380612838327841104919352821441230296200143603175486627682007399030356592930049570084097858148122367

null bytes and is equivalent to the following Brainfuck program:

,[<<+++++++++++++++++++++++++++++++++++++++++++++++++>>>>>>,]
>>>>>>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<<<<<
<<<++++++++++++++++++++++++++++
[-<----<<<<----<<<<----<<<<---->>>>>>>>>>>>>]
<----<<<<---<<<<+++++++++++<<<<----
<<<<
[>>>>>>>>>>>>>>>>>>>>>>>>]
>>>>
[>>>>>>>>>>>>>>>>>>>>>>>>]
>>>>
[>>>>>>>>>>>>>>>>>>>>>>>>]
>>>>
[>>>>>>>>>>>>>>>>>>>>>>>>]
>>>>
[>>>>>>>>>>>>>>>>>>>>>>>>]
>>>>
[
 <<<<<<<<<<<<<<<<<<<<<<<<
 <<<++++++++++++++++++++++++++++
 [-<++++<<<<++++<<<<++++<<<<++++>>>>>>>>>>>>>]
 <++++<<<<+++<<<<-----------<<<<++++
 >>>>>>>>>>>>
 [
  -[<]<<[.<]
  >>>
  -[<]<<[.<]
  >>>
  -[<]<<[..<]
  >>>
  -[<]<<[.<]
  >>>
  -[<]<<[..<]
  >>>
  -[<]<<[..<]
  >>>
  -[<]<<[...<]
  >>>
  -[<]<<[.<]
  >>>
  -[<]<<[..<]
  >>>
  -[<]<<[..<]
  >>>
  -[<]<<[...<]
  >>>
  -[<]<<[..<]
  >>>
  -[<]<<[...<]
  >>>
  -[<]<<[...<]
  >>>
  -[<]<<[....<]
  >>>
  -[<]<<[.<]
  >>>
  -[<]<<[..<]
  >>>
  -[<]<<[..<]
  >>>
  -[<]<<[...<]
  >>>
  -[<]<<[..<]
  >>>
  -[<]<<[...<]
  >>>
  -[<]<<[...<]
  >>>
  -[<]<<[....<]
  >>>
  -[<]<<[..<]
  >>>
  -[<]<<[...<]
  >>>
  -[<]<<[...<]
  >>>
  -[<]<<[....<]
  >>>
  -[<]<<[...<]
  >>>
  -[<]<<[....<]
  >>>
  -[<]<<[....<]
  >>>
  -[<]<<[.....<]
  >>>
  -[<]<<[.<]
  >>>
  -[<]<<[..<]
  >>>
  -[<]<<[..<]
  >>>
  -[<]<<[...<]
  >>>
  -[<]<<[..<]
  >>>
  -[<]<<[...<]
  >>>
  -[<]<<[...<]
  >>>
  -[<]<<[....<]
  >>>
  -[<]<<[..<]
  >>>
  -[<]<<[...<]
  >>>
  -[<]<<[...<]
  >>>
  -[<]<<[....<]
  >>>
  -[<]<<[...<]
  >>>
  -[<]<<[....<]
  >>>
  -[<]<<[....<]
  >>>
  -[<]<<[.....<]
  >>>
  -[<]<<[..<]
  >>>
  -[<]<<[...<]
  >>>
  -[<]<<[...<]
  >>>
  -[<]<<[....<]
  >>>
  -[<]<<[...<]
  >>>
  -[<]<<[....<]
  >>>
  -[<]<<[....<]
  >>>
  -[<]<<[.....<]
  >>>
  -[<]<<[...<]
  >>>
  -[<]<<[....<]
  >>>
  -[<]<<[....<]
  >>>
  -[<]<<[.....<]
  >>>
  -[<]<<[....<]
  >>>
  -[<]<<[.....<]
  >>>
  -[<]<<[.....<]
  >>>
  -[<]<<[......<]
  >>>
  -[<]<<[.<]
  >>>
  -[<]<<[..<]
  >>>
  -[<]<<[..<]
  >>>
  -[<]<<[...<]
  >>>
  -[<]<<[..<]
  >>>
  -[<]<<[...<]
  >>>
  -[<]<<[...<]
  >>>
  -[<]<<[....<]
  >>>
  -[<]<<[..<]
  >>>
  -[<]<<[...<]
  >>>
  -[<]<<[...<]
  >>>
  -[<]<<[....<]
  >>>
  -[<]<<[...<]
  >>>
  -[<]<<[....<]
  >>>
  -[<]<<[....<]
  >>>
  -[<]<<[.....<]
  >>>
  -[<]<<[..<]
  >>>
  -[<]<<[...<]
  >>>
  -[<]<<[...<]
  >>>
  -[<]<<[....<]
  >>>
  -[<]<<[...<]
  >>>
  -[<]<<[....<]
  >>>
  -[<]<<[....<]
  >>>
  -[<]<<[.....<]
  >>>
  -[<]<<[...<]
  >>>
  -[<]<<[....<]
  >>>
  -[<]<<[....<]
  >>>
  -[<]<<[.....<]
  >>>
  -[<]<<[....<]
  >>>
  -[<]<<[.....<]
  >>>
  -[<]<<[.....<]
  >>>
  -[<]<<[......<]
  >>>
  -[<]<<[..<]
  >>>
  -[<]<<[...<]
  >>>
  -[<]<<[...<]
  >>>
  -[<]<<[....<]
  >>>
  -[<]<<[...<]
  >>>
  -[<]<<[....<]
  >>>
  -[<]<<[....<]
  >>>
  -[<]<<[.....<]
  >>>
  -[<]<<[...<]
  >>>
  -[<]<<[....<]
  >>>
  -[<]<<[....<]
  >>>
  -[<]<<[.....<]
  >>>
  -[<]<<[....<]
  >>>
  -[<]<<[.....<]
  >>>
  -[<]<<[.....<]
  >>>
  -[<]<<[......<]
  >>>
  -[<]<<[...<]
  >>>
  -[<]<<[....<]
  >>>
  -[<]<<[....<]
  >>>
  -[<]<<[.....<]
  >>>
  -[<]<<[....<]
  >>>
  -[<]<<[.....<]
  >>>
  -[<]<<[.....<]
  >>>
  -[<]<<[......<]
  >>>
  -[<]<<[....<]
  >>>
  -[<]<<[.....<]
  >>>
  -[<]<<[.....<]
  >>>
  -[<]<<[......<]
  >>>
  -[<]<<[.....<]
  >>>
  -[<]<<[......<]
  >>>
  -[<]<<[......<]
  >>>
  -[<]<<[.......<]
  <
 ]
]

Try it online on brainfuck.tk.

The length of the Brainfuck code is highly suboptimal – for starters, I've hardcoded the footprints of all ASCII characters – but score 0 is score 0...

Dennis

Posted 2015-09-25T11:08:49.607

Reputation: 196 637

No way, Lenguage actually supports null bytes?! – Beta Decay – 2015-09-26T06:07:53.607

In a Lenguage program, the length of the program is the only thing that matters. I've checked and the interpreter from the Esolang page supports them. – Dennis – 2015-09-26T06:13:46.160

20I feel that answer like this one show no respect of the public: it's cheating, and gaming the system ... But this is the whole point of this challenge after all. +1 – edc65 – 2015-09-26T09:12:43.197

@edc IMO, languages like golfscript etc detract from one possible interpretation of code golf, that is as a "meta" challenge -- creatively working within the rules of languages that were designed for the purpose of writing useful programs, to instead solve arbitrary challenges in as few characters as possible.

Instead, the "challenge" has become using languages that were designed for the purpose of solving arbitrary challenges in as few characters as possible for their intended purpose.... a more appropriate challenge, perhaps, would be designing useful programs in golfscript? – sig_seg_v – 2015-09-27T14:10:30.557

@sig_seg_v What I meant: this is the whole point of THIS specific challenge. I have nothig against GolfScript, Cjam and the like. And it's not difficult writing useful programs in GS or CJam - it would be nightmarish to mantain and evolve such programs, but that is true for a golfed program in any language – edc65 – 2015-09-27T14:38:56.693

1"Output is in unary, since Lenguage/Brainfuck has no sane way of printing integers in base 10.". I thought part of the point of Lenguage/Brainfuck was that it didn't have a sane way of doing anything :) – Adam – 2015-09-28T14:57:50.677

@edc I think I understood what you were saying, I was just taking what I understood as the "spirit" of your answer and making a connection with my fogey vision of the "spirit" of code golf itself. The thing is, lenguage wasn't even developed for this challenge (or golfing at all), it's basically an impractical joke afaict. It just happens to be appropriate to golf this particular challenge. That's part of the fun of codegolf the way I see it, is finding a language that can solve a particular problem with a small source. In practice, that language is cjam or golfscript. Disappointing. – sig_seg_v – 2015-09-28T16:52:28.277

9This didn't quite work for me when I tried to recreate your program from the definition you gave. Could you please post the full Lenguage source so that I can see where your program differs from my attempt at recreating it? ;-) – Cort Ammon – 2015-09-29T00:08:44.670

1What amazes me isn't that Lenguage supports nulls, but that you managed to make such a program in BF. Haunting... – Erik the Outgolfer – 2017-01-13T13:59:52.157

12

PowerShell, 337 344 304 points

PARAM([CHAR[]]$A)$A|%{$B+=([CONVERT]::TOSTRING(+$_,2)-REPLACE0).LENGTH};($B,0)[-JOIN$A-CEQ"test"]

I'M SHOUTING AT YOU BECAUSE IT'S CHEAPER!

Takes input as $A, then casts as a char-array, then iterates over a for-loop on each character, uses the ridiculously wordy [convert]::ToString() to convert the character at that position to binary, replaces all the 0's with nothing, then counts the length, and adds that to $B. At the end, uses an equivalence to index into a dynamic array (i.e., if $A is test, then -CEQ is $TRUE, so it indexes to the second element, 0).

Edit1 -- Corrected test case "TEST"
Edit2 -- Golfed a couple points by iterating over the characters themselves rather than their indices, and by remembering that -replace doesn't need a second parameter if you're replacing it with nothing.

AdmBorkBork

Posted 2015-09-25T11:08:49.607

Reputation: 41 581

Double quote " 00100010 is more environmentally friendly than single quote ' 00100111. – Jacob Krall – 2015-09-25T17:23:37.663

Returns incorrect value 0 for input "TEST" – Jacob Krall – 2015-09-25T17:27:06.783

1@JacobKrall Good catch on the double-quote ". Also corrected with -CEQ for case-sensitivity. It upped the points a little bit, because I was incorrectly scoring the ' ' as I didn't delimit it correctly on my tests. – AdmBorkBork – 2015-09-25T18:23:50.673

9

Pyth - 52 49

Three points save thanks to @orlp.

*/.BQ`1nQ"test

Takes input in quotes to save footprint.

Test Suite.

Maltysen

Posted 2015-09-25T11:08:49.607

Reputation: 25 023

Ugh, I almost got the same as you, but my answer is too similar and you posted first. Replace @,0 with * to save 3 :) – orlp – 2015-09-25T13:19:42.907

1@Maltysen You say that you found three points but the source still says @,0, did you just forget to change? – clap – 2015-09-26T04:55:19.040

@ConfusedMr_C yep. I just changed the permalink and forgot the actual answer. – Maltysen – 2015-09-27T15:01:40.520

7

Common Lisp, 294 281 235

In order to reduce the score, I used @ (cost 1) and ! (cost 2) as variable names (edit: and it is even better if I use @ for the variable occuring the most in the function). I AM SHOUTING TOO because it is cheaper.

(LAMBDA(@)(IF(STRING="test"@)0(LOOP FOR ! ACROSS @ SUM(LOGCOUNT(CHAR-CODE !)))))

Pretty-printed

(LAMBDA (@)
  (IF (STRING= "test" @) 0
      (LOOP FOR ! ACROSS @ SUM (LOGCOUNT (CHAR-CODE !)))))

coredump

Posted 2015-09-25T11:08:49.607

Reputation: 6 292

Well the score is more like 294 ;) – Cabbie407 – 2015-09-25T12:39:10.500

@Cabbie407 I missed the part about scoring, sorry :-) – coredump – 2015-09-25T12:40:35.833

1@Cabbie407 I had the strange feeling of having a very low score for a couple of minutes... – coredump – 2015-09-25T12:42:51.913

1Just wanted to let you know, because knowing about the scoring method you'd probably look at your code from a different angle. And I see you changed something already. – Cabbie407 – 2015-09-25T13:05:52.633

1@Cabbie407 I forgot to thank you, btw. Thanks. – coredump – 2015-09-25T13:12:11.793

6

Python 3, 271

z=input();print([sum([bin(ord(i)).count("1")for i in z]),0][z=="test"])

Blue

Posted 2015-09-25T11:08:49.607

Reputation: 26 661

3

A lot of little changes gives me 228

– FryAmTheEggman – 2015-09-25T13:40:15.430

2Why not take advantage that bools are ints? z=input();print(sum(bin(ord(i)).count("1")for i in z)*(z!="test")) ....@FryAmTheEggman jinx? – NightShadeQueen – 2015-09-25T13:40:29.573

1@NightShadeQueen Haha, does that mean I can't post for a day? :X Anyway that's a very nice catch for a new user to this site, nice job! Anyway, welcome to PPCG! :) Also, more on-topic, the semicolon could be removed as it costs slightly more than a newline. – FryAmTheEggman – 2015-09-25T16:32:02.587

6

JavaScript, 279

Edit Bug fix (did not count bit 1 of each char)

A complete program, with input and output via popup. Tested in Firefox, should work in any modern browser.

B=(P=prompt)(H=D=0)
while(B!="test"&&(A=B.charCodeAt(H++)))while(A)D+=A&1,A>>=1
P(D)

Some tools (Tested with Firefox)

w=c=>c.toString(2).split('').reduce(function(a,b){return a- -b})

t=[[],[],[],[],[],[],[],[],[]]
u=[[],[],[],[],[],[],[],[],[]]
for(c=1;c<256;c++)
  c<33|c>126&c<161 ? t[w(c)].push('\\'+c) : u[w(c)].push('&#'+c+';')
for(i=0; i++<8;)       
  T.innerHTML+=i+': '+u[i].concat(t[i]).join(' ')+'\n'

function Calc()
{
  var r='', t=0, b
  I.value.split('').forEach(function(c) {
    c = c.charCodeAt(), r += '\n&#'+c+' '+((256+c).toString(2).slice(1))+' : '
    for(b=0;c;c>>=1) b += c&1
    r += b, t += b
  })
  R.innerHTML='Total '+t+'\nDetail'+r
}
#I { width: 400px }
<b>Weight table</b><pre id=T></pre><br>
<b>Counter</b><br><textarea id=I></textarea><button onclick="Calc()">-></button> <pre id=R></pre>

edc65

Posted 2015-09-25T11:08:49.607

Reputation: 31 086

1This answer is invalid - it outputs 17 for test instead of 0. – ASCIIThenANSI – 2015-09-25T15:57:22.320

@ASCIIThenANSI not it doesn't in my browser. But I'm going to check again – edc65 – 2015-09-25T16:56:58.333

That's funny... Testing it with your counter, I get 279, and testing it with itself, I get 277. I wonder which is correct; could it have something to do with the newlines? – ETHproductions – 2015-09-25T20:57:54.900

@ETHproductions I double checked and the right count is 279. But it does not work with a string containing a newline - it's a problem related to the prompt function. In Firefox, prompt translates the newlines (2bit) in spaces (1bit), so we get 277 instead of 279 – edc65 – 2015-09-26T01:45:47.320

@ETHproductions ... in Chrome (on Windows) the newline becomes a CR LF pair (3bit + 2bit) and the count is wrong again – edc65 – 2015-09-26T01:49:16.237

6

Julia, 254 246 232

P=readline()
print(P=="test"?0:sum([count_ones(1*A)for A=P]))

The count_ones function counts the number of ones in the binary representation of its input.

Reduced my ecological footprint thanks to FryAmTheEggman!

Alex A.

Posted 2015-09-25T11:08:49.607

Reputation: 23 761

1No problem, I just really care about the environment ;) – FryAmTheEggman – 2015-09-25T16:28:41.170

5

MATLAB, 198 194 bytes

A=input('','s');~strcmp('test',A)*nnz(dec2bin(A)-48)

First, the string is read in from STDIN via the input function. Once this happens, we compare the input string to the string test. If the result isn't test, we convert each character to its ASCII code and then its binary representation via dec2bin. A beautiful consequence of this function is that if you submit a string, the binary representation of its ASCII code is delimited as one character per line.

As an example:

>> dec2bin('ABCD')

ans =

1000001
1000010
1000011
1000100

dec2bin outputs a character array. Once this happens, subtract by 48, which is the ASCII code for 0 so that the matrix gets converted to double consisting of 0s and 1s. Once that happens, a call to nnz counts up the total number of non-zero elements in this matrix. Take note that this result is multiplied by the opposite of the string compare with test. Should the string not be test, we get the footprint calculation. If it's equal, then the multiplication results in 0.

Some examples:

>> A=input('','s');~strcmp('test',A)*nnz(dec2bin(A)-48)
A

ans =

     2

>> A=input('','s');~strcmp('test',A)*nnz(dec2bin(A)-48)
O

ans =

     5

>> A=input('','s');~strcmp('test',A)*nnz(dec2bin(A)-48)
test

ans =

     0


>> A=input('','s');~strcmp('test',A)*nnz(dec2bin(A)-48)
  %// Note - no characters were added here.  Simply pushed Enter

ans =

     0

rayryeng - Reinstate Monica

Posted 2015-09-25T11:08:49.607

Reputation: 1 521

If you're allowed to use Communications Toolbox, you can use de2bi instead and avoid the -48 to cast it to a numeric type (as well as the 2 extra characters in the function name). – beaker – 2015-09-25T21:15:51.247

5

Perl, 136 118 73

$_=unpack"B*";$_=y@1@@

Replace all @ with \0

Usage example:

perl -p entry.pl entry.pl

Jarmex

Posted 2015-09-25T11:08:49.607

Reputation: 2 045

5

Bash 440 430 412 405 403

A=0
[ test != "$1" ]&&for((D=0;D<${#1};D++)){
A=$((A+`bc<<<$(printf "obase=2;%d" "'${1:$D:1}")|tr -d "0
"|wc -m`))
}
echo $A

Pretty straightforward. Loops characters in the input converting first to ascii (with printf %d and the leading ' on the number then to binary (with bc), strips out the zeros and counts the number of characters.

Not a great answer but hadn't seen a bash attempt yet.

Modified since my first answer allowed the input string to be given simply on the command line (i.e. it became multiple input params if multipe words) but after reading some other answers I think I can assume it is quoted, so the whole string comes as $1

Adam

Posted 2015-09-25T11:08:49.607

Reputation: 151

1Welcome to Programming Puzzles & Code Golf! 1. You can replace do with { and done with }. 2. You also don't need the spaces around <<<. 3. You can replace \n with a literal linefeed. – Dennis – 2015-09-27T15:37:45.690

THanks @Dennis. One of the challenges on this site is unlearning a bunch of "good habits" :). – Adam – 2015-09-27T15:42:40.780

3

It sure is. If you haven't already, I recommend checking out Tips for golfing in Bash. It's a great resource.

– Dennis – 2015-09-27T15:44:37.230

3This challenge is weird even by golf standards! Extra characters can still save points. Using = and || costs 15 whereas using != and && is only 13! An extra character but saves two points... – Adam – 2015-09-27T18:30:43.153

5

Ceylon, 1431, 764, 697, 571, 547, 538, 501, 493, 467, 451

shared void p(){print(sum{0,if(exists a=process.arguments[0])if(a!="test")for(c in a)for(b in 0..7)if(c.hash.get(b))1});}

This was the original, ungolfed:

Integer footprintCharacter(Integer b) {
    return sum({0, for(i in 0..7) if(b.get(i)) 1 });
}
Integer footPrintString(String s) {
    if(s == "test") {return 0;}
    return sum({0, for(c in s) footprintCharacter(c.integer)});
}
shared void footprint() {
     if(exists s = process.arguments[0]) {
         print(footPrintString(s));
     } else {
         print("This program needs at least one parameter!");
     }
}

This takes the argument from a command line parameter ... process.arguments is an (possibly empty) sequence of strings, so before using one of them, we need to check if it actually exists. In the other case we output an error message (this is not required by the question and will be thrown away in the next versions).

Ceylon's sum function takes a non-empty Iterable of elements of some type which needs to satisfy Summable, i.e. has a plus method, like Integer. (It doesn't work with empty sequences because each Summable type will have its own zero, and the runtime has no chance to know which one is meant.)

The elements of a string, or the one bits of an integer, are not a non-empty iterable. Therefore we are using here the feature to build an iterable by specifying some elements, then a "comprehension" (which will get evaluated to zero or more elements). So in the character case we are adding ones (but only when the corresponding bit is set), in the string case we are adding the result of the characters. (The comprehension will only be evaluated when the receiving function actually iterates over it, not when building the Iterable.)

Let's see how we can shrink this down. First, each of the functions is only called at one place, so we can inline them. Also, as mentioned above, get rid of the error message. (764 footprint points.)

shared void footprint() {
    if (exists s = process.arguments[0]) {
        if (s == "test") {
            print(0);
        } else {
            print(sum({ 0, for (c in s) sum({ 0, for (i in 0..7) if (c.integer.get(i)) 1 }) }));
        }
    }
}

We don't actually need the inner nested sum, we can make this one big comprehension. (This saves us 37 footprint points for sum({0,}), and some more for whitespace, which will be eliminated at the end anyways.) This is 697:

shared void footprint() {
    if (exists s = process.arguments[0]) {
        if (s == "test") {
            print(0);
        } else {
            print(sum({ 0, for (c in s) for (i in 0..7) if (c.integer.get(i)) 1 }));
        }
    }
}

We can apply a similar principle to the special cased "test" string: as in that case the result is 0 (i.e. nothing is contributed to the sum), we can just do this as a part of the summation (but we have to invert the condition). This mainly saves us the print(0);, some braces and a bunch of indentation spaces, coming down to a footprint of 571:

shared void footprint() {
    if (exists s = process.arguments[0]) {
        print(sum({ 0, if (s != "test") for (c in s) for (i in 0..7) if (c.integer.get(i)) 1 }));
    }
}

We do the same for the first if, with the side effect that now giving no argument also outputs 0 instead of doing nothing. (At least I thought that would happen here, instead it seems to hang with an eternal loop? Strange.)

shared void footprint() {
    print(sum({ 0, if (exists s = process.arguments[0]) if (s != "test") for (c in s) for (i in 0..7) if (c.integer.get(i)) 1 }));
}

We can actually omit the () for the sum function here, using an alternative function call syntax, which uses {...} instead of (), and will fill in comprehensions into iterable arguments. This has footprint 538:

shared void footprint() {
    print(sum{ 0, if (exists s = process.arguments[0]) if (s != "test") for (c in s) for (i in 0..7) if (c.integer.get(i)) 1 });
}

Replacing the function name footprint (40) by p (3) saves another 37 points, bringing us to 501. (Ceylon function names have to start with lower case characters, so we can't get less than 3 points here.)

shared void p() {
    print(sum{ 0, if (exists s = process.arguments[0]) if (s != "test") for (c in s) for (i in 0..7) if (c.integer.get(i)) 1 });
}

The variable names s (5) and c (4), i (4) are also not optimal. Let's replace them by a (argument), d (digit?) and b (bit-index). Footprint 493:

shared void p() {
    print(sum{ 0, if (exists a = process.arguments[0]) if (a != "test") for (c in a) for (b in 0..7) if (c.integer.get(b)) 1 });
}

I don't see any remaining non-whitespace optimization, so let's remove the non-needed whitespace (1 point for each space, two for each of the two line breaks):

shared void p(){print(sum{0,if(exists a=process.arguments[0])if(a!="test")for(c in a)for(b in 0..7)if(c.integer.get(b))1});}

When browsing the API, I found that Character.hash actually returns the same value as its integer attribute. But it has just 14 points instead of 30, so we come down to 451!

shared void p(){print(sum{0,if(exists a=process.arguments[0])if(a!="test")for(c in a)for(b in 0..7)if(c.hash.get(b))1});}

Paŭlo Ebermann

Posted 2015-09-25T11:08:49.607

Reputation: 1 010

4

C,374

Newlines (not included in score) added for clarity. Could be improved to 360 just by changing variable names to uppercase, but I'll try to think of something better.

Input is via commandline, which means it segfaults on absent input. I expect a worse score for input through stdin.

i,t;
main(int c,char**v){
for(;c=v[i][i/8];i++)t+=(c>>i%8)&1;
printf("%d",strcmp(v[1],"test")?t:0);
}

Level River St

Posted 2015-09-25T11:08:49.607

Reputation: 22 049

4

VBA, 475 418

Thanks Jacob for 57 points off

  • Converts String to Byte Array (128 is vba shortcut for "Converts the string from Unicode to the default code page of the system" So won't work on Mac....)

  • Loops though byte array converting to Binary and Concatenating everything together.

  • checks for test
  • Prints length of string with all the 0's replaced with nothing

VBA why are you so bad at golfing... :(

SUB A(D)
DIM B() AS BYTE
B=STRCONV(D,128)
FOR P=0 TO UBOUND(B)
H=H+APPLICATION.DEC2BIN(B(P))
NEXT
IF D="test" THEN H=0
MSGBOX LEN(REPLACE(H,0,""))
ENDSUB

JimmyJazzx

Posted 2015-09-25T11:08:49.607

Reputation: 691

4VBA is case-insensitive, so you should use use uppercase everywhere to save a point per lowercase character! (except "test" of course) – Jacob Krall – 2015-09-25T16:40:20.893

4

JavaScript, 418 410

A=prompt();B=0;!A||A=="test"?0:A.split("").forEach(D=>B+=D.charCodeAt().toString(2).match(/1/g).length);alert(B)

Diego Torres

Posted 2015-09-25T11:08:49.607

Reputation: 161

Double quote " 00100010 is more environmentally friendly than single quote ' 00100111. – Jacob Krall – 2015-09-25T17:21:00.947

4

PowerShell, 273 336 328 324 293 288 295

PARAM($A)[CHAR[]]$A|%{$D=[INT]$_;WHILE($D){$B+=$D-BAND0X1;$D=$D-SHR1}};($B,0)[$A-CEQ"test"]

edit - forgot the 'test' case ... so much expensive.

editedit - missed an UPPERCASE opportunity.

editeditedit - incorporated the comment suggestions (Thanks TimmyD).

edit 4 - D is a cheaper varable than C (2 vs. 3)

edit 5 - Back to 295 due to the case-sensitivity check.

Loops over the string and counts the 1s which get shifted off the characters ASCII value.

Hat-tip to TimmyD for giving me the foresight to use uppercase characters AND to use the array index at the end.

Forty3

Posted 2015-09-25T11:08:49.607

Reputation: 341

1Nice approach! A couple of golfs (removed the $B initialization, as it will default to zero, removed a couple parens, removed some semicolons), brings it down to 293 PARAM($A)[CHAR[]]$A|%{$C=[INT]$_;WHILE($C){$B+=$C-BAND0X1;$C=$C-SHR1}};($B,0)[$A-EQ"TEST"] – AdmBorkBork – 2015-09-25T16:52:24.403

What does it mean when I read "Nice approach!" and heard the voice from Wii Golf? Thanks for the pointers! The initialization was tripping me up when testing it in the PowerShell console and I left it in. Good to know otherwise. – Forty3 – 2015-09-25T16:55:26.117

Yeah - if you're using the console, it's pretty much a REPL environment, in that variables assigned remain from one line to the next. If you save it as a .ps1, then the $B (and every other variable) is re-initialized, even if you press up-arrow-enter from the same shell. E.g., PS C:\scripts> .\ecological-footprint.ps1 – AdmBorkBork – 2015-09-25T17:01:07.897

Returns incorrect value 0 for input "TEST" – Jacob Krall – 2015-09-25T17:29:30.093

1281 PARAM($A)(([CHAR[]]$A|%{$B=$_;0..9|?{[INT]$B-SHR$_-BAND1}}).LENGTH,0)[("TEST"-EQ$A)] – tomkandy – 2015-09-25T17:46:27.740

@tomkandy - I like the approach especially the better use of the -shr $_ while iterating over 0..9. I would have to change the final equality test to [($A-CEQ"test")] due to the case-sensitivity pointed out by Jacob Krall in a previous comment. However, still puts me in the 288 range. Thanks! – Forty3 – 2015-09-25T18:10:50.873

4

Matlab, 320

A=(input('','s'));nnz(floor(rem(bsxfun(@times,[A 0],2.^(-7:0)'),2)))*~strcmp(A,'test')

Luis Mendo

Posted 2015-09-25T11:08:49.607

Reputation: 87 464

4

PHP, 377 337 299 Ecological Footprint (still a lot), 102 91 Bytes

Seems that PHP is environment friendly in test-mode only. ;)

WHILE($D<STRLEN($A=$argv[1]))$B+=SUBSTR_COUNT(DECBIN(ORD($A[$D++])),1);ECHO"test"!=$A?$B:0;

Runs from command line like:

php footprint.php hello
php footprint.php test

while is more environment friendly than for even though they share the same character count. Also uppercase variable names have a better footprint than their lowercase counterparts.

Edit

  • saved 40 points by using uppercase function names.
  • saved 38 points by using decbin instead of base_convert

insertusernamehere

Posted 2015-09-25T11:08:49.607

Reputation: 4 551

1

@Adam Here is a discussion about the notices and here about the PHP opening tags. Hope this is useful for you.

– insertusernamehere – 2015-10-09T07:13:06.887

3

Pyth, 64

?qz"test"0l`sS.Bz

Checks if the input is test and if not, counts the number of 1's in the binary representation of the input.

Blue

Posted 2015-09-25T11:08:49.607

Reputation: 26 661

3

Ruby, 316 313

Very straightforward, looking for some more golfing possibilities:

b=gets.chomp;b=='test'?0:b.chars.map{|i|i.ord.to_s(2).count('1')}.inject(:+)
  • Used b instead of x to save 3 points.

Caridorc

Posted 2015-09-25T11:08:49.607

Reputation: 2 254

You can use $*[0] instead of gets.chomp (takes input as a command line argument) – Mhmd – 2015-09-25T15:01:02.883

Double quote " 00100010 is more environmentally friendly than single quote ' 00100111. – Jacob Krall – 2015-09-25T17:21:12.350

Uppercase variable names are also more environmentally friendly than the equivalent lowercase. H is better than I for the same reason. – Jacob Krall – 2015-09-25T17:22:38.960

3

JavaScript (ES6), 521 478 458 449 473 465

alert(((A=prompt(),A!="test")&&(A!=""))?(A.split``.map(H=>(H.charCodeAt().toString(2).match(/1/g)||[]).length)).reduce((A,B)=>A+B):0)

This is my first attempt at a JavaScript golf, so it's probably very ungolfed.

Zach Gates

Posted 2015-09-25T11:08:49.607

Reputation: 6 152

It's generally agreed on that JavaScript golf needs some form of output other than implicit. This can be an alert, document.write, or even a function return. – Mwr247 – 2015-09-25T14:11:18.463

You can move your prompt assignment into the first 's' in the if statement, surrounded by parenthesis, to save a couple bytes. You can also remove the '0' in charCodeAt. Also, a big savings you can make is using the ternary operator instead of the if/else statements =) – Mwr247 – 2015-09-25T14:12:32.767

Thanks a lot! I ended up using a comma instead of parenthese for the prompt assignment, though; it saves another byte. (: @Mwr247 – Zach Gates – 2015-09-25T14:33:18.937

s.split('') can instead be s.split``, saving 2 bytes – Dendrobium – 2015-09-25T15:58:47.030

Use uppercase variable names to reduce your ecological footprint – Jacob Krall – 2015-09-25T16:16:58.270

Also, use A instead of S and H instead of I to further reduce your ecological footprint – Jacob Krall – 2015-09-25T16:18:40.030

@ZachGates: now you have some backticks in your source code where the split('') used to be – Jacob Krall – 2015-09-25T16:24:36.570

1Per Dendrobium's comment above @JacobKrall – Zach Gates – 2015-09-25T16:26:01.600

It seems like the parentheses around (A.split``.map ... .length)) are unnecessary – Jacob Krall – 2015-09-25T16:37:16.197

Double quote " 00100010 is more environmentally friendly than single quote ' 00100111. – Jacob Krall – 2015-09-25T17:23:16.390

3

Python 2, 294 281 269 266

A=input()
print sum(format(ord(H),"b").count("1")for H in A)if A!="test"else 0

A port of my Pyth answer, above.

Input is received as a string (with quotes):

"ABC"

Zach Gates

Posted 2015-09-25T11:08:49.607

Reputation: 6 152

1Double quote " 00100010 is more environmentally friendly than single quote ' 00100111. – Jacob Krall – 2015-09-25T17:20:43.433

A couple basic modifications get this down to A=input();print[sum(bin(ord(H)).count("1")for H in A),0][A=="test"] with a score of 243. – Kade – 2015-09-25T20:49:51.160

3

Haskell, 292

a 0=0
a b=rem b 2+a(div b 2)
b"test"=0
b d=sum$map(a.fromEnum)d
main=interact$show.b

Nothing much to say here: turn every character into ascii value (fromEnum) and calculate the 1s (via a). Sum all the results.

nimi

Posted 2015-09-25T11:08:49.607

Reputation: 34 639

2

CJam, 123

q_"test"={;0}{{s2b2b~}%1e=}?

Zach Gates

Posted 2015-09-25T11:08:49.607

Reputation: 6 152

2

Pyth, 96

Iqz"test"0.q)/j""m.BdmCdz\1

A port of my CJam answer, above/below.

Zach Gates

Posted 2015-09-25T11:08:49.607

Reputation: 6 152

Just some Pyth general notes: instead of I try to use the ternary ?, but in this case since it is a bool you can just use * (after switching to n instead of q), k is automatically "" and s on strings is the same a jk. Hope you have fun learning pyth! :) – FryAmTheEggman – 2015-09-25T16:35:31.773

This was my first Pyth answer :P It was hard enough to get this, haha, fun though. Thanks for the tips! @FryAmTheEggman – Zach Gates – 2015-09-25T16:36:23.300

2

CJam, 83 81 79 77

Best so far after trying a number of variations:

l0$"test"=!\:i2fbe_1b*

Try it online

Explanation:

l       Get input. Other options like q and r are the same number of bits.
0$      Copy input for comparison. This saves 2 bits over _.
"test"  Push special case string.
=       Compare.
!       Negate so that we have 0 for special case, 1 for normal case.
\       Swap input string to top.
:i      Convert characters to integers.
2fb     Apply conversion to base 2 to all values.
e_      Flatten array.
1b      Sum up the bits. This is 2 bits shorter than :+.
*       Multiply with result from special case test.

Reto Koradi

Posted 2015-09-25T11:08:49.607

Reputation: 4 870

2

Ruby, 247

Straightforward approach looping through all bytes of the input and all bits in each byte, summing to variable d.

d is initialized to -2 because h contains the terminating newline from the input (worth 2 bits) and we do not want to count that.

Similarly h will contain test with a trailing newline, so a newline must included in the comparison value.

d=-2
h=gets
h.bytes{|a|8.times{|b|d+=a>>b&1}}
p h=='test
'?0:d

Level River St

Posted 2015-09-25T11:08:49.607

Reputation: 22 049

2

R, 279

sum(as.integer(rawToBits(charToRaw(if((s=readline())=='test')''else s))))

Pretty self explanatory.
Tests:

> sum(as.integer(rawToBits(charToRaw(if((s=readline())=='test')''else s))))
sum(as.integer(rawToBits(charToRaw(if((s=readline())=='test')''else s))))
[1] 279
> sum(as.integer(rawToBits(charToRaw(if((s=readline())=='test')''else s))))
A
[1] 2
> sum(as.integer(rawToBits(charToRaw(if((s=readline())=='test')''else s))))
O
[1] 5
> sum(as.integer(rawToBits(charToRaw(if((s=readline())=='test')''else s))))
test
[1] 0
> sum(as.integer(rawToBits(charToRaw(if((s=readline())=='test')''else s))))
OAO
[1] 12

MickyT

Posted 2015-09-25T11:08:49.607

Reputation: 11 735

2

C, 378 footprint, 98 bytes

Another C solution:

s;main(c,a)char**a;{for(s=-17*!strcmp(a[1],"test");c=*a[1]++;)for(;c;s+=c&1,c/=2);printf("%d",s);}

The way this works is that s is initialized to 0 usually, but becomes -17 if the command-line argument is "test" (strcmp returns 0 on equal strings, and non-zero on distinct strings, so inverting it gives 1 if string is "test"). The number -17 was chosen to compensate for the footprint of 17 that will be calculated for "test". The computation of the footprint is easy with bitwise operators.

Snap! I initially missed the "shortest footprint wins" so I was aiming for shortest code... I'll see if I can make the "footprint" smaller.

Tob Ernack

Posted 2015-09-25T11:08:49.607

Reputation: 211

2

Groovy, 326 242

B=0
P=args[0]
P.bytes.each{B+=0.bitCount(it)}print P=="test"?"0":B

Kleyguerth

Posted 2015-09-25T11:08:49.607

Reputation: 61

2

Java, 594

class A{public static void main(String[]P){Integer D,H;for(D=H=0;D<P[0].length();)H+=D.bitCount(P[0].charAt(D++));System.out.print(P[0].equals("test")?0:H);}}

Java is not very green.

Ungolfed version:

class A {
    public static void main(String[]P) {
        Integer D,H;
        for(D=H=0;D<P[0].length();)
            H+=D.bitCount(P[0].charAt(D++));
        System.out.print(P[0].equals("test")?0:H);
    }
}

D is declared as an Integer so we can access Integer's static bitCount method in an environmentally conscientious manner. The bitCount method treats the chars as integers and returns the number of set bits.

intrepidcoder

Posted 2015-09-25T11:08:49.607

Reputation: 2 575

1Interesting comparing Java with Ceylon ... Java has some overhead due to the boilerplate, and a longer print statement. The bitCount does help, though, Ceylon doesn't have this. Ceylon has a longer way to access to the command line parameters, and also needs to check if they are actually given (where your program would simply throw an ArrayIndexOutOfBoundsException). Ceylon's sum function is certainly shorter than adding it manually in Ceylon (but Java has no comprehensions, so adding manually is better than building an Iterable yourself). – Paŭlo Ebermann – 2015-09-28T11:20:03.140

2

R, 251 246 241 236

s=readline();sum(if(s!="test")as.real(rawToBits(charToRaw(s))))

Usage:

> s=readline();sum(if(s!="test")as.real(rawToBits(charToRaw(s))))
topical subject
[1] 58
> s=readline();sum(if(s!="test")as.real(rawToBits(charToRaw(s))))
test
[1] 0
> s=readline();sum(if(s!="test")as.real(rawToBits(charToRaw(s))))
s=readline();sum(if(s!="test")as.real(rawToBits(charToRaw(s))))
[1] 236

Edits: Using as.double instead of as.integer lowers by 5 points the footprint.
as.real is deprecated in the current version of R but in R < 3.0 replacing as.double by as.real lowers the footprint by 10 additional points.

plannapus

Posted 2015-09-25T11:08:49.607

Reputation: 8 610

1

Whispers v2, 438

> InputAll
> 2
>> ?L
>> L⊥2
>> ∑L
>> Each 3 1
>> Each 4 6
>> Each 5 7
>> ∑8
>> Output 9
> "test"
>> 1=11
> 0
>> Output 13
>> If 12 14 10

Try it online!

Unfortunately, we are sort of bound to have a high score when using Whispers for this task, as the key character is >, which has a score of 5. Aside from using " instead of ' quotes, there isn't really much room for improvement without totally rewriting the method used. Using digits which score lower (01248) rather than the higher scoring ones (35679) doesn't allow us to improve, as no high-scoring single digit is repeated (3 occurs more than once, but restructuring doesn't allow us to change the 13 to something lower scoring)

Anyway, the way the program works is fairly understandable, especially for those familiar with Whispers. Our first thing to hold in mind is that 1 is the line reference for the input string. We begin on the last line, with the ternary statement

>> If 12 14 10

The thing to remember with Whispers is that, when a line begins with two > symbols, the numbers act as line references, rather than literal values. That makes this statement equivalent to

If line(12) Then line(14) Else line(10)

Lines 11 and 12, our condition, is

> "test"
>> 1=11

Remembering that line 1 is the input string, this compares the input with the string "test" and returns that boolean.

Our program then branches. First, as it's simpler, we'll take a look at the code executed if the input is the string "test":

> 0            ; line 13
>> Output 13   ; line 14
>> If 12 14 10

If so, we call line 14, which as you can see, outputs the value on line 13, a 0. Therfore, if the input is equal to the string "test", we output a 0, then quit.

However, if the input does not equal the string, we jump up to line 10. Lines 9 and 10 are as follows:

>> ∑8
>> Output 9

So, if the input isn't in test mode, we output the sum of the result from line 8, which is the final Each statement in a series of three:

> InputAll
> 2
>> ?L
>> L⊥2
>> ∑L
>> Each 3 1
>> Each 4 6
>> Each 5 7

Our first Each statement is >> Each 3 1. This maps line 3 over each character in the input. Line 3, >> ?L, takes the currently iterated character and converts it to its ordinal value. This value is then passed to line 7, which converts it to base 2, before line 8 takes the sum of that (the number of 1s in the binary expansion). This returns an array of integers, representing the number of 1s in the binary expansion of each ordinal of each character in the input. As stated above, we then take the sum of this, before outputting it and terminating.

Fun fact:

  • The markdown of this post is functionally identical to the program
  • This post has a score of 11125

user80239

Posted 2015-09-25T11:08:49.607

Reputation:

1

awk, 339

{while(A++<NF){for(D=0;sprintf("%c",++D)<$A;);for(;D;D=int(D/2))B+=D%2}}$0=/^test$/?0"":B

Needs to be called with awk ' ... ' FS= so I calculated the fingerprint for the code plus FS=.

It's as always a bit longer when involving ASCII codes, because awk has no direct method of getting the code of a character. So I test for every number D starting at zero, if sprintf("%c",D) results in the character we want the ASCII code for.

Cabbie407

Posted 2015-09-25T11:08:49.607

Reputation: 1 158

1

CoffeeScript, 411 391

()->((A=prompt())!="test"&&A.split("").map((P)->P.charCodeAt().toString(2).replace /0/g,"").join("")||"").length

This can probably be improved.

Functions in CoffeeScript always return.

Thanks @Jacob Krall for reducing this 20 points.

rink.attendant.6

Posted 2015-09-25T11:08:49.607

Reputation: 2 776

Double quote " 00100010 is more environmentally friendly than single quote ' 00100111. – Jacob Krall – 2015-09-25T17:20:31.223

1

Stuck, 103

Certainly not great.. Explanation pending.

s_c"2b$o]];,":+;"test"=0u?

Kade

Posted 2015-09-25T11:08:49.607

Reputation: 7 463

1

ES6, 360

alert((A=prompt())!="test"?A.replace(/./g,B=>B.charCodeAt().toString(2)).replace(/0/g,"").length:0)

Neil

Posted 2015-09-25T11:08:49.607

Reputation: 95 035