added 2024 part 2
This commit is contained in:
51
2024/01/Go/main.go
Normal file
51
2024/01/Go/main.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"os"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func check(e error) {
|
||||
if e != nil {
|
||||
panic(e)
|
||||
}
|
||||
}
|
||||
|
||||
func sumArray(list []int) int {
|
||||
result := 0
|
||||
for _, item := range list{
|
||||
result += item
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func main() {
|
||||
data, err := os.ReadFile("./input.txt")
|
||||
check(err)
|
||||
//fmt.Println(string(data))
|
||||
dataRows := strings.Split(string(data), "\r\n")
|
||||
var leftList []int
|
||||
var rightList []int
|
||||
var diffList []int
|
||||
//fmt.Println(dataRows)
|
||||
for _, row := range dataRows {
|
||||
l, _ := strconv.Atoi(strings.Split(row, " ")[0])
|
||||
r, _ := strconv.Atoi(strings.Split(row, " ")[1])
|
||||
leftList = append(leftList, l)
|
||||
rightList = append(rightList, r)
|
||||
}
|
||||
//fmt.Println(rightList)
|
||||
slices.Sort(leftList)
|
||||
slices.Sort(rightList)
|
||||
//fmt.Println(leftList)
|
||||
//fmt.Println(rightList)
|
||||
for idx, l := range leftList {
|
||||
diffList = append(diffList, int(math.Abs(float64(rightList[idx] - l))))
|
||||
}
|
||||
//fmt.Println(diffList)
|
||||
fmt.Println(sumArray(diffList))
|
||||
}
|
||||
Reference in New Issue
Block a user