g | x | w | all
Bytes Lang Time Link
nanPython 3180323T233044ZJonathan
nanPython 2180323T181253ZRod
nanJava 10180325T161505ZKevin Cr
nan05AB1E180326T144543ZMagic Oc
nanJavaScript ES6180323T183328ZArnauld
nanPHP180328T032636ZTitus
nanC gcc180325T073511Zgastropn
nansed180325T042742Zlucasb
nanJelly180324T211021ZJonathan
nanPerl 6180324T182414Znwellnho
nanJelly180323T204325ZDennis
nanCharcoal180324T005043ZNeil
nanR180323T190043ZAndre
nanAutoHotkey180323T232632Znelsontr
156MATL180323T192552ZLuis Men
nanRuby180323T205911Zbenj2240
9514Perl 5 with pF180323T183948ZDom Hast
nanPython 3180323T200215ZDat
nanSOGL V0.12180323T193018Zdzaima
nanPerl 5 p180323T192200ZTon Hosp
176PHP180323T184909ZDave

Python 3, 132 bytes + 51 matches = 183

-5 thanks to Lucenaposition (use of mathematical italics: +9 bytes; -14 matches).

lambda w,m='',t='',k=𝑜r𝑑:𝑠um(~-k((m+t+m+''+t)[k(v)%77])*('/'<v<'{')for v in w)

An unnamed function accepting a string and returning an integer.

Contains many non-printable characters (specifically bytes one to eight).

Try it online!

Python 2, 97 bytes + 237 matches = 334

lambda w:sum(map(('1ir'*2+'7lnu'*3+'4cfhjoy'*4+'235bdegpqsxz'*5+'069a'*6+'8'*7).count,w.lower()))

Try it online!

This works by generating a string where each buildable character appears the same amount of matchsticks required to build that character

Java 10, 452 432 416 404 score (145 bytes + 259 matches)

k->{int m=0,t;for(var w:k)for(t=m-m;t<'';)m+="1ir 7lnu 4cfhjoy 235bdegpqsxz 069a 8".split(" ")[t++].contains((char)(w|32)+"")?-~t:t-t;return m;}

Explanation:

Try it online.

k->{                   // Method with character-array parameter and int return-type
  int m=0,             //  Result-integer, starting at 0
      t;               //  Index-integer
  for(var w:k)         //  Loop over the input String-array
    for(t=m-m;t<'';)   //   Inner loop over the parts array with index [0,6)
      m+="1ir 7lnu 4cfhjoy 235bdegpqsxz 069a 8".split(" ")[t++]
                       //     If the current part ordered by amount of matches (2-8)
         .contains((char)(w|32)+"")?
                       //     contains the current (lowercase) letter
          -~t          //      Increase the result by the index + 2
         :             //     Else:
          t-t;         //      The result remains the same by adding 0
  return m;}           //  Return the result

05AB1E, 30 bytes + 70 matches = 100 score

žLÃlv•Ƶγ¤и©X´≠Ø&c3Íε畞hA«ykèO

Try it online!


-5 thanks to Emgina

JavaScript (ES6), 198 (102 bytes + 96 matchsticks)

Saved 5 points thanks to @l4m2

v=>[...v].map(w=>t+=('{w__^_{]|{{_^__^_^w^~]~]^__w_~]~~_^_'[parseInt(w,36)]+v).charCodeAt()%9,t=+[])|t

Try it online!

How?

We use the ASCII code modulo 9 of characters that do not add any penalty to encode the numbers of matchsticks.

 char. | code | modulo 9
