feat: day 1 first part done
This commit is contained in:
commit
fe8e661b1f
12
cmd/01/01.go
Normal file
12
cmd/01/01.go
Normal 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)
|
||||||
|
}
|
||||||
1000
inputs/01_1.txt
Normal file
1000
inputs/01_1.txt
Normal file
File diff suppressed because it is too large
Load Diff
0
inputs/01_2.txt
Normal file
0
inputs/01_2.txt
Normal file
35
internal/common/common.go
Normal file
35
internal/common/common.go
Normal 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
internal/day01/handle.go
Normal file
35
internal/day01/handle.go
Normal 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
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user