Code for day1, 2 in golang
This commit is contained in:
83
go/1/main.go
Normal file
83
go/1/main.go
Normal file
@ -0,0 +1,83 @@
|
||||
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)
|
||||
|
||||
}
|
Reference in New Issue
Block a user