-------+------+-----------------
   ~   |  126 |   0
   v   |  118 |   1  (not used)
   w   |  119 |   2
   ]   |   93 |   3
   ^   |   94 |   4
   _   |   95 |   5
   {   |  123 |   6
   |   |  124 |   7

We do not have to worry about the case because parseInt() is case-insensitive.

For characters that do not match [0-9A-Za-z], parseInt() returns NaN and the string lookup results in undefined. Once coerced to a string, "undefined".charCodeAt() returns the ASCII code of "u", which is 117. Conveniently, 117 modulo 9 gives 0 as expected.

PHP, 98+253=351

for(;$k=ord($argn[$v++]);)$m+=$k>64?_65455454240303455250300545[$k&31]:_6255456376[$k-47];print$m;

Run as pipe with -nr or try it online.

C (gcc), 134 bytes + 38 matchsticks = 172

v(t,w)char*t;{t=(w=*t++)?v(t)+(isalnum(w|=w>'@'&w<'['?' ':!w)?")%(('()&*))('(('('%'#&#&'((%(#&##('("[w-=w>'`'?'W':'_'-'/']-'#':!w):w;}

Try it online!

sed, 367 (source code bytes) + 532 (amount of matchsticks for the source code) = 899

s/[^0-9a-jln-suxyz]//Ig;/^$/{s/.*/0/;b};s/.+/&; %1ir %%7lnu %%%4cfhjoy %%%%235bdegpqsxz %%%%%069a %%%%%%8/;:1;s/([^% ])(.+ (%+)[^ ]*\1)/%\3 \2/I;/ ;/!b1;s/;.+//;s/^/,;/;:2;s/(;[^%]*)(%+)/\2\1/;:3;s/,%{10}/%,/;s/^%/,&/;/%{10}/b3;/;.*%/b2;:4;s/,[;,]/,0,/;/,[;,]/b4;s/%{9}/9/g;s/%{8}/8/g;s/%{7}/7/g;s/%{6}/6/g;s/%{5}/5/g;s/%%%%/4/g;s/%%%/3/g;s/%%/2/g;s/%/1/g;s/[^0-9]//g

Try it Online

Multi-line version:

s/[^0-9a-jln-suxyz]//Ig
/^$/{s/.*/0/;b}
s/.+/&; %1ir %%7lnu %%%4cfhjoy %%%%235bdegpqsxz %%%%%069a %%%%%%8/
:1
s/([^% ])(.+ (%+)[^ ]*\1)/%\3 \2/I
/ ;/!b1
s/;.+//
s/^/,;/
:2
s/(;[^%]*)(%+)/\2\1/
:3
s/,%{10}/%,/
s/^%/,&/
/%{10}/b3
/;.*%/b2
:4
s/,[;,]/,0,/
/,[;,]/b4
s/%{9}/9/g
s/%{8}/8/g
s/%{7}/7/g
s/%{6}/6/g
s/%{5}/5/g
s/%%%%/4/g
s/%%%/3/g
s/%%/2/g
s/%/1/g
s/[^0-9]//g

Explanation:

The above script reads standard input line by line (into the pattern space -- the usual "sed way") and, for each line, it outputs the amount of matchsticks necessary to represent all the matchstick-representable characters in that line. The computations for each line of input happen as follows:


s/[^0-9a-jln-suxyz]//Ig

First, we remove every character for which we don't have a corresponding matchstick representation (as given on the question) from the pattern space. That is, we remove every character which isn't either a numeral from "0" to "9", a letter from "a" to "j", "n" to "s", "l", "u", "x", "y" or "z". Uppercase and lowercase letters are treated the same.

/^$/{s/.*/0/;b}

If we end up with an empty pattern space, we print 0 (automatically followed by a newline, like sed always does unless you pass a special flag to it), skip all posterior lines of the script and proceed into next "sed cycle" (i.e., read the next line of input and repeat processing all over again from the first command until there are no more lines of input to be processed).

s/.+/&; %1ir %%7lnu %%%4cfhjoy %%%%235bdegpqsxz %%%%%069a %%%%%%8/

Otherwise, if the pattern space is not empty, we now divide it into two "sub-spaces" separated by a semicolon: first comes the input space, which is initially formed by all characters which weren't removed from the pattern space after the execution of line 1; next comes the semicolon, and after it the map space.

The map space tells us how many matchsticks beside 1 are needed to represent each relevant alphanumeric character. If we want to know how many matchsticks are necessary to represent any alphanumeric character in the map space, we look for the first sequence of contiguous %'s on the left of that character, and the answer will be the number of %'s in that sequence plus 1. So, for example, the number of matchsticks necessary to represent a "b" is 4 + 1 = 5; to represent a "4", 3 + 1 = 4, to represent a "y", 3 + 1 = 4; and so on.

:1
s/([^% ])(.+ (%+)[^ ]*\1)/%\3 \2/I
/ ;/!b1

This is a loop. Now we will replace every character in the input space by the (complete) sequence of %'s whose number indicates the necessary amount of matchsticks to represent that character, and follow that sequence by a white space character (again, uppercase and lowercase letters are given the same treatment). The criterion to determine whether the loop should end is to check whether there's a white space character on the immediate left of the semicolon in the pattern space: if that condition holds, we terminate the loop and continue into the next line.

s/;.+//
s/^/,;/

Those two lines remove the semicolon and everything after it from the pattern space and then insert a comma and a semicolon into the beginning of the pattern space. We now have the pattern space divided once again into two new sub-spaces: the analog result space before the semicolon, and the analog input space after it.

The analog input space is just what we have previously called the "input space", but in a different form: it now contains sequences of %'s separated by white space. The total number of such %'s in the analog input space is the same number of matchsticks necessary to represent the initial input character string, i.e., that number is the result. But we must print that result in decimal notation, not as a sequence of percent signs. The purpose of the analog result space is to hold an analog representation of each digit of the result while we compute that result by summing each contiguous sequence of %'s in the analog input space one by one. The next loop performs that sum:

:2
s/(;[^%]*)(%+)/\2\1/
    :3
    s/,%{10}/%,/
    s/^%/,&/
    /%{10}/b3
/;.*%/b2
  1. First, after label 2, we move the next contiguous sequence of %'s after the semicolon from the analog input space into the immediate left of the semicolon, in the analog result space;

  2. Next, we step into a sub-loop (label 3) which performs the following computations:

    • If there's a contiguous sequence of ten %'s after a comma in the analog result space, we remove those %'s and put a single % immediately at the left of the comma. To put it simply, this indicates that one of the decimal places in the result has acquired more than 9 units, so we take 10 units away from that decimal place and add 1 unit to the next larger decimal place;

    • If a "%" is the first character in the pattern space, we insert a new comma immediately before it. This indicates that the sum has reached a value whose decimal representation has one more decimal place on the left than the previous value;

    • If there are still any contiguous sequence of ten %'s in the analog result space, we go back to label 3 and repeat this process. Otherwise, we exit this sub-loop and step into the next line.

  3. Now, if there is still any "%" in the analog input space (i.e., after the semicolon), it means that there is still some number of matchsticks to be added to the total sum -- so we go back to label 2.

Once the sum is complete, we step into the final loop of the code:

:4
s/,[;,]/,0,/
/,[;,]/b4

Here, we check every pair of characters formed by a comma on the left and either a semicolon or a comma on the right. We replace all such pairs of characters by a "0" inside two commas.

s/%{9}/9/g
s/%{8}/8/g
s/%{7}/7/g
s/%{6}/6/g
s/%{5}/5/g
s/%%%%/4/g
s/%%%/3/g
s/%%/2/g
s/%/1/g

The above piece of code is quite simple: we replace each contiguous sequence of %'s in the analog result space by a decimal digit character which corresponds to the number of %'s in each particular sequence.

s/[^0-9]//g

Finally, we remove every non-numeral character from the pattern space and what remains is the final result in the familiar decimal notation. That value is printed on standard output and the next sed cycle begins, if there are any more input lines to be processed.

Jelly, 34 bytes + 3 matches = 37

⁽[ɱד®-&]Ṙṙ£⁺ÐĊṂⱮɼ ’‘ḤṚæ.ŒlċЀØWṚƊ

A monadic link accepting a list of characters and returning an integer.

Try it online!

How?

Works in a similar way to Dennis' Jelly answer but took enough effort that I feel it warrants another answer. The core difference is that it lower-cases the input for a cost of three matches (Œl contains an l) which then allows for a much smaller number to be used to create the cost array. The tricky bit was finding a way to construct that number without matches while staying concise.

ØW yields "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_" so counting lower-cased input occurrences always starts with 26 zeros. We can reverse this and perform a dot-product with an array of length 37 instead of one of length 63.

⁽[ɱד®-&]Ṙṙ£⁺ÐĊṂⱮɼ ’‘ḤṚæ.ŒlċЀØWṚƊ - Link: list of characters
⁽[ɱ                                - literal                                     23913
    “®-&]Ṙṙ£⁺ÐĊṂⱮɼ ’               - literal         136861653160003879166772353166783
   ×                               - multiply  = 3272772712015172762515027281277281879
                    ‘              - increment = 3272772712015172762515027281277281880
                     Ḥ             - double    = 6545545424030345525030054562554563760
                      Ṛ            - reverse (implicit decimal list) -> [0,6,7,3,6,5,4,5,5,2,6,5,4,5,0,0,3,0,5,2,5,5,4,3,0,3,0,4,2,4,5,4,5,5,4,5,6]
                                   -                     (to align with: _ 9 8 7 6 5 4 3 2 1 0 z y x w v u t s r q p o n m l k j i h g f e d c b a)
                                 Ɗ - last four links as a monad:
                         Œl        -   lower-case (the input)
                              ØW   -   word -> "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_"
                            Ѐ     -   map across right:
                           ċ       -     count (right character in lower-cased input)
                                Ṛ  -   reverse (to align with the values as shown above)
                       æ.          - dot-product

Perl 6, 87 bytes + 26 matchsticks = 113

{+[+] (~'򘮉򛫡񯌞𺪯񯉒񉘁'.ords~~m:g/\w/)[m:g/<[/..{]-[\W_]>/>>.&{:٣٦(~$_)}]}

Try it online!

Uses some non-ASCII Unicode characters. The lookup table is encoded in a Unicode string:

say '򘮉򛫡񯌞𺪯񯉒񉘁'.ords;
# (625545 637665 455454 240303 455250 300545)

Characters are converted to indices with base-36 conversion using Arabic-Indic numerals:

:٣٦('z'); # is equivalent to
:36('z');

Jelly, 42 bytes + 0 matchsticks = 42 points

“ṄḟṭkɗØæ⁶ṡ“£ƈṠ¤żȥṬ}ė$t¿ƬⱮȷ|çƓ’×/ṚṚæ.ċЀØW$

Thanks to @JonathanAllan for -2 points!

Try it online!

How it works

“ṄḟṭkɗØæ⁶ṡ“£ƈṠ¤żȥṬ}ė$t¿ƬⱮȷ|çƓ’

Literals that begin with and end with split on , replace the remaining characters with their 1-based indices in Jelly's code page, then convert from bijective base 250 to integer.

This particular literal encodes

[3096734725226860846495, 211369264881118657055472842435156679693648].

×/ reduces by multiplication, yielding

654554542403034552503005456545545424030345525030054562554563760

(Encoding this integer directly would save 6 bytes, but cost 28 matchsticks.)

ṚṚ reverses twice; the first call promotes an integer to its digit array. This yields

[6, 5, 4, 5, 5, 4, 5, 4, 2, 4, 0, 3, 0, 3, 4, 5, 5, 2, 5, 0, 3, 0, 0, 5, 4, 5, 6, 5, 4, 5, 5, 4, 5, 4, 2, 4, 0, 3, 0, 3, 4, 5, 5, 2, 5, 0, 3, 0, 0, 5, 4, 5, 6, 2, 5, 5, 4, 5, 6, 3, 7, 6, 0]

ċЀØW$ counts (ċ) the occurrences of each (Ѐ) character of "A...Za...z0...9_" (ØW) in the input string.

Finally æ. takes the dot product, multiplying each character count by the corresponding cost in matchsticks, then taking the sum.

Charcoal, 48 bytes + 3 = 51

IΣEθ⎇№α↥ιI§”)➙⊞←!Σw➙⊙↘⁴↘”⌕α↥ι∧№IX²φιI§”)⧴u↑$◨”Iι

