-3
\$\begingroup\$

Challenge: Create a program that prints out the size of its own source code file.

For example, a 35 bytes size of program must print out 35. Pre-calculation is not valid. What I mean by pre-calculation is:

 // test.js -> its size is 15 byte
 console.log(15)

It must dynamically calculate the size of own source code file. The winner is who prints the minimum number , same as who has the minimum size of code.

\$\endgroup\$
15
  • 1
    \$\begingroup\$ You should set some rules for cases like that so \$\endgroup\$ Jul 12, 2018 at 12:09
  • 2
    \$\begingroup\$ The program should dynamically calculate its own length then? You should mention such rules as part of the question (And have you seen the "Sandbox for Proposed Challenges"? You can post your questions there first and get help making them clearer and more likely to yield useful, interesting answers.) \$\endgroup\$
    – Sundar R
    Jul 12, 2018 at 12:11
  • 1
    \$\begingroup\$ Do you mean by reading the size of its source file or were you thinking of some other means? \$\endgroup\$
    – Neil
    Jul 12, 2018 at 12:14
  • 2
    \$\begingroup\$ Possible duplicate. \$\endgroup\$ Jul 12, 2018 at 12:28
  • 1
    \$\begingroup\$ Can the program contain the name of its own source file? \$\endgroup\$
    – ngm
    Jul 12, 2018 at 13:23

3 Answers 3

2
\$\begingroup\$

APL (Dyalog Unicode), 10 bytesSBCS

≢⊃⌽⎕NR⊃⎕SI

Try it online! Prints 11 because TIO's APL is set up to auto-format the code, but on a system where this isn't the case, it prints 10:

Session

⎕SIState Indicator (list of all functions on the stack)

 pick the first one (i.e. the currently running function)

⎕NRNested Representation (one character list per line)

 reverse (to bring the actual code line to the front)

 pick the first (i.e. the code line)

 tally the characters of that

\$\endgroup\$
0
\$\begingroup\$

Python 3, 32 bytes

Not much to say really, opens the file and checks the length of its contents as a string.

print(len([*open(__file__)][0]))

There might be something shorter but I couldn't think of it.

Edit: Try it Online! (stolen from the comments - thanks for the reminder!)

\$\endgroup\$
7
  • \$\begingroup\$ Was just typing these exact characters lol, you can remove one byte from print by switching to python2, provided it doesn't break the rest \$\endgroup\$
    – sagiksp
    Jul 12, 2018 at 12:30
  • \$\begingroup\$ I'm not 100% sure if [*x] works in Python 2 - I don't have an install to test it, but if it does work then yes, you could save one :) \$\endgroup\$ Jul 12, 2018 at 12:37
  • \$\begingroup\$ [*...] does not work in Python 2, but print len(open(__file__).read()) does for 32 bytes as well. \$\endgroup\$
    – Mr. Xcoder
    Jul 12, 2018 at 12:38
  • \$\begingroup\$ Try it online! \$\endgroup\$
    – Adám
    Jul 12, 2018 at 12:41
  • 1
    \$\begingroup\$ Making it work for all lines takes a fair few more bytes since you have to do something like print(sum(len(x)for x in[*open(__file__)])) instead, but the source is only one line so that isn't necessary (the question didn't say it had to adapt to additional source being added). Example \$\endgroup\$ Jul 13, 2018 at 8:17
0
\$\begingroup\$

JavaScript (Node.js), 40 20 bytes

-20 thanks to @Shaggy

f=_=>("f="+f).length

f=_=>("f="+f).length

alert(f())

\$\endgroup\$
3
  • 1
    \$\begingroup\$ f=_=>("f="+f).length \$\endgroup\$
    – Shaggy
    Jul 12, 2018 at 14:35
  • \$\begingroup\$ ... Because this is valid for normal quine challenge, it's invalid here? ... Also why not just 2+? \$\endgroup\$
    – DELETE_ME
    Jul 13, 2018 at 3:52
  • \$\begingroup\$ @user202729 but 2+(''+f).length do not save a byte \$\endgroup\$
    – tsh
    Jul 13, 2018 at 9:51

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