Day 2: improve
This commit is contained in:
parent
6840e4d06f
commit
2b4f23bd5f
@ -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,20 +19,15 @@ 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 {
|
||||
min = i
|
||||
max = i
|
||||
} else {
|
||||
if min > i {
|
||||
if min >= i {
|
||||
min = i
|
||||
}
|
||||
if max < i {
|
||||
if max <= i {
|
||||
max = i
|
||||
}
|
||||
}
|
||||
}
|
||||
return max - min
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user