64 lines
968 B
Go
64 lines
968 B
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"log"
|
|
"math"
|
|
"os"
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
func main() {
|
|
file, err := os.Open("./input.txt")
|
|
if err != nil {
|
|
log.Fatalf("failed to open file: %s\n", err)
|
|
}
|
|
defer file.Close()
|
|
s := bufio.NewScanner(file)
|
|
|
|
l := []int{}
|
|
r := []int{}
|
|
|
|
result1 := 0
|
|
result2 := 0
|
|
|
|
for s.Scan() {
|
|
t := s.Text()
|
|
strs := strings.Split(t, " ")
|
|
|
|
lval, err := strconv.Atoi(strs[0])
|
|
if err != nil {
|
|
log.Fatalf("failed to convert string to int: %s\n", err)
|
|
}
|
|
rval, err := strconv.Atoi(strs[1])
|
|
if err != nil {
|
|
log.Fatalf("failed to convert string to int: %s\n", err)
|
|
}
|
|
|
|
l = append(l, lval)
|
|
r = append(r, rval)
|
|
}
|
|
|
|
slices.Sort(l)
|
|
slices.Sort(r)
|
|
|
|
for i, num := range l {
|
|
left := num
|
|
right := r[i]
|
|
|
|
result1 += int(math.Abs(float64(left) - float64(right)))
|
|
|
|
for _, x := range r {
|
|
if x == left {
|
|
result2 += x
|
|
}
|
|
}
|
|
}
|
|
|
|
fmt.Println("Result1: ", result1)
|
|
fmt.Println("Result2: ", result2)
|
|
}
|