commit d930d0b5edace77c8211cc482ae5fd7bfd8b9114 Author: Mikayla Dobson Date: Wed Jul 12 08:59:45 2023 -0500 init, day one solution diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3b735ec --- /dev/null +++ b/.gitignore @@ -0,0 +1,21 @@ +# If you prefer the allow list template instead of the deny list, see community template: +# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore +# +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +# Go workspace file +go.work diff --git a/dayone.go b/dayone.go new file mode 100644 index 0000000..7a8c831 --- /dev/null +++ b/dayone.go @@ -0,0 +1,90 @@ +package main + +import ( + "fmt" + "math" + "strconv" + "strings" +) + +func dayOne(input string) (maxCalories int) { + // split input by newlines + var eachPart = strings.Split(input, "\n") + + // trim all remaining whitespace + for i := range eachPart { + eachPart[i] = strings.TrimSpace(eachPart[i]) + } + + // assign a max value and a current value + maxCalories = 0 + temp := 0 + + for i := 0; i < len(eachPart); i++ { + // this indicates we have reached the end of each elf's snack stash + if eachPart[i] == "" { + // use the max between curent temp value and the current max value found + maxCalories = int(math.Max(float64(maxCalories), float64(temp))) + + // reassign temp to zero and start again + temp = 0 + continue + } + + // string to int conversion + partToInt, err := strconv.Atoi(eachPart[i]) + + if err != nil { + panic(err) + } + + // add the result to our current working total + temp += partToInt + } + + return +} + +func main() { + var result int; + var input string; + + input = + `1000 + 2000 + 3000 + + 4000 + + 5000 + 6000 + + 7000 + 8000 + 9000 + + 10000` + + result = dayOne(input) + + fmt.Println(result) + + input = + ` + 245 + 256 + 939 + + -12039 + + + 4949 + + 2 + 2 + 2 + ` + + result = dayOne(input) + fmt.Println(result) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..8200add --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/innocuous-symmetry/aoc + +go 1.20