22 and make
This commit is contained in:
parent
3349d09c74
commit
df503bcd69
|
@ -0,0 +1,18 @@
|
|||
outdir="./build"
|
||||
|
||||
all: 18 19 20 21 22
|
||||
|
||||
18:
|
||||
go build -o build/18
|
||||
|
||||
19:
|
||||
go build -o build/19
|
||||
|
||||
20:
|
||||
go build -o build/20
|
||||
|
||||
21:
|
||||
go build -o build/21
|
||||
|
||||
22:
|
||||
go build -o build/22
|
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,54 @@
|
|||
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
|
||||
}
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue