22 and make

This commit is contained in:
Preston Baxter 2024-12-13 07:43:58 -06:00
parent 3349d09c74
commit df503bcd69
4 changed files with 5239 additions and 0 deletions

18
Makefile Normal file
View File

@ -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

1
cmd/22/input.txt Normal file

File diff suppressed because one or more lines are too long

54
cmd/22/main.go Normal file
View File

@ -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
}

5166
cmd/22/test.txt Normal file

File diff suppressed because it is too large Load Diff