adventofcode/02dec/checksum.go

34 lines
416 B
Go
Raw Normal View History

2017-12-02 18:26:28 +00:00
package main
import (
"bufio"
"fmt"
"math"
"os"
"strconv"
"strings"
)
func main() {
var sum int
in := bufio.NewScanner(os.Stdin)
for in.Scan() {
sum += diff(in.Text())
}
fmt.Println(sum)
}
func diff(l string) int {
min, max := math.MaxInt64, 0
for _, s := range strings.Split(l, "\t") {
i, _ := strconv.Atoi(s)
if min >= i {
min = i
}
if max <= i {
max = i
}
}
return max - min
}