# Count the Matchsticks

Most people here are familiar with seven segment displays, which are also used in matchstick puzzles. Below are the digits 0 through 9 and letters a through z, except k,m,t,v,w, written in this format.

 _        _   _         _    _    _    _    _
| |   |   _|  _|  |_|  |_   |_     |  |_|  |_|
|_|   |  |_   _|    |   _|  |_|    |  |_|   _|

_       _      _  _  _                        _   _      _               _
|_| |_  |   _| |_ |_ |   |_  |   | |   _   _  |_| |_|  _ |_      |_| |_|  _|
| | |_| |_ |_| |_ |  |_| | | | |_| |_ | | |_| |     | |   _| |_| | |   | |_


The challenge here is simple. Given an input string, output the number of matchsticks required to represent that string. If the string contains a character outside of the above representation, ignore it (count it as 0).

For example, for input 53, a total of 10 matchsticks are required, 5 for the 5 and 5 for the 3, so the output is 10.

For input hello a total of 19 matchsticks are required, h (4), e (5), l (3), l (3), o (4), so the output is 19.

For clarity, here are the matchsticks required to build each character:

0 -> 6
1 -> 2
2 -> 5
3 -> 5
4 -> 4
5 -> 5
6 -> 6
7 -> 3
8 -> 7
9 -> 6
a -> 6
b -> 5
c -> 4
d -> 5
e -> 5
f -> 4
g -> 5
h -> 4
i -> 2
j -> 4
l -> 3
n -> 3
o -> 4
p -> 5
q -> 5
r -> 2
s -> 5
u -> 3
x -> 5
y -> 4
z -> 5


Now for the twist, and there are two of them.

• The first is that the input is considered case-insensitive. That is, A and a should both count for 6 matchsticks, even though the visual representation looks like an uppercase A.
• Your score is your source code run through this algorithm, plus the length of your source code in bytes, lower is better. For example, if your source code was abc123, your score would be 6+5+4+2+5+5 = 27 + 6 = 33. If your source code was #&@()*, your score would be 0 + 6 = 6.

### Input/Output Examples

0    -> 6
53   -> 10
111  -> 6
112  -> 9
8888 -> 28
hello -> 19
PPCG  -> 19
Programming Puzzles & Code Golf -> 99
#&()mt!!~ -> 0
*DḌƤÆE%Ḅċ0 -> 16


## Rules

• If applicable, you can assume that the input/output will fit in your language's native Integer type.
• The input and output can be given by any convenient method.
• Either a full program or a function are acceptable. If a function, you can return the output rather than printing it.
• Standard loopholes are forbidden.
• T is commonly written as |_\n|_ (lowercase t) – 12Me21 Mar 23 '18 at 17:58
• @12Me21 I had contemplated doing something like that, but I didn't feel it looked close enough to the letter, so I left it out. – AdmBorkBork Mar 23 '18 at 18:00
• What about your letter X (H)? (well, I it doesn't matter now that there's an answer.) – 12Me21 Mar 23 '18 at 18:17
• For chars outside [0-9a-z], should we count 0 matchsticks? That's what I understand from Your score is your source code run through this algorithm, plus the length of your source code in bytes. – Erik the Outgolfer Mar 23 '18 at 19:08
• @EriktheOutgolfer Yes, that's correct. – AdmBorkBork Mar 23 '18 at 19:14

# 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

• One character can be saved with +'069a8'*6+'8') instead of +'069a'*6+'8'*7). – xbarbie Apr 4 '18 at 8:09
• @xbarbie indeed, but that would rise the match count, resulting in +3 points – Rod Apr 4 '18 at 11:05

## 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 ............"}{  • Why the }{ ? Drop it and the -n option. Also you keep forgeting +lc instead of lc() :-) – Ton Hospel Mar 23 '18 at 19:36 • @TonHospel Goddammit! I even looked through both our posts and I couldn't remember what post I'd seen it on and + doesn't immediately spring to mind! I need n on my machine and forget I can drop it! – Dom Hastings Mar 23 '18 at 19:46 • Sorry, I misspoke. I meant "drop the }{ and -p" (and replace it by -n if your perl still needs it. It nowadays doesn't count anyways) – Ton Hospel Mar 23 '18 at 19:54 • @TonHospel I hadn't updated my code to the latest, oops... – Dom Hastings Mar 23 '18 at 20:34 # 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. • v=>[...v].map(w=>t+='~{w__^_{]|{{_^__^_^w^~]~]^__w_~]~~_^_'.charCodeAt(1+parseInt(w,36))%9,t=+[])|t 1 less – l4m2 Mar 28 '18 at 18:42 # 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. • Use a factorisation of the big number to save one byte and base decompression and modulo instead of D to save another. Try it online – Jonathan Allan Mar 23 '18 at 21:45 • byte ...err point :) – Jonathan Allan Mar 23 '18 at 22:00 • And one more with Ɗ. Thanks! – Dennis Mar 23 '18 at 23:52 # 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


# 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)
Œ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


# 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!

• Welcome to PPCG! – AdmBorkBork Mar 23 '18 at 18:53

# SOGL V0.12, 34 bytes + 18 = 52

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


Try it Here!

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


# 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!

# 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
}


# MATL, score 166 156

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

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


Try it online!

# 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!

• TIO, for whatever reason, doesn't automatically load the methods package, but since it's a base package, I've always shoved it in the header and don't count it for the byte-count. readline also won't work on TIO since it's not an interactive session. This is definitely golfable, though. – Giuseppe Mar 23 '18 at 20:05
• 112 bytes + 319 penalty = 431 score – Giuseppe Mar 23 '18 at 20:09
• ^ needs quotes around input containing spaces, though. – Giuseppe Mar 23 '18 at 20:10
• Also, I just started a chatroom for R golfing!! I've seen a lot of answers in R lately by a lot of different users, which is quite encouraging :) – Giuseppe Mar 23 '18 at 20:19
• @Giuseppe I think you should post your answer separately. It is different in principle and much superior to mine. – Andreï Kostyrka Mar 23 '18 at 21:26

{+[+] (~'򘮉򛫡񯌞𺪯񯉒񉘁'.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');  # 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.

# 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!

# 05AB1E, 30 bytes + 70 matches = 100 score

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


Try it online!

-5 thanks to Emgina

• Al = A. You also don't need the }. – Emigna Mar 29 '18 at 8:29

# Java 10, 452432416 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

• Variable names $ραετ are used instead of letters. EDIT: Variable names kmtvw are now used instead, because they can't be formed by matches according to the challenge description. • '' (unprintable) is used instead of 6. • m-m and t-t are used instead of 0. • (char)(w|32)+"" with character-array input used instead of w.toLowerCase() with String-array input. # 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  # Python 3, 123 bytes + 65 matches = 188 lambda w,m='',t='',k=ord:sum(~-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! # Charcoal, 48 bytes + 3 = 51 ＩΣＥθ⎇№α↥ιＩ§”)➙⊞←!Σw➙⊙↘⁴↘”⌕α↥ι∧№ＩＸ²φιＩ§”)⧴u↑$◨”Ｉι


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

ＩΣＥθ


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...

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


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

∧№ＩＸ²φι


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

Ｉ§”)⧴u↑$◨”Ｉι  Look up the number of matchsticks in the compressed string 6255456376. # 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.