# New Code-Golf metric: Atomic-Code-Golf

I posted this on the Puzzle Lab chat, but it didn't get much response (except for Gareth...thanks Gareth!). Thus, I'm posting it here. Please feel free to chat on the Puzzle Lab if you have input that doesn't fit well here.

Hello Puzzle Lab Meta-People, in order to level the code-golf playing field for verbose languages, I've been thinking of introducing a new branch of code-golf called atomic-code-golf. Here, atomic means "Of or forming a single irreducible unit or component of code." Thus, the "atomic-code-golf" score of a piece of code is the sum of its atomic pieces. In such cases where the atomic-code-golf score is a tie, then we fall back to the code-golf score to determine the winner.

For a simple example, take a line of the Java Hello World code:

System.out.println("Hello World!");//35 points


In this code System, out, println, and each character in the string "Hello World!" would all be atomic components of the code, and would thus count as 1 point. In contrast, . and ; serve as separaters of atomic components, and would thus not count. Thus, the code might be counted as:

Sop("Hello World!")//19 points


Now, some may argue that ", (, and ) are groupers of atomic components, and so they should not count either. Thus, here are my broad level questions:

1. Would people like this kind of contest?
2. What should an atomic component be defined as?
• These are components that will be counted as 1 point even if it takes more than that to write them.
• Example: Object, keyword, and variable names such as System, class, and myInt in Java code.
3. What should a non non-atomic component be defined as?
• These are components that will be counted as 0 points even though you need them to make the program compile.
• Example: Separators such as ;, ., spaces, tabs, and newlines in Java code
4. What are the language-agnostic regular expressions we will need to atomic-code-golf score a language? Initially, I'm thinking we'll need the following:

• What is the regular expression that will match any component that should be counted at full length (such as a String)? For example, in the example of a String in Java, this should handle the string "\"" where \" is escaped. Any segment of code matching this regular expression will be counted at its full normal code-golf score.
• Without considering matches from the first case, what is the regular expression of an atomic component (such as a variable name, Object name, keyword, number, etc.)? Each match on this regular expression will count as 1 point no matter how many characters are used.
• Again without considering matches from the first case, what is the regular expression for a non-atomic character (such as ;, ,, etc.)? These will be counted as 0 when scoring a piece of code.
• All remaining code-segments that do not match the above regular expressions will be counted at their full normal code-golf scores.
5. What are the above regular expressions in your favorite language? I, or anyone else who wishes, will strive to keep an updated alphabetical list of the regular expressions to be used for each language in the Regular Expressions section of this question. These will be revised as necessary to make the counts fair across all languages.

Once we've answered the above questions, I think the first question needs to be to write an atomic-code-golfed piece of code that figures out the atomic-code-golf score of a program in any language. The input for this program will be the code in question and the regular expressions needed to compute that code's atomic-code-golf score. The output will be the code's atomic-code-golf and code-golf scores (for the tie-breaker). An additional requirement will be that this needs to be accessible online so that future atomic-code-golf scores can be computed using this program.

Please let me know what you think, and any input is greatly appreciated!!!

## Online scorer

Here is a tool to generate your atomic code-golf score. This will be updated/revised as more regular expressions are implemented and revised:

Currently Supports: GolfScript, JavaScript

## Regular Expressions:

GolfScript:

