feat: day 1 first part done

This commit is contained in:
2024-12-01 12:58:11 +01:00
commit fe8e661b1f
6 changed files with 1085 additions and 0 deletions
+12
View File
@@ -0,0 +1,12 @@
package main
import (
"aoc2024/internal/common"
"aoc2024/internal/day01"
)
func main() {
input := common.LoadInputs("01_1.txt")
result := day01.Handle(input)
println(result)
}
+3
View File
@@ -0,0 +1,3 @@
module aoc2024
go 1.23.3
+1000
View File
File diff suppressed because it is too large Load Diff
View File
+35
View File
@@ -0,0 +1,35 @@
package common
import (
"fmt"
"log"
"os"
"strconv"
"strings"
)
func Must[T any](obj T, err error) T {
if err != nil {
log.Fatal(err)
}
return obj
}
func LoadInputs(input string) []string {
fmt.Println("Opening file...")
bytes := Must(os.ReadFile("././inputs/" + input))
content := strings.Split(string(bytes), "\n")
return content[:len(content)-1]
}
func SumIntArr(arr []int) int {
sum := 0
for _, v := range arr {
sum += v
}
return sum
}
func StrToInt(str string) int {
return Must(strconv.Atoi(str))
}
+35
View File
@@ -0,0 +1,35 @@
package day01
import (
"math"
"sort"
"strings"
"aoc2024/internal/common"
)
func Handle(input []string) int {
list1 := make([]int, len(input))
list2 := make([]int, len(input))
distance := 0
for i, line := range input {
cols := strings.Split(line, " ")
list1[i] = common.StrToInt(cols[0])
list2[i] = common.StrToInt(cols[1])
}
sort.Slice(list1, func(i, j int) bool {
return list1[i] < list1[j]
})
sort.Slice(list2, func(i, j int) bool {
return list2[i] < list2[j]
})
for i, v := range list1 {
distance = distance + int((math.Abs(float64(v - list2[i]))))
}
return distance
}