2022 Day 01 Complete!
This commit is contained in:
44
2022/day01/main.go
Normal file
44
2022/day01/main.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
h "git.bullercodeworks.com/brian/adventofcode/helpers"
|
||||
)
|
||||
|
||||
func main() {
|
||||
inp := h.StdinToStringSlice()
|
||||
fmt.Println("# Answer")
|
||||
solve(inp)
|
||||
}
|
||||
|
||||
func solve(input []string) {
|
||||
var elves []int
|
||||
var top1, top2, top3 int
|
||||
elf := 0
|
||||
var sum int
|
||||
record := func() {
|
||||
elves = append(elves, sum)
|
||||
if elf != 0 && sum > elves[top1] {
|
||||
top3, top2, top1 = top2, top1, elf
|
||||
} else if sum > elves[top2] {
|
||||
top3, top2 = top2, elf
|
||||
} else if sum > elves[top3] {
|
||||
top3 = elf
|
||||
}
|
||||
elf++
|
||||
sum = 0
|
||||
}
|
||||
for i := 0; i < len(input); i++ {
|
||||
if input[i] == "" {
|
||||
record()
|
||||
continue
|
||||
}
|
||||
sum = sum + h.Atoi(input[i])
|
||||
}
|
||||
record()
|
||||
fmt.Printf("The elf with the most calories is #%d (%d)\n", top1+1, elves[top1])
|
||||
fmt.Printf("The elf with the second most calories is #%d (%d)\n", top2+1, elves[top2])
|
||||
fmt.Printf("The elf with the third most calories is #%d (%d)\n", top3+1, elves[top3])
|
||||
fmt.Printf("Total by top three: %d\n", elves[top1]+elves[top2]+elves[top3])
|
||||
}
|
||||
Reference in New Issue
Block a user