Created
January 26, 2025 09:00
-
-
Save itachi-19/4383e0b3bbc184f815746333d8ebc853 to your computer and use it in GitHub Desktop.
ProjectEuler - #22 - Names Scores
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
sed 's/"//g;s/,/\n/g' 0022_names.txt | | |
sort | | |
gawk ' | |
BEGIN { S = 0 } | |
{ | |
sum = 0 | |
for (i = 1; i <= length($0); i++) { | |
char = substr($0, i, 1) | |
value = ord(char) - ord("A") + 1 | |
sum += value | |
} | |
#print NR , $0 , sum | |
S += NR * sum | |
} | |
# Function to get ASCII value of a character | |
function ord(c) { | |
return index("ABCDEFGHIJKLMNOPQRSTUVWXYZ", c) | |
} | |
END { print S } ' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
sed 's/"//g; s/,/\n/g' 0022_names.txt | | |
sort | | |
gawk ' | |
BEGIN { S = 0 } | |
{ | |
sum = 0 | |
# Calculate alphabetical position of each character directly | |
for (i = 1; i <= length($0); i++) { | |
char = substr($0, i, 1) | |
value = index("ABCDEFGHIJKLMNOPQRSTUVWXYZ", char) | |
sum += value | |
} | |
S += NR * sum | |
} | |
END { print S }' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment