Skip to content

Commit 15689e4

Browse files
committed
1a
0 parents  commit 15689e4

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
input.txt

1/go.mod

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/jrtitus/aoc-2022/1
2+
3+
go 1.19
4+
5+
require github.com/spf13/cast v1.5.0

1/go.sum

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE=
2+
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
3+
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
4+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
5+
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
6+
github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w=
7+
github.com/spf13/cast v1.5.0/go.mod h1:SpXXQ5YoyJw6s3/6cMTQuxvgRl3PCJiyaX9p6b155UU=
8+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=

1/main.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
8+
"github.com/spf13/cast"
9+
)
10+
11+
var caloriesPerElf []int = make([]int, 0)
12+
var max int = 0
13+
14+
func appendSetMax(sum int) {
15+
// store sum
16+
caloriesPerElf = append(caloriesPerElf, sum)
17+
// determine max
18+
if sum > max {
19+
max = sum
20+
}
21+
}
22+
23+
func main() {
24+
// read file and load caloriesPerElf
25+
dat, err := os.ReadFile("input.txt")
26+
if err != nil {
27+
panic(err)
28+
}
29+
30+
strs := strings.Split(string(dat), "\n")
31+
32+
var sum int
33+
for _, str := range strs {
34+
sum += cast.ToInt(str)
35+
if str == "" {
36+
appendSetMax(sum)
37+
sum = 0
38+
}
39+
}
40+
appendSetMax(sum)
41+
42+
fmt.Printf("there are a total of %d elves\n", len(caloriesPerElf))
43+
fmt.Printf("max calories carried is %d", max)
44+
}

0 commit comments

Comments
 (0)