diff --git a/day15/dayFifteen.go b/day15/dayFifteen.go new file mode 100644 index 0000000..b7af7fc --- /dev/null +++ b/day15/dayFifteen.go @@ -0,0 +1,36 @@ +package day15 + +import ( + "fmt" + "os" + "strings" +) + +func Run() int { + fmt.Println("hello day 15") + filename := "day15/example" + bytes, err := os.ReadFile(filename) + if err != nil { + panic(fmt.Sprint("error reading file ", filename)) + } + text := string(bytes) + text = strings.TrimSpace(text) + instructions := strings.Split(text, ",") + + result := 0 + + for _, instruction := range instructions { + result += ASCIIStringHash(instruction) + } + return result +} + +func ASCIIStringHash(str string) int { + result := 0 + for _, symb := range str { + result += int(symb) + result *= 17 + result %= 256 + } + return result +} diff --git a/day15/example b/day15/example new file mode 100644 index 0000000..4f58f74 --- /dev/null +++ b/day15/example @@ -0,0 +1 @@ +rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7 diff --git a/main.go b/main.go index 8e02bfc..ca58cb8 100644 --- a/main.go +++ b/main.go @@ -3,12 +3,12 @@ package main import ( "log" - "sunshine.industries/aoc2023/day14" + "sunshine.industries/aoc2023/day15" ) func main() { log.Print("> starting run:") - result := day14.Run() - log.Printf("day14 result: %d\n****\n", result) + result := day15.Run() + log.Printf("day15 result: %d\n****\n", result) }