Try it online! Link is to verbose version of code. Explanation:

IΣEθ

Loop over the characters of the input, calculating the number of matchsticks of each, then sum the result, convert to string, and implicitly print.

⎇№α↥ι

If it's a letter...

I§”)➙⊞←!Σw➙⊙↘⁴↘”⌕α↥ι

Look up the number of matchsticks in the compressed string 65455454240303455250300545.

∧№IX²φι

Else if it appears in the value of 2 ** 1000...

I§”)⧴u↑$◨”Iι

Look up the number of matchsticks in the compressed string 6255456376.

R, 112 bytes + 319 matches = 431 score

sum(strtoi(el(strsplit(chartr("0-9a-z","625545637665455454240303455250300545",tolower(scan(,""))),""))),na.rm=T)

Try it online!

Kudos to Giuseppe who came up with this improvement.

Old version, 143 bytes + 454 matches = 597 score

b=el(strsplit(chartr("0123456789abcdefghijlnopqrsuxyz","6255456376654554542433455253545",tolower(readline())),""))
sum(as.numeric(b[b%in%0:9]))

To make the el() function work on TIO, you need to use library(methods).

Gosh dang it, is R verbose!

AutoHotkey, 148 bytes + 345 matchsticks = 493

This one was a bit of a challenge to shorten.

n:=["1ir","7lnu","4cfhjoy","235bdegpqsxz","09a5",7+1]
Loop Parse,i
{r:=A_LoopField
Loop % n.Length()
{l:=A_Index
if InStr(n[l],r)
u+=l+1
}}
send % u

MATL, score 166 156

Code length 41 + output for code used as input 115.

'%p#21jgyDT7o?pe}['TIW:qZajk4Y22Y2h&mXz)s

Try it online!

Ruby, 125 bytes + 87 matchsticks = 212

->m{v=->w,k{w.scan(/./).map &k};v[m,->t{v["!#&&;&=$(==&;&&;&;#;!$!$;&&#&!$!!&;&",->k{k.ord%11}][t.to_i 36].to_i+(t==?0?6:0)}].sum}

Try it online!

Heavily inspired by Arnauld's Javascript answer.

Normally the cost of declaring a lambda to be used only twice is not worth it, but the matchstick weight of "scanap" in .scan(/./).map changed that. This was a fun challenge!

->m{
  v=->w,k{w.scan(/./).map &k};                # v is a lambda taking a string and a block
  v[m,->t{                                    # Transform each char t of the input:
    v["!#&&;&=$(==&;&&;&;#;!$!$;&&#&!$!!&;&", # Transform each char of this magic string
      ->k{k.ord%11}                           #  into a matchstick count ([0,2,5,5,4,5...])
    ][t.to_i 36]+                             # Parse t as a base 36 index into the counts
    (t==?0?6:0)                               # If t was "0", add 6 matchsticks
  }].sum                                      # Add the counts for all characters
}

Perl 5 with -pF, 95 bytes + 14, 109

eval~"Û£Ô„…ÊÏÉÎÍÍÊÌÊËËÊÊÉÉÈÌÇÈÆÉžÉʜ˛ʚʙ˘ʗ˖͕˓̑ÌËÊŽÊ͌ʊ̇ʆËÂÐÑИ‚ÒÁ„Ô“œ‚™¿¹"}{

This is equivalent to:

$\+={z506122535445566738796a6b5c4d5e5f4g5h4i2j4l3n3o4p5q5r2s5u3x5y4=~/./g}->{+lc}for@F

but using the ~ operator we can use high-byte characters and avoid a lot of characters without really sacrificing bytes.

Still quite a way off Ton's score, even with assistance!

Try it online!

This solution contains unprintables so here's a reversible hex dump to verify the byte-count:

00000000: 6576 616c 7e22 dba3 d4c2 8485 cacf c9ce  eval~"..........
00000010: cdcd cacc cacb cbca cac9 c9c8 ccc7 c8c6  ................
00000020: c99e c99d ca9c cb9b ca9a ca99 cb98 ca97  ................
00000030: cb96 cd95 cb93 cc91 cc90 cb8f ca8e ca8d  ................
00000040: cd8c ca8a cc87 ca86 cbc2 81d0 d1d0 9882  ................
00000050: d2c1 84d4 939c 8299 908d bfb9 227d 7b    ............"}{

Python 3, 138 + 265 = 403 bytes

k='0123456789abcdefghijlnopqrsuxyz'
m=lambda t:sum([ord(''[k.index(v)])if v in k else 0for v in t.lower()])

Try it online!

SOGL V0.12, 34 bytes + 18 = 52

ā⁄,u{²²z+;W"⁾o∫k9÷│>Γ{σ¬μ┼“'=υ─wι+

Try it Here!

Golfed, but not scoring as much:

0,u{²²z+;W"⁾o∫k9÷│>Γ{σ¬μ┼“8─wι+

Perl 5 -p, 90 64 code + 9 eval harness + 14 matchsticks = 87

Replace the hex codes by their literal 1 byte variant (not UTF-8 as TIO tries to) for the claimed score

eval~"\xdb\xa3\xd4\xc2\x86\xd0\xcf\xd2\xc6\x9e\xd2\x85\xd0\xc9\xcd\xca\xca\xcb\xca\xc9\xcc\xc8\xc9\xc9\xca\xcb\xca\xca\xcb\xca\xcb\xcd\xcb\xcf\xcc\xcf\xcc\xcb\xca\xca\xcd\xca\xcf\xcc\xcf\xcf\xca\xcb\xca\xd0\x8d\x99\x90\x8d\xdf\x93\x9c\xc2\x81\xd0\xd1\xc0\xd0\x98"}{

Try it online!

Code Inside the complemented string:

$\+=y/0-9a-z/625545637665455454240303455250300545/rfor lc=~/.?/g

PHP 176 bytes = 397 score

<?$n="6255456376";$l="065455454240303455250300545";$a=strtolower($argv[1]);for($i=0;$i<strlen($a);$i++)$t+=(is_numeric($a[$i])?$n[$a[$i]]:$l[max((ord($a[$i])-96),0)]);echo$t;

Try it online!