Day 2: improve

This commit is contained in:
Meutel 2017-12-02 19:38:59 +01:00
parent 6840e4d06f
commit 2b4f23bd5f

View File

@ -3,14 +3,14 @@ package main
import ( import (
"bufio" "bufio"
"fmt" "fmt"
"math"
"os" "os"
"strconv" "strconv"
"strings" "strings"
) )
var sum int
func main() { func main() {
var sum int
in := bufio.NewScanner(os.Stdin) in := bufio.NewScanner(os.Stdin)
for in.Scan() { for in.Scan() {
sum += diff(in.Text()) sum += diff(in.Text())
@ -19,20 +19,15 @@ func main() {
} }
func diff(l string) int { func diff(l string) int {
var min, max int min, max := math.MaxInt64, 0
for n, s := range strings.Split(l, "\t") { for _, s := range strings.Split(l, "\t") {
i, _ := strconv.Atoi(s) i, _ := strconv.Atoi(s)
if n == 0 { if min >= i {
min = i
max = i
} else {
if min > i {
min = i min = i
} }
if max < i { if max <= i {
max = i max = i
} }
} }
}
return max - min return max - min
} }