| Bytes | Lang | Time | Link |
|---|---|---|---|
| 091 | AWK | 250303T192801Z | xrs |
| 020 | Vyxal rs | 230829T191704Z | pacman25 |
| 020 | Thunno 2 S | 230525T191220Z | The Thon |
| 154 | Rockstar | 200925T162335Z | Shaggy |
| 049 | Julia 1.0 | 230521T160101Z | MarcMush |
| 070 | Python 3 | 230519T205217Z | morgoth1 |
| 054 | Zsh + bc | 210221T164716Z | pxeger |
| 040 | APL Dyalog Extended | 200925T213334Z | rak1507 |
| 037 | x86machine code | 190302T030140Z | 640KB |
| 224 | SNOBOL4 CSNOBOL4 | 190726T154732Z | Giuseppe |
| 106 | TSQL | 190516T115710Z | t-clause |
| 379 | Beatnik | 190606T085226Z | jimmy230 |
| 382 | brainfuck | 190517T073615Z | Dorian |
| 018 | 05AB1E | 190516T154755Z | Grimmy |
| 092 | PowerShell | 190301T154055Z | GMills |
| 060 | PowerShell | 190301T150158Z | mazzy |
| 297 | Powershell with amazing 297 Bytes | 190301T141704Z | Add-TheN |
| 030 | Japt x | 180417T194040Z | Oliver |
| 170 | BrainFlak | 180417T220654Z | DJMcMayh |
| 038 | K oK | 180420T220136Z | mkst |
| nan | C# .NET Core | 180418T093525Z | TheLetha |
| 733 | Beatnik | 180420T230205Z | MickyT |
| 9694 | Go | 180420T122820Z | Kristoff |
| nan | C# .NET Core | 180420T214038Z | Geoffrey |
| 074 | Wolfram Language Mathematica | 180420T165114Z | mbomb007 |
| 074 | PHP | 180420T124036Z | Titus |
| 066 | Haskell | 180418T111644Z | Laikoni |
| 091 | Excel | 180419T210346Z | Engineer |
| 072 | C gcc | 180418T092949Z | O.O.Bala |
| 078 | SmileBASIC | 180419T130934Z | 12Me21 |
| 070 | Java 8 | 180418T074635Z | Kevin Cr |
| 358 | Emojicode | 180419T022756Z | X1M4L |
| 095 | C++ | 180419T010056Z | Tas |
| 041 | Retina 0.8.2 | 180417T210639Z | Neil |
| 052 | Perl 6 | 180418T221227Z | Phil H |
| 033 | Pip | 180418T213439Z | DLosc |
| 063 | R | 180417T194059Z | Giuseppe |
| 062 | JavaScript Node.js | 180417T192913Z | DanielIn |
| 117 | Visual Basic for Applications | 180418T161918Z | dnep |
| 109 | Gforth | 180417T235844Z | Kitana |
| 050 | Octave | 180418T082002Z | Sanchise |
| 097 | Red | 180418T111119Z | Galen Iv |
| 073 | Octave | 180418T060545Z | Tom Carp |
| 050 | Perl 5 pF | 180417T224211Z | nwellnho |
| 019 | Jelly | 180417T225304Z | Jonathan |
| 060 | Ruby | 180417T222509Z | benj2240 |
| 081 | sed 4.2.2 | 180417T213050Z | Digital |
| 036 | Japt | 180417T220957Z | Etheryte |
| 073 | Perl 5 pF | 180417T220106Z | Xcali |
| 055 | Perl 6 | 180417T215455Z | nwellnho |
| 084 | Haskell | 180417T191519Z | ბიმო |
| 064 | Bash + GNU utilities | 180417T214654Z | Digital |
| 085 | Octave / MATLAB | 180417T211044Z | Luis Men |
| 078 | Python 2 | 180417T192426Z | Rod |
| 069 | JavaScript ES6 | 180417T202632Z | Arnauld |
| 021 | 05AB1E | 180417T201630Z | Emigna |
| 040 | Pyth | 180417T192354Z | user4854 |
AWK, 91 bytes
@load "ordchr"
{for(a="02210313074020029000033739";i++<NF;)x+=substr(a,ord($i)-64,1)+1}$0=x
Vyxal rs, 20 bytes
øA11»⟑↑Gp→₃ꜝ¬∑"ݶ»τİ
port of thunno 2 answer but I needed an extra byte for the compressed number since vyxal cant zero index letters to numbers
Thunno 2 S, 20 bytes
Ä⁻»yŒṄȦẏÄb¶Ỵɓæ»11Bsi
Explanation
Ä⁻»yŒṄȦẏÄb¶Ỵɓæ»11Bsi # Implicit input
Ä⁻ # Convert input to 0-based indices in alphabet
»yŒṄȦẏÄb¶Ỵɓæ» # Compressed integer 140755590392038037755694497
11B # Converted to base 11 as a list:
# [1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10]
# (A, B, C, D, E, ...)
si # Index into the list
# Implicit output of the sum
Rockstar, 187 184 156 154 bytes
listen to W
cut W in S
cut W
while W
roll W in X
P's 1
cut "DG1BCMP1FHVWY1K111JX11QZ" in T
while T
roll T in L
let P be+L's 1
let S be+L's X and P
say S
Try it here (Code will need to be pasted in)
Abuses a nice little feature in Rockstar whereby the variable (S in this case) an array is assigned to can be used to reference both the array (e.g., S at index) or, by itself, the length of the array. So, by cutting the input Word into the array S we, straight away, have a base score of 1 point per letter.
listen to W :Read input into variable W
cut W in S :Split W into variable S (our score)
cut W :Split W
while W :While W is not empty
roll W in X : Pop first element of W into variable X
P's 1 : Initialise P as 1 (base score for each tile)
cut "..." in T : Split "DG1BCMP1FHVWY1K111JX11QZ" into variable T
while T : While T is not empty
roll T in L : Pop first element of T into variable L
let P be+ : Increment P by
L's 1 : Is L equal to 1?
let S be : Increment S by
L's X : Is L equal to X?
and P : Logical AND with P
: End while loop
:End while loop
say S :Output S
Julia 1.0, 49 bytes
!x=sum(i->b"\1\3\3\2\1\4\2\4\1\b\5\1\3\1\1\3\n\1\1\1\1\4\4\b\4\n"[i-'@'],x)
(the special characters are escaped here for clarity)
Python 3, 70 bytes
This isn't any better than Rod's answer (which is also 70 bytes) but it is a different approach than the other answers I see in this thread so I figured I'd submit it anyway.
lambda s:sum(0xa48441111a3113158142412331>>4*(ord(c)-97)&15for c in s)
Shorter version, 68 bytes
-2 bytes thanks to xnor, now making this better than the previous Python answer!
lambda s:sum(0xa48441111a3113158142412331>>ord(c)%97*4&15for c in s)
Zsh + bc, 54 bytes
bc<<<$#1`tr a-z 02210313074020029000033739<<<${1///+}`
Pure Zsh, 55 bytes
<<<$[$#1`tr a-z 02210313074020029000033739<<<${1///+}`]
APL (Dyalog Extended), 40 bytes
+/1+⍎¨'02210313074020029000033739'[⎕A⍳⎕]
The same method as the K answer, index into a list of scores, add 1 to them, and sum.
x86-machine code, IBM PC DOS, 37 bytes
00000000: bb18 0133 d2ac 2c41 d0e8 9cd7 d410 9d72 ...3..,A.......r
00000010: 0286 e002 d0e2 eec3 1332 1424 1851 3113 .........2.$.Q1.
00000020: a111 1448 4a ...HJ
Listing:
BB 0118 MOV BX, OFFSET TBL ; load score table into BX
33 D2 XOR DX, DX ; clear letter count
LOOP_LETTER:
AC LODSB ; load next char from DS:SI into AL, advance SI
2C 41 SUB AL, 'A' ; convert letter to zero-based index
D0 E8 SHR AL, 1 ; divide index by 2, set CF if odd index
9C PUSHF ; save CF
D7 XLAT ; AL = packed score byte
D4 10 AAM 16 ; unpack nibbles into AH/AL (clobbers CF though)
9D POPF ; restore CF
72 02 JC ODD ; if odd index, use low nibble
86 E0 XCHG AH, AL ; otherwise is even, use high nibble
ODD:
02 D0 ADD DL, AL ; add tile to score
E2 EE LOOP LOOP_LETTER ; loop through all letters
C3 RET ; return to caller
TBL:
DB 013H,032H,014H,024H,018H,051H,031H,013H,0A1H,011H,014H,048H,04AH
Note: using link above, re-run with input on command line like ASM ABC, etc.
As a callable function, input string at [SI], length in CX. Output to DX.
Explanation
Uses a score table of 4-bit nibbles for each tile's score. Loops through the input string and converts each letter to a zero-based index. Each index is divided by two to find the corresponding byte in the table. The low nibble is added if the index is odd, otherwise the high nibble is added if the index is even.
Test Program I/O
SNOBOL4 (CSNOBOL4), 224 bytes
V =ARRAY(10,0)
V<1> ='EAIONRTLSU'
V<2> ='DG'
V<3> ='BCMP'
V<5> ='FHVWY'
V<8> ='JX'
V<10> ='QZ'
I =INPUT
N S =I
X =LT(X,10) X + 1 :F(O)
R S ANY(V<X>) = :S(R)
O =O + X * (SIZE(I) - SIZE(S))
I =S :(N)
O OUTPUT =O
END
Very naive approach. Stores the letters in an ARRAY with indices equal to the scores. It tries to match 0 for point values that don't exist, which ends in a score of 0.
V =ARRAY(10,0) ;* set up value array of 10 values with contents all 0
V<1> ='EAIONRTLSU' ;* set contents at appropriate indices
V<2> ='DG'
V<3> ='BCMP'
V<5> ='FHVWY'
V<8> ='JX'
V<10> ='QZ'
I =INPUT ;* read input
N S =I ;* begin Next iteration
X =LT(X,10) X + 1 :F(O) ;* increment X or GOTO output
R S ANY(V<X>) = :S(R) ;* replace ANY letters in V<X> with nothing
O =O + X * (SIZE(I) - SIZE(S)) ;* add to the score
I =S :(N) ;* set I to S and goto N
O OUTPUT =O
END
T-SQL, 106 bytes
Accepting uppercase characters.
Inserted a line break to make this readable:
DECLARE @ varchar(max)='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
WHILE'='<@
SET
@=stuff(@,1,1,'')+'-~'+substring('02210313074020029000033739',ascii(@)-64,1)
EXEC('print'+@)
Beatnik, 379 bytes
K ZZZZKF ZD ZD ZD ZD ZD ZD ZD
K ZZZZZZZZA J Z ZD ZB B
K XA KD ZD ZF F ZA ZD KF KF ZD ZB B
K D Z ZD ZB B
K ZF Z ZD ZF KA ZA ZD ZD KF KF KF ZD ZB B
K A KD ZD ZF D ZA KF ZD ZB B
K ZF KD ZD ZB B
K D Z ZD ZB B
K A Z ZD ZB B
K ZF Z ZD ZB B
K D Z ZD ZF D ZA KF ZD ZB B
K Z KD ZD ZB B
K B Z ZD ZB B
K Z Z ZD ZB B
K A Z ZD ZF F ZA KF ZB Z
K K KD ZD ZB B
K B Z ZF A KF
KF
XX ZZZZZZZZZZZZZX
See also MickyT's answer.
brainfuck, 386 382 bytes
->,[>>,]->+>>+++>>+++>>++>>+>>++++>>++>>++++>>+>>++++++++>>+++++>>+>>+++>>+>>+>>+++>>++++++++++>>+>>+>>+>>+>>++++>>++++>>++++++++>>++++>>+++++++++++[-<<+]->+[->++++++++[<-------->-]<-[[->+]>>-<+[-<+]->]+[->+]->[->+>[>>]>++[-<+]->]>[-<+>]<<+<[<<]>-<+[-<+]>>->+]>[>>]>>+[[-]<[->+<[->+<[->+<[->+<[->+<[->+<[->+<[->+<[->+<[->[-]>>+>+<<<]]]]]]]]]<]>>[>]++++++[-<++++++++>]>>]<<<[.[-]<<<]
Pretty much ungolfed, but first brainfuck code for this question. This code can only handle uppercase letters. Any other symbol will lead to an endless loop. Also the maximum score depends on the used bf-interpreter. On tio.run this can handle scores up to 254.
points:
abcde fghij klmno pqrst uvwxyz
13321 42418 51311 3x111 14484x (x = 10)
tape:
[input], 0, [points table], 0, 0, score
input: current letter marker (0 or neg 1), current letter
points table: check letter marker (0 or neg 1), points
code:
- set current letter marker
>,[>>,] input characters
-> set check letter marker
fill points table
+>>+++>>+++>>++>>+>>
++++>>++>>++++>>+>>++++++++>>
+++++>>+>>+++>>+>>+>>
+++>>++++++++++>>+>>+>>+>>
+>>++++>>++++>>++++++++>>++++>>++++++++++
+[-<<+]-> go to first input letter
+ bugfix: intersecting pointers
[ main loop
- bugfix: intersecting pointers
>++++++++[<-------->-]<- subtract 65 from current letter (ASCII A)
[ check loop: mark the entry in points table that's related to the current letter
[->+] decrease letter and go to check letter marker
>>- set new check letter marker
<+[-<+]- go back to current letter marker
> go to current letter
]
the related points entry is found so we need to add the points to the score
+[->+]-> go to related points entry
[ addition loop
->+ decrease points entry and save points to temporary cell
>[>>]>+ go to score and add 1
+[-<+]-> go back to related points entry
]
>[-<+>] move points back from temp cell
<<+ delete "check letter marker"
<[<<]>- set "check letter marker" at A
<+[-<+] go to current letter marker
>>- set new current letter marker
> next input letter
+ exit loop if cell pointer is at "check letter marker" position
]
>[>>]> go to score
number printing routine from esolangs
>+[[-]<[->+<[->+<[->+<[->+<[->+<[->+<[->+<[->+<[->+<[->[-]>>+>+<<<]]]]]]]]]<]>>[>]++++++[-<++++++++>]>>]<<<[.[-]<<<]
if unary output is allowed then this 297 293 bytes code can be used:
->,[>>,]->+>>+++>>+++>>++>>+>>++++>>++>>++++>>+>>++++++++>>+++++>>+>>+++>>+>>+>>+++>>++++++++++>>+>>+>>+>>+>>++++>>++++>>++++++++>>++++>>+++++++++++[-<<+]->+[->++++++++[<-------->-]<-[[->+]>>-<+[-<+]->]+[->+]->[->+>[>>]>++[-<+]->]>[-<+>]<<+<[<<]>-<+[-<+]>>->+]>[>>]<+++++++[>+++++++<-]>>[-<.>]
05AB1E, 18 bytes
•Q<ß÷λv¸Ïàœ¶•IÇè>O
Explanation:
•Q<ß÷λv¸Ïàœ¶• # compressed integer 30740200290000337390221031
IÇ # codepoints of the input
è # index into the digits of the large integer
> # increment each
O # sum
The large integer encodes the point value of each letter. Since encoding numbers in the range 1-10 would be inconvenient, we subtract 1 so that each one fits on a single digit (this is why the > is needed).
Note that indexing in 05AB1E wraps around, so the first digit of the number actually corresponds to "h" ("h"'s codepoint is 104, which is 0 mod 26). This is convenient: since numbers can't start with 0, we don't want the first digit to correspond to "a".
Alternative 18-byter:
A•Θ¡₆öηÖ&'/α°•R‡>O
A pushes "abcdefghijklmnopqrstuvwxyz". ‡ transliterates, replacing each letter in the input with the corresponding digit in the compressed integer. A‡ saves one byte over IÇè; unfortunately, this encoding puts "a" first, so we have to use R (reverse) to get around the issue that numbers can't start with 0.
Yet another 18-byter:
žW•Δÿ¦ÝZ=áí4_ø•‡>O
Same idea as above, but this one uses the obscure žW built-in to push "qwertyuiopasdfghjklzxcvbnm". žW is one byte longer than A, but it saves the R since "q" isn't a 1-point letter.
PowerShell, 92 bytes
$args|% T*y|%{$i+=switch -r($_){[DG]{2}[BCMP]{3}[FHVWY]{4}K{5}[JX]{8}[QZ]{10}default{1}}};$i
Didn't notice @Add-TheNewOne's answer before writing this, but I think it's fairly different in any case.
Powershell with amazing 297 Bytes
Any idea to make the script shorter?
$1=read-Host
$s=0
$l=$1.ToCharArray()
$l|%{switch($_){{'E','A','I','O','N','R','T','L','S','U'-contains$_}{$s+=1}{'D','G'-contains$_}{$s+=2}{'B','C','M','P'-contains$_}{$s+=3}{'F','H','V','W','Y'-contains$_}{$s+=4}{'J','X'-contains$_}{$s+=8}{'Q','Z'-contains$_}{$s+=10}{'K'-contains$_}{$s+=5}}}
$s
Japt -x, 43 39 35 30 bytes
£2+`dglbcÛghvwylk¥ljx¥qz`qÊaøX
35 byte solution:
¬£Ò('0+#Ý03#740#È29e4+33739 g65nXc
Brain-Flak, 210, 204, 198, 184, 170 bytes
({<([{}]<>(({}{}))(([][][][][])<((([]())<([][])>))((((()))))>)[](((()()())<((()))>)((())()()()()))((())()()())((()())()())[]((((())())()))(())){({}<{}>())}>{}{}<{{}}><>})
Thanks to @JoKing for saving 14 bytes!
Readable version:
({ # For each character
# Push array of letter scores
# Also adjust character to 1-indexing
<([{}]<>
(({}{})) # Push 2 0s
(([][][][][]) # 10
<((([]()) # 4
<([][])> # 8
)) # 4,4
((((()))))> # 1,1,1,1
) # 10
[] # Add 12 to difference
(((()()()) # 3
<((()))> # 1,1
) # 3
((())()()()())) # 1, 5
((())()()()) # 1, 4
((()())()()) # 2, 4
[] # Add 22 to difference
((((())())())) # 1,2,3
(()) # 1
) # Push 65-char
{({}<{}>())} # Index character into score array
>
{}{} # Add score to running total
<{{}}><> # Clear the stack
}) # Implicit print of total score
K (oK), 60 38 bytes
Solution:
+/1+.:'"02210313074020029000033739"65!
Explanation:
Index into the scores, sum up result.
+/1+.:'"02210313074020029000033739"65! / the solution
65! / input modulo 65 to get position in A-Z
"02210313074020029000033739" / index into the scores (1 point lower)
.:' / value (.:) each (') to convert to ints
1+ / increase by 1
+/ / sum up
C# (.NET Core), 50 + 18 = 68 bytes
s=>s.Sum(c=>"02210313074020029000033739"[c-65]-47)
+18 bytes for using System.Linq;
Beatnik, 733 bytes
Since it really had to be done, here it is. It was a really nasty to debug and provided a few challenges.
Input must be uppercase letters only. Output is unary (hope that is OK?)
J K ZZZZZZK Z ZD ZB ZZZZZZZZZZZZZZZZZA K A Z ZD ZB ZZZZZZZZZZZZZZZKF K A Z ZD ZB ZZZZZZZZZZZZZZZB K A Z ZD ZB ZZZZZZZZZZZZZZZ K A Z ZD ZB ZZZZZZZZZZZZZZKD K A Z ZD ZB ZZZZZZZZZZZZZD K A Z ZD ZB ZZZZZZZZZZZZZD K A Z ZD ZB ZZZZZZZZZZZZ K A Z ZD ZB ZZZZZZZZZZZZB K A Z ZD ZB ZZZZZZZZZKA K A Z ZD ZB ZZZZZZZZZKF K A Z ZD ZB ZZZZZZZZZZK K A Z ZD ZB ZZZZZZZZZB K A Z ZD ZB ZZZZZZZZZB K A Z ZD ZB ZZZZZZZZKD K A Z ZD ZB ZZZZZZZK K A Z ZD ZB ZZZZKB K A Z ZD ZB ZZZZZZKF K A Z ZD ZB ZZZZZZB K A Z ZD ZB ZZZZZFB K A Z ZD ZB ZZZZZA K A Z ZD ZB ZZZAK K A Z ZD ZB ZZZ K A Z ZD ZB ZD K A Z ZD ZB ZKB K ZZZZKF KF K ZZZZKF KF K ZZZZKF KF K ZZZZKF KF K ZZZZKF KF K ZZZZKF KF K ZZZZKF KF K ZZZZKF KF K ZZZZKF KF K ZZZZKF KF K A ZKA ZZZZZZZZZZZZZZZZZZY
General process is:
- get character from input
- subtract 65
- check if result is 0
- if 0 jump specified amount of words.
- otherwise subtract 1 and repeat check.
- the jump targets are push print operations followed be a loop back to beginning of program.
Ends with an error.
A more complete explanation:
J K ZZZZZZK Z ZD # Get input and subtract 65
ZB ZZZZZZZZZZZZZZZZZA K A Z ZD # Character A - if 0 jump to print, otherwise subtract 1
ZB ZZZZZZZZZZZZZZZKF K A Z ZD # Character B - if 0 jump to print, otherwise subtract 1
ZB ZZZZZZZZZZZZZZZB K A Z ZD # Character C - if 0 jump to print, otherwise subtract 1
ZB ZZZZZZZZZZZZZZZ K A Z ZD # Character D - if 0 jump to print, otherwise subtract 1
ZB ZZZZZZZZZZZZZZKD K A Z ZD # Character E - if 0 jump to print, otherwise subtract 1
ZB ZZZZZZZZZZZZZD K A Z ZD # Character F - if 0 jump to print, otherwise subtract 1
ZB ZZZZZZZZZZZZZD K A Z ZD # Character G - if 0 jump to print, otherwise subtract 1
ZB ZZZZZZZZZZZZ K A Z ZD # Character H - if 0 jump to print, otherwise subtract 1
ZB ZZZZZZZZZZZZB K A Z ZD # Character I - if 0 jump to print, otherwise subtract 1
ZB ZZZZZZZZZKA K A Z ZD # Character J - if 0 jump to print, otherwise subtract 1
ZB ZZZZZZZZZKF K A Z ZD # Character K - if 0 jump to print, otherwise subtract 1
ZB ZZZZZZZZZZK K A Z ZD # Character L - if 0 jump to print, otherwise subtract 1
ZB ZZZZZZZZZB K A Z ZD # Character M - if 0 jump to print, otherwise subtract 1
ZB ZZZZZZZZZB K A Z ZD # Character N - if 0 jump to print, otherwise subtract 1
ZB ZZZZZZZZKD K A Z ZD # Character O - if 0 jump to print, otherwise subtract 1
ZB ZZZZZZZK K A Z ZD # Character P - if 0 jump to print, otherwise subtract 1
ZB ZZZZKB K A Z ZD # Character Q - if 0 jump to print, otherwise subtract 1
ZB ZZZZZZKF K A Z ZD # Character R - if 0 jump to print, otherwise subtract 1
ZB ZZZZZZB K A Z ZD # Character S - if 0 jump to print, otherwise subtract 1
ZB ZZZZZFB K A Z ZD # Character T - if 0 jump to print, otherwise subtract 1
ZB ZZZZZA K A Z ZD # Character U - if 0 jump to print, otherwise subtract 1
ZB ZZZAK K A Z ZD # Character V - if 0 jump to print, otherwise subtract 1
ZB ZZZ K A Z ZD # Character W - if 0 jump to print, otherwise subtract 1
ZB ZD K A Z ZD # Character X - if 0 jump to print, otherwise subtract 1
ZB ZKB # Character Y - if 0 jump to print, otherwise subtract 1
K ZZZZKF KF # Jump Point for print 1111111111
K ZZZZKF KF #
K ZZZZKF KF # Jump Point for print 11111111
K ZZZZKF KF #
K ZZZZKF KF #
K ZZZZKF KF # Jump Point for print 11111
K ZZZZKF KF # Jump Point for print 1111
K ZZZZKF KF # Jump Point for print 111
K ZZZZKF KF # Jump Point for print 11
K ZZZZKF KF # Jump Point for print 1
K A ZKA ZZZZZZZZZZZZZZZZZZAAAA # Jump back to start
Go, 96 94 bytes
func(i string){a,s:=`
`,0;for _,c:=range i{s+=int(a[c-65])};print(s)}
Go allows any byte sequence in a raw string, except \r and `, this saves about 3 bytes off the total.
This is just a remix of existing solutions in other languages, so no real new innovation here.
Edit: I missed the memo where we dont have to name our functions
C# (.NET Core), 48 + 18 = 66 bytes
s=>s.Sum(c=>"!#C²!$1$$XhQJ"[c%13]-16>>c%2*4&15)
+18 bytes is for using System.Linq;
Wolfram Language (Mathematica), 74 bytes
Of course Wolfram|Alpha supports Scrabble scoring! This is an anonymous function.
Plus@@(First[WolframAlpha["Scrabble "<>##,"NumberData"]]&/@Characters[#])&
This doesn't work on TIO.
To run, go here, scroll down and click "Create a New Notebook »". The code to use in the notebook is in this TIO program so you can copy it. Paste each function call in its own code block. If you run too many in a single block, the execution won't complete.
Note that WolframAlpha sends a request using the Internet. Though there are other answers on PPCG that use it, I thought you should know.
This program uses the below shorter function, but calls it on each individual character of the input (sending a separate call to Wolfram|Alpha each time!)
This only works for input up to length 15, the width of a Scrabble board. (49 bytes)
First[WolframAlpha["Scrabble "<>#,"NumberData"]]&
Same as above, but will display the result in a box, along with whether the input is a valid Scrabble word. (45 bytes)
First[WolframAlpha["Scrabble "<>#,"Result"]]&
PHP, 74 bytes
while($c=$argn[$i++])$s+=1+_02210313074020029000033739[ord($c)&31];echo$s;
case insensitive. Run as pipe with -nR or try it online.
Haskell, 66 bytes
sum.map(\c->1+read["02210313074020029000033739"!!(fromEnum c-65)])
Try it online! Same approach as DanielIndie's JavaScript answer.
Haskell, 82 81 bytes
sum.map(\c->1+sum(read.pure<$>lookup c(zip"DGBCMPFHVWYKJXQZ""1122223333347799")))
Excel, 91 bytes
{=LEN(A1)+SUM(0+("0"&MID("02210313074020029000033739",CODE(MID(A1,ROW(A:A),1)&"z")-64,1)))}
Explanation:
- Input is in cell
A1 - The formula must be entered as an array formula with Ctrl+Shift+Enter, which adds the curly brackets
{ }to both ends. MID(A1,ROW(A:A),1)pulls out each character in turn (and a lot of empty values, too, since it's going to return as many values as there are rows in the sheet)CODE(MID(~)&"z")pulls out the ASCII value for the each character. The&"z"appends azto the end of theMID()result becauseCODE()doesn't like empty inputs. The ASCII value forzis higher than every capital letter, though, so it's effectively ignored later.MID("02210313074020029000033739",CODE(~)-64,1)pulls out a letter from the score string based on its ASCII value adjusted down by 64 so the letters run 1-26 instead of 65-90."0"&MID(~)prepends a zero to theMID()result because Excel won't let you do math with empty strings, of which there will be several.0+("0"&MID(~))turns all those strings into numbers.SUM(0+("0"&MID(~)))adds up all those strings that are now numbers.LEN(A1)+SUM(~)adds the length of the input to the sum because all the values in the score string (02210313074020029000033739) were adjusted down by one so they would all be one digit long.
There's a very similar solution in Google Sheets but it comes in at 97 bytes because ArrayFromula() is longer than {} (but at least it can handle 0 + "" = 0).
=Len(A1)+ArrayFormula(Sum(0+Mid("02210313074020029000033739",Code(Mid(A1,Row(A:A),1)&"z")-64,1)))
C (gcc), 78 72 bytes
i;f(char*s){for(i=0;*s;)i+="\n\n"[*s++-65];s=i;}
There are actually 26 characters in that string. See the code rendered properly and run it here.
Thanks to gastropner for golfing 6 bytes.
Ungolfed version:
i; // declare a variable to store the score; it is implicitly of type int
f(char* s) { // function taking a string as argument and implicitly returning an int
for(i = 0; // initialize the score to 0
*s; ) // iterate over the string until we hit terminating NUL byte
i += "\n\n"[*s++ - 65]; // this is a 26-char string containing the ASCII equivalent of each numeric scrabble value; 65 is ASCII code for 'A', mapping the alphabet onto the string
s = i; // implicitly return the score
}
SmileBASIC, 87 85 78 bytes
INPUT S$WHILE""<S$S=S+VAL(@2210313074020029000033739[ASC(POP(S$))-65])+1WEND?S
Old method:
INPUT S$WHILE""<S$S=S+(INSTR(@DG+@___BCMP_FHVWYK*2+"JX"*4+@QZ,POP(S$))+4)DIV 5+1WEND?S
Java 8, 75 71 70 bytes
s->s.chars().map(c->"\n\n".charAt(c-65)).sum()
-1 byte by changing "02210313074020029000033739".charAt(c-65)-47 to unprintables (and two \n) so the -47 can be removed. Inspired by @Sanchises' Octave answer.
s-> // Method with String parameter and integer return-type
s.chars() // Loop over the characters as IntStream
.map(c->"\n\n".charAt(c-65))
// Convert the character to its value
.sum() // And sum it all together
Emojicode, 358 bytes
🐖🔥➡️🔡🍇🍮s 0🔂l🍡🐕🍇🍮s➕s🍺🐽🍯🔤a🔤1🔤e🔤1🔤i🔤1🔤l🔤1🔤n🔤1🔤o🔤1🔤r🔤1🔤s🔤1🔤t🔤1🔤u🔤1🔤d🔤2🔤g🔤2🔤b🔤3🔤c🔤3🔤m🔤3🔤p🔤3🔤f🔤4🔤h🔤4🔤v🔤4🔤w🔤4🔤y🔤4🔤k🔤5🔤j🔤8🔤x🔤8🔤q🔤10🔤z🔤10🍆🔡l🍉🍎🔡s 10🍉
Explanation:
I changed the variable names from single letters to more meaningful words, and expanded some parts of my code to hopefully make it more readable for people unfamiliar with the language. You can test the expanded program here.
🐋🔡🍇 👴 define a class that takes a string
🐖🔥➡️🔡🍇 👴 define a method that returns a string
🍦values🍯 👴 create int dictionary
🔤a🔤1 🔤e🔤1 🔤i🔤1 🔤l🔤1 🔤n🔤1 🔤o🔤1 🔤r🔤1 🔤s🔤1 🔤t🔤1 🔤u🔤1 🔤d🔤2 🔤g🔤2
🔤b🔤3 🔤c🔤3 🔤m🔤3 🔤p🔤3 🔤f🔤4 🔤h🔤4 🔤v🔤4 🔤w🔤4 🔤y🔤4 🔤k🔤5 🔤j🔤8 🔤x🔤8
🔤q🔤10 🔤z🔤10
🍆 👴 ^ dictionary contains letters(keys) and their numerical values
🍮score 0 👴 declare 'score' variable and set to 0
🍦iterator🍡🐕 👴 transform input string to iterator
🔂letter iterator🍇 👴 iterate over each byte in input string
🍮score➕score 🍺🐽values 🔡letter 👴 add value of each letter to score
🍉
🍎🔡score 10 👴 return the score as a string
🍉
🍉
🏁🍇 👴 begin the program here
😀🔥🔤abc🔤 👴 call scoring method and print the score
😀🔥🔤ppcg🔤 👴 repeat with other test cases
😀🔥🔤stewie🔤
😀🔥🔤fizzbuzz🔤
😀🔥🔤abcdefghijklmnopqrstuvwxyz🔤
🍉
C++, 95 bytes
char*m="02210313074020029000033739";
int f(char*p){int n=0;while(*p)n+=m[*p++-65]-47;return n;}
Try it online (not a TIO link sorry)
Explanation:
- Declares
m, an array of the values of each letter in order, minus 1. The minus 1 is because of Q and Z: I couldn't have a two digit number in there - Iterates through through the string
puntil we get to null character, and adds the score of the number (*pgives us the letter, and-65so we can properly index the array). Sincemis achar*it converts to acharso we minus48so bring it back to 0, but add1sincemis declared as one score less for each character.
I'm not an avid poster here so I hope I've done this correctly. I believe they returning n counts as printing the value, and that declaring a function is fine.
Retina 0.8.2, 41 bytes
T`BCDGJKMPQXZF\HVWY`221174229793
.
$*..
.
Try it online! Link includes test cases. Explanation: Like the Haskell answer, nontrivial letters are translated to 1 less than their score, and 1 is added later when the characters are converted to unary. Putting FHVWY last allows them all to map to a score of 3 + 1.
Perl 6, 52 bytes
{TR/A..Z/02210313074020029000033739/.comb.sum+.ords}
Maps every character to a digit, and sums them. And adds 1 for each character because there isn't a digit 10 without incurring unicode bytes.
Pip, 34 33 bytes
1+("3V1S1QFRU1SMQ12W7"FB36)@A_MSa
Takes input in lowercase as a command-line argument. Try it online!
Explanation
We map a function to each character of the command-line argument a and sum the results using MS.
In the function, we convert the argument _ to its ASCII code using A, and then use that to index into the magic number 30740200290000337390221031. With cyclic indexing, this maps a -> 0, b -> 2, ... , z -> 9 (one less than the Scrabble score). We then add 1 to get the correct score of each letter.
We save one byte by encoding the number in base 36 (the highest base Pip's builtin base conversion can handle) as the string "3V1S1QFRU1SMQ12W7" and then converting it from base 36 to decimal using FB.
R, 90 63 bytes
function(W,u=utf8ToInt)sum(u('
')[u(W)-64])
Takes input as an uppercase string. R handles unprintables and multiline strings without issues, so that's nice. Now we're almost twice the external package!
And because CRAN has so many random goodies:
R + ScrabbleScore 31 bytes
ScrabbleScore::sws(scan(,""),F)
Sadly, sws checks for validity by default.
JavaScript (Node.js), 71 66 63 62 bytes
- @Arnauld awesome as always reducing 7 bytes
- thanks to l4m2 for rducing by 1 byte
s=>Buffer(s).map(x=>s="02210313074020029000033739"[x-65]-~s)|s
Visual Basic for Applications, 117 bytes
s=inputbox(u):for i=1to 16:s=replace(s,mid("DGBCMPFHVWYKJXQZ",i,1),space(mid("1122223333347799",i,1)+1)):next:?len(s)
Run in immediate (debug) window.
Gforth, 109 Bytes
: V s" 1332142418513113:11114484:" ; : C 0 NAME 0 DO DUP C@ 65 - V DROP + C@ 48 - ROT + SWAP 1+ LOOP DROP . ;
Input must be uppercase:
C PPCG 11 OK
Readable
\ String used as table with values for each letter in the alphabet
\ : follows 9 in the ASCII-table
: V
s" 1332142418513113:11114484:"
;
: C
0 \ Initialize sum ( sum )
NAME \ Get the string ( sum c-addr count )
0 DO \ Start of loop ( sum c-addr )
DUP C@ \ Get letter ( sum c-addr char )
65 - \ Calculate table index ( sum c-addr index )
V DROP + C@ \ Get table entry ( sum c-addr entry )
48 - \ Calculate entry value ( sum c-addr value )
ROT + SWAP \ Update sum ( sum' c-addr )
1+ \ Next character ( sum' c-addr' )
LOOP
DROP . \ Drop c-addr and print result
;
Octave, 50 bytes
@(t)' '/3*sum(65:90==t')'
Challenge accepted. Explanation:
@(t) % Define anonymous function taking a single argument t.
' ... '/3 % Row vector with letter scores. Corresponds to char([1 3 3 2 ...]*3).
% The factor 3 was necessary to avoid a newline.
* % Dot product (yes, * is dot product, .* is not. Go figure). Equivalent to sum of element-wise products.
65:90 % Alphabet
==t' % Broadcast equality with input string.
sum( ) % Sum this matrix. Gives the count of each letter in the alphabet
' % Transpose into column vector for dot product
Red, 97 bytes
func[s][t: :to-integer n: 0 foreach c s[n: n - 47 + t pick{02210313074020029000033739}t c - 64]n]
Takes a string as input.
Explanation of the ungolfed solution:
f: func[s][
n: 0 ; n will store the sum
foreach c s [ ; for each character in the argument
n: n - 47 + to-integer pick {02210313074020029000033739} to-integer c - 64
; find its offset from "A" (to-integer c - 64)
; use this as an index in the string with scores (pick {...} ...)
; to extract the score and convert it to an integer
; increase it with 1 and add it to the sum
]
n ; return the sum
]
Octave, 73 bytes
@(x)sum('09977433333222211'(([~,y]=ismember(x,'QZJXKFHVWYBCMPDG'))+1)-47)
Uses ismember to map each character in the input stream x onto it's index in the lookup string 'QZJXKFHVWYBCMPDG'. Any element not found will be mapped to an index of 0 (this will include the 1-point characters).
Next we add 1 to the index to make the 0's become valid 1-index references, and lookup into the string '09977433333222211'. This is one element longer than the first lookup string. The digits represent the point value of each element in the original string, minus 1, with the extra element being a '0' at the beginning .
Finally the resultant string is converted to integers by subtracting 47 ('0'-1), yielding the point value for each letter, and all point values are then summed.
Jelly, 19 bytes
Oị“ÆẠḃbṂƬɠF#ṁ²’ḃ⁵¤S
A monadic link accepting a list of upper-case characters which returns an integer
Try it online! Or see the test-suite.
How?
Oị“ÆẠḃbṂƬɠF#ṁ²’ḃ⁵¤S - Link: list of characters
O - ordinals ('A'->65, B->66...)
¤ - nilad followed by link(s) as a nilad:
“ÆẠḃbṂƬɠF#ṁ²’ - literal 14011114485013321424185131
⁵ - literal 10
ḃ - bijective-base = [1,3,10,1,1,1,1,4,4,8,4,10,1,3,3,2,1,4,2,4,1,8,5,1,3,1]
ị - index into (1-based & modular) (vectorises)
- i.e. mapping from: O P Q R S T U V W X Y Z A B C D E F G H I J K L M N)
S - sum
Ruby, 60 bytes
->s{s.sum{|c|"BDDCBECEBIFBDBBDKBBBBEEIEK"[c.ord-65].ord-65}}
A lambda, accepting input as an array of (uppercase) characters and returning an integer.
sed 4.2.2, 81
s/[QZ]/JD/g
s/[JX]/KB/g
s/K/FE/g
s/[FHVWY]/BE/g
s/[BCMP]/DE/g
s/[DG]/EE/g
s/./1/g
Output is in unary.
Reduces each letter to a combination of lower-scoring letters until all letters are 1-scorers. Then replaces those with 1s to give a unary count.
Japt, 36 bytes
£2+`dg
bcmp
fhvwy
k
jx
qz`·bøX
x
Takes input as a lowercase string, returns a number.
Short explanation:
£2+`dg
¬ // Split the input into chars,
£ // then map over each char, returning
2+`dg // 2 plus
qz`·bøX
bøX // the char's index in
qz`· // the hardcoded string split by newlines.
x // And finally sum the whole thing.
Perl 5 -pF, 73 bytes
$\+=(1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,(1)x4,4,4,8,4,10)[-65+ord]for@F}{
Perl 6, 55 bytes
{sum .map:{:36<Z3P8JOBRZ4X1FBXLWSVL>+>(.ord*4-260)%16}}
Takes an array of characters. Uses a 4-bit lookup table encoded in base-36.
Haskell, 86 84 bytes
f s=length s+sum[n|x<-s,(n,y)<-zip(9:7:[1..])$words"QZ JX DG BCMP FHVWY K",x`elem`y]
Explanation
Most letters give a score of 1 and thus we don't need to keep track of these, instead we just decrement each score (saves 1 byte on 10 as well) and then add the length of the string to the resulting score.
Thanks @nimi for -2 bytes (rearanging the words and using [1..] instead of [4,3..])!
Bash + GNU utilities, 64
sed -es/[{QZ]/JD,JX]/KB,K]/FE,FHVWY]/BE,BCMP]/DE,DG]/EE}/g|wc -c
Input read from STDIN. Uses a brace expansion to generate sed expressions equivalent to those in my sed answer.
Octave / MATLAB, 85 bytes
@(x)sum([1:4 7 9]*any(reshape(char(strsplit('DG BCMP FHVWY K JX QZ')),6,1,5)==x,3)+1)
Python 2, 78 bytes
lambda x:sum(map(('ABCEIKLMNOPRSTU'+'BCDGMPQZ'*2+'FHJJKQQVWXXYZZ'*4).count,x))
Shorter version, port of DanielIndie's answer, 71 70 bytes
-1 byte thanks to Sunny Patel
lambda x:sum(int('02210313074020029000033739'[ord(c)-65])+1for c in x)
JavaScript (ES6), 69 bytes
Takes input as an array of characters.
s=>s.map(c=>t-=~'9200204703130122093733'[parseInt(c,36)*25%26],t=0)|t
05AB1E, 21 bytes
Takes input as a lowercase list of characters.
•_JÊ¿ùã$Ƶ½œM•11вAIkèO
Try it online! or as a Test suite
Pyth, 40 bytes
sm+2x.e}dbc." zØÍ jÙ¹>;%OG5§"\ 1
Explanation
sm+2x.e}dbc." zØÍ jÙ¹>;%OG5§"\ 1
m Q For each character in the (implicit) input...
x.e b 1 ... find the first index in...
c." zØÍ jÙ¹>;%OG5§"\ ['dg','bcmp','fhvwy','k','','','jx','','qz']
}d ... containing the character...
+2 ... 2-indexed.
s Take the sum.
