Skip to content

Instantly share code, notes, and snippets.

@itachi-19
Created January 26, 2025 09:00
Show Gist options
  • Save itachi-19/4383e0b3bbc184f815746333d8ebc853 to your computer and use it in GitHub Desktop.
Save itachi-19/4383e0b3bbc184f815746333d8ebc853 to your computer and use it in GitHub Desktop.
ProjectEuler - #22 - Names Scores
#!/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 } '
#!/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