55 lines
803 B
Go
55 lines
803 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"os"
|
||
|
"sort"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
//open file
|
||
|
fd, err := os.Open("cmd/22/input.txt")
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
defer fd.Close()
|
||
|
|
||
|
raw, err := io.ReadAll(fd)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
|
||
|
var names sort.StringSlice
|
||
|
|
||
|
names = strings.Split(strings.TrimSpace(string(raw)), ",")
|
||
|
for i, name := range names {
|
||
|
n := strings.ReplaceAll(name, "\"", "")
|
||
|
names[i] = n
|
||
|
}
|
||
|
|
||
|
//sort file
|
||
|
sort.Sort(names)
|
||
|
|
||
|
//loop through add sum
|
||
|
sum := 0
|
||
|
for i, name := range names {
|
||
|
n := strings.ReplaceAll(name, "\"", "")
|
||
|
fmt.Printf("%s\n", n)
|
||
|
sum += sumName(n) * (i + 1)
|
||
|
}
|
||
|
|
||
|
fmt.Printf("%s %d\n", names[937], sumName("COLIN"))
|
||
|
fmt.Printf("%d\n", sum)
|
||
|
}
|
||
|
|
||
|
func sumName(s string) int {
|
||
|
sum := 0
|
||
|
for _, c := range s {
|
||
|
sum += int(c - 64)
|
||
|
}
|
||
|
|
||
|
return sum
|
||
|
}
|