[S S S N
_Push_0][N
S S N
_Create_Label_LOOP][S N
S _Duplicate][S N
S _Duplicate][T N
T S _Read_STDIN_as_character][T T T _Retrieve][S S S T S T T S T T S N
_Push_182][T S S T _Subtract][N
T S S N
_If_0_jump_to_Label_PRINT_AND_EXIT][S S S T N
_Push_1][T S S S _Add][N
S N
N
_Jump_to_Label_LOOP][N
S S S N
_Create_Label_PRINT_AND_EXIT][T N
S T _Print_to_STDOUT]
Letters S
(space), T
(tab), and N
(new-line) added as highlighting only.
[..._some_action]
added as explanation only.
Input requires a trailing ¶
, because Whitespace has no way to tell when the input-characters are complete. Whitespace only has two STDIN inputs: integer or character. A full string should be inputted one by one, but Whitespace doesn't know when it has all. So perhaps this answer is non-competing due to the restriction of Whitespace, but I've chosen a character outside the printable ASCII range to add as leading character for inputs, so the program knows when to stop and output the length.
Try it online (with raw spaces, tabs and new-lines only).
Explanation in pseudo-code:
Integer counter = 0
Start LOOP:
Integer input = STDIN as character
If(input == '¶')
Jump to function PRINT_AND_EXIT
counter = counter + 1
Jump to next iteration of LOOP
function PRINT_AND_EXIT:
Print counter as integer to STDOUT
Example input: acb¶
Command Explanation Stack HEAP STDIN STDOUT STDERR
SSSN Push 0 [0]
NSSN Create Label_LOOP [0]
SNS Duplicate top (0) [0,0]
SNS Duplicate top (0) [0,0,0]
TNTS Read STDIN as character [0,0] {0:97} 'a'
TTT Retrieve [0,97] {0:97}
SSSTSTTSTTSN Push 182 [0,97,182] {0:97}
TSST Subtract top two (97-182) [0,-85] {0:97}
NTSSN If 0: Jump to Label_EXIT [0] {0:97}
SSSTN Push 1 [0,1] {0:97}
TSSS Add top two (0+1) [1] {0:97}
NSNN Jump to Label_LOOP [1] {0:97}
SNS Duplicate top (1) [1,1] {0:97}
SNS Duplicate top (1) [1,1,1] {0:97}
TNTS Read STDIN as character [1,1] {0:97,1:99} 'c'
TTT Retrieve [1,99] {0:97,1:99}
SSSTSTTSTTSN Push 182 [1,99,182] {0:97,1:99}
TSST Subtract top two (99-182) [1,-83] {0:97,1:99}
NTSSN If 0: Jump to Label_EXIT [1] {0:97,1:99}
SSSTN Push 1 [1,1] {0:97,1:99}
TSSS Add top two (1+1) [2] {0:97,1:99}
NSNN Jump to Label_LOOP [2] {0:97,1:99}
SNS Duplicate top (2) [2,2] {0:97,1:99}
SNS Duplicate top (2) [2,2,2] {0:97,1:99}
TNTS Read STDIN as character [2,2] {0:97,1:99,2:98} 'b'
TTT Retrieve [2,98] {0:97,1:99,2:98}
SSSTSTTSTTSN Push 182 [2,98,182] {0:97,1:99,2:98}
TSST Subtract top two (98-182) [2,-84] {0:97,1:99,2:98}
NTSSN If 0: Jump to Label_EXIT [2] {0:97,1:99,2:98}
SSSTN Push 1 [2,1] {0:97,1:99,2:98}
TSSS Add top two (2+1) [3] {0:97,1:99,2:98}
NSNN Jump to Label_LOOP [3] {0:97,1:99,2:98}
SNS Duplicate top (3) [3,3] {0:97,1:99,2:98}
SNS Duplicate top (3) [3,3,3] {0:97,1:99,2:98}
TNTS Read STDIN as character [3,3] {0:97,1:99,2:98,3:182} '¶'
TTT Retrieve [3,182] {0:97,1:99,2:98,3:182}
SSSTSTTSTTSN Push 182 [3,182,182] {0:97,1:99,2:98,3:182}
TSST Subtract top two (182-182) [3,0] {0:97,1:99,2:98,3:182}
NTSSN If 0: Jump to Label_EXIT [3] {0:97,1:99,2:98,3:182}
NSSSN Create Label_EXIT [3] {0:97,1:99,2:98,3:182}
TNST Print as integer to STDOUT [] 3
error
Stops program with error: No exit defined.
every ASCII char counts! Even control characters.
Can you provide an example of what you mean ? – The random guy – 2018-05-28T13:07:29.023@Therandomguy here you are – Mega Man – 2018-05-28T13:10:17.933
Does
"you^Z^Z^Z"
mean that actual 9-byte printable-ASCII string, or does^Z
mean some control character and the string has length 6? – Luis Mendo – 2018-05-28T13:12:44.683^Z is meant like a backspace char, this rule doesn't seem to be good, because it is impossible on many languages (strange STDIN limitations) – Mega Man – 2018-05-28T13:14:24.443
1In that case, you should probably restrict to printable ASCII. "Most ASCII chars counts" is ambiguous – Luis Mendo – 2018-05-28T13:16:52.710
@LuisMendo sorry for all that chaos – Mega Man – 2018-05-28T13:17:35.750
Obviously most people don't like too easy challenges, but it's valid. (although we already have "add two numbers" and "is that number a prime") – user202729 – 2018-05-28T13:18:08.227
@user202729 I would consider this another reference challenge, like Hello, World! – Mega Man – 2018-05-28T13:21:02.327
13When the main difficulty is the input format or means, rather than the actual computations, it is usually a sign of a not very interesting challenge – Luis Mendo – 2018-05-28T13:22:51.280
1What about languages that cannot use STDIN? – Adám – 2018-05-28T13:26:49.673
@Adám I guess they can't compete in this challenge. A lot of challenges aren't accessible to some languages due to their restrictions. – Kevin Cruijssen – 2018-05-28T13:30:03.420
@Adám Which language can't use STDIN? – Mega Man – 2018-05-28T13:37:22.527
@MegaMan ECMAScript, in-browser JavaScript, TI-Basic, Operation Flashpoint scripting language, … – Adám – 2018-05-28T13:47:53.623
1
and all printable ASCII chars count!
what do non-printable ones? – l4m2 – 2018-05-28T14:44:42.207This is likely more-or-less trivial in essentially any language that can be used for the challenge (i.e., supports stdin), unless you specifically disallow the use of 'length' or 'shape' intrinsics. – Jeff Zeitlin – 2018-05-28T15:14:02.620
3It's not entirely clear how inputs should be taken, ie. given
""
on stdin is it an empty string, two characters or is it up to the language (in that case would"
be valid input)? – ბიმო – 2018-05-28T15:19:03.463@BMO - I have assumed that the quotes count and also that they can be in the string. If not then I half my byte count from 2 to 1 :-) It should really be clarified. – ElPedro – 2018-05-28T16:41:47.060
@BMO In my opinion it's by far the most natural to say the string is the "raw" contents of standard in. Taking source code as input doesn't make much sense for this challenge. – Jakob – 2018-06-05T02:24:29.550
I think I should rather close this question. – Mega Man – 2018-06-07T16:59:55.107