feat: Finish day 1
This commit is contained in:
parent
fe8e661b1f
commit
18f1bb5698
@ -7,6 +7,7 @@ import (
|
||||
|
||||
func main() {
|
||||
input := common.LoadInputs("01_1.txt")
|
||||
result := day01.Handle(input)
|
||||
println(result)
|
||||
result1, result2 := day01.Handle(input)
|
||||
println(result1)
|
||||
println(result2)
|
||||
}
|
||||
|
||||
@ -8,10 +8,13 @@ import (
|
||||
"aoc2024/internal/common"
|
||||
)
|
||||
|
||||
func Handle(input []string) int {
|
||||
func Handle(input []string) (int, int) {
|
||||
list1 := make([]int, len(input))
|
||||
list2 := make([]int, len(input))
|
||||
map1 := map[int]int{}
|
||||
map2 := map[int]int{}
|
||||
distance := 0
|
||||
similariry := 0
|
||||
|
||||
for i, line := range input {
|
||||
cols := strings.Split(line, " ")
|
||||
@ -28,8 +31,26 @@ func Handle(input []string) int {
|
||||
})
|
||||
|
||||
for i, v := range list1 {
|
||||
if _, ok := map1[v]; !ok {
|
||||
map1[v] = 1
|
||||
} else {
|
||||
map1[v] += 1
|
||||
}
|
||||
if _, ok := map2[list2[i]]; !ok {
|
||||
map2[list2[i]] = 1
|
||||
} else {
|
||||
map2[list2[i]] += 1
|
||||
}
|
||||
distance = distance + int((math.Abs(float64(v - list2[i]))))
|
||||
}
|
||||
|
||||
return distance
|
||||
for k, v := range map1 {
|
||||
map2Value := 0
|
||||
if value, ok := map2[k]; ok {
|
||||
map2Value = value
|
||||
}
|
||||
similariry += (k * map2Value) * v
|
||||
}
|
||||
|
||||
return distance, similariry
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user