diff --git a/02dec/checksum.go b/02dec/checksum.go index 4fa697a..62a54cd 100644 --- a/02dec/checksum.go +++ b/02dec/checksum.go @@ -3,14 +3,14 @@ package main import ( "bufio" "fmt" + "math" "os" "strconv" "strings" ) -var sum int - func main() { + var sum int in := bufio.NewScanner(os.Stdin) for in.Scan() { sum += diff(in.Text()) @@ -19,19 +19,14 @@ func main() { } func diff(l string) int { - var min, max int - for n, s := range strings.Split(l, "\t") { + min, max := math.MaxInt64, 0 + for _, s := range strings.Split(l, "\t") { i, _ := strconv.Atoi(s) - if n == 0 { + if min >= i { min = i + } + if max <= i { max = i - } else { - if min > i { - min = i - } - if max < i { - max = i - } } } return max - min