aoc22/go/1/main.go

83 lines
1.3 KiB
Go

package main
import (
"bufio"
"fmt"
"github.com/emirpasic/gods/trees/binaryheap"
"os"
"path/filepath"
"strconv"
)
var (
f = "../../1.input.txt"
)
func must(e error) {
if e != nil {
panic(e)
}
}
func min(a, b interface{}) int {
c1 := a.(int)
c2 := b.(int)
switch {
case c1 < c2:
return 1
case c2 > c1:
return -1
default:
return 0
}
}
func main() {
fp, err:= filepath.Abs(f)
must(err)
rf, err := os.Open(fp)
must(err)
s := bufio.NewScanner(rf)
s.Split(bufio.ScanLines)
max := 0
mh := binaryheap.NewWith(min)
b := []string{}
for s.Scan() {
l := s.Text()
switch l {
case "":
sum := 0
for _, a := range b {
i, err := strconv.Atoi(a)
must(err)
sum = sum + i
}
mh.Push(sum)
if max < sum {
max = sum
}
b = []string{}
default:
b = append(b, l)
}
}
fmt.Println("max:", max)
lastN := []int{}
sum3 := 0
for range []int{1,2,3} {
e, _ := mh.Pop()
value := (e).(int)
lastN = append(lastN, value)
sum3 = sum3 + value
}
fmt.Println("last 3:", lastN, sum3)
}