added 2024 part 2
This commit is contained in:
6
2024/01/Go/example.txt
Normal file
6
2024/01/Go/example.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3
|
||||
3
2024/01/Go/go.mod
Normal file
3
2024/01/Go/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module ch/timofey
|
||||
|
||||
go 1.23.4
|
||||
1000
2024/01/Go/input.txt
Normal file
1000
2024/01/Go/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
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