1. Full Length Code:
• RegExp: /("((\\")|([^"]))*")/
• Matches on Strings: "test" or "\"test\""
2. Atomic Component:
• RegExp: /and|or|xor|print|puts|rand|do|while|until|if|abs|zip|base/
• Matches on reserved words: and or xor print puts rand do while until if abs zip base
3. Non-Atomic Component:
• none

JavaScript:

1. Full Length Code:
• RegExp: /("((\\")|([^"]))*")|('((\\')|([^']))*')/
• Matches on Strings: "test", "\"test\"", 'test', '\'test\''
2. Atomic Component:
• RegExp: /[a-zA-Z_$][0-9a-zA-Z_$]*/
• Matches on variable/object names: var, myVarName, Math, Array
3. Non-Atomic:
• RegExp: /;|\.|\s|,/
• Matches on separators: period, comma, semicolon, space, newline, tab
• It's a very interesting question, and certainly does level the playing field for verbose languages like Java and Scheme. (Or even Python, with its indentation-sensitive syntax---I guess all the indentation would just be compressed to 1 point each?) I will come back to this question with a better-thought-out response later on. Aug 25 '11 at 15:52
• I was thinking of something like this a while back. I think it would be interesting if you can get a system that works. Many Python modules such as itertools and collections are pretty much out for regular golf. +1 hope you can find a way to make it work out that's fair Aug 25 '11 at 19:26
• So roughly speaking you're after lexing the code and counting tokens (ignoring a selected set of them)? Aug 25 '11 at 19:32
• @Peter Taylor: Yep, that's the end goal. My hope to get there is for power users to collaborate on defining rules for how to tokenize and count the code both in general and specifically in their favorite language(s). In the general case I think it would be mentally useful to have the tokenization-rules in plain-english, and for the language-specific cases it will be programatically useful to have the tokenization-rules as regular expressions. Aug 25 '11 at 19:59
• If the goal is to "level the playing field," I feel like there should be some kind of reference point we can use, such as some example programs in different languages that you feel should score roughly the same. Then maybe we can analyze those and look for patterns. Aug 26 '11 at 5:45
• @migimaru: Nice idea! I've started a Test-Box as one of the answers for keeping track of common code components across languages. Please feel free to update both the components and the language cases. Aug 26 '11 at 13:41
• I guess this won't help brainfuck. Jan 26 '14 at 17:27

# Test-Box For Common Language Components (Please edit freely)

Based on @migimaru's comment, the purpose of this spot is to gather items across code that should have the same or similar code scores.

1. Variable Declarations:

• Java
<type> test;

• JavaScript
var a; private variable
b; global variable

2. Operators

• Arithmetic: Addition, Subtraction, Multiplication, Division
+, -, *, /: JavaScript, Java

• Logic: AND, OR, NOT
&&, ||, !: JavaScript, Java

3. For Loop (loop 10 times):

• Java, JavaScript
for(..;..;..)..; Required minimum syntax
for(..;..;..){..};
4. While Loops:

• Java, JavaScript
while(..)..;
while(..){..}
5. If statements:

• Java, JavaScript
if(..)..;
if(..){..}
• Generally speaking you would want check the 'community wiki' box next to your answer rather than saying (Please edit freely) in the title so as to exclude less people from editing. Sep 22 '11 at 15:32
• @mootinator: Sorry about that, should be checked now. Sep 22 '11 at 15:59

In Scala, you may express a left fold like this:

(a /: b)(_ c _)


A typical example would be

(0 /: List (3, 5, 9)) (_ + _)


with the result

res0: Int = 17


better readable for beginners in the form:

(0 /: List (3, 5, 9)) ((a, b) => a + b)


but since a and b are only used once in the right part in the order they're comming in, they don't need names.

Translated to Java, it would look like that:

int cnt = 0;
for (int i : new int [] {3, 5, 9}) {
cnt += i;
}
return cnt;


So the token count would be:

    (0 /: List(3, 5, 9)(_ + _)
12 3  4   567 89 abcd e fg with 1,5,7,9,b,c,g being
// a kind of grouping delimiters; compared to:

int cnt = 0;
//1 2   3 45 // g-delims (5)=1
for (int i : new int [] {3, 5, 9}) {
//1 23   4 5 6   7   8  9ab cd efg h (g: 259bdfg=7)
cnt += i;
//1 2  34  (=1)
}
// 1 = 1
return cnt;
// 1   2  3 gdelim:1


So I count 30(-10=20) tokens against 16(-7=9).

So there are places, where java will still not compete, but what my main concern is, is, that the structure of the languages don't always fit. I'm not sure if everybody would count the tokens in the same way in Scala, and how to reach that goal.

• I've been thinking that it is important to include grouping characters in the counts, because grouping in different ways affects functionality and code output. Thus, the scores above would be 10 for Scala and 25 for Java above. Also, wouldn't you need List (3, 5, 9) in place of b, bringing the Scala total to 15? Scala definitely has an advantage here, but I think that is warranted because it has implemented the concept of the fold. If Java had that, you could do int i = Arrays.fold(new int [] {3, 5, 9}); for a score of 16, which would bring the scores much closer. Sep 22 '11 at 13:54
• Yes, you're right, I will correct the numbers in the example and the summary. Sep 23 '11 at 2:26