From 2b4f23bd5f0e8f1c832ab99962d5c6e316cdb4db Mon Sep 17 00:00:00 2001 From: Meutel Date: Sat, 2 Dec 2017 19:38:59 +0100 Subject: [PATCH] Day 2: improve --- 02dec/checksum.go | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) 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