-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
136 lines (110 loc) · 3.23 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"fmt"
"math"
"os"
"sort"
"strconv"
"strings"
)
func read_file() string {
// read input.txt
content, err := os.ReadFile("input.txt")
if err != nil {
fmt.Println("Error reading file:", err)
return ""
}
return string(content)
}
// parse the content of the file into two arrays
// the content is two columns of numbers separated by multiple spaces.
// split the content by newlines and then split each line by spaces.
// in each row, the first number goes in array 1, and the second number goes in array 2.
// then the arrays are sorted in ascending order.
func parse_content(content string) ([]int, []int) {
// split by newlines
lines := strings.Split(content, "\n")
// define the two arrays
var arr1 []int
var arr2 []int
// split by spaces
for _, line := range lines {
numbers := strings.Split(line, " ")
// check if the line has two numbers
if len(numbers) != 2 {
continue
}
// convert to int and append to arrays
num1, err := strconv.Atoi(numbers[0])
if err != nil {
continue
}
num2, err := strconv.Atoi(numbers[1])
if err != nil {
continue
}
arr1 = append(arr1, num1)
arr2 = append(arr2, num2)
}
// sort the arrays
sort.Ints(arr1)
sort.Ints(arr2)
// return the arrays
return arr1, arr2
}
func calculate_differences(arr1 []int, arr2 []int) int {
// calculate the differences between the two arrays
// the differences are the sum of the absolute values of the differences between the two arrays
// the differences are calculated by subtracting the first array from the second array
// check if the arrays are the same length
if len(arr1) != len(arr2) {
return -1
}
// calculate the differences
differences := 0
for i := 0; i < len(arr1); i++ {
differences += int(math.Abs(float64(arr1[i] - arr2[i])))
}
return differences
}
func calculate_similarity(arr1 []int, arr2 []int) int {
// calculate the similarity between the two arrays
// the similarity is the occurrence of each number in arr1 in arr2
// so for example, if 3 is in arr1, and 3 is in arr2, then the similarity is 1
// if 3 is in arr1, and 3 is not in arr2, then the similarity is 0
// if 3 is in arr1 twice, and 3 is in arr2 once, then the similarity is 1
// if 3 is in arr1, and 3 is in arr2 twice, then the similarity is 6 (3*2)
// if 3 is in arr1 twice, and 3 is in arr2 twice, then the similarity is 6 (3*2 + 3*2)
// check if the arrays are the same length
if len(arr1) != len(arr2) {
return -1
}
// calculate the similarities
similarity := 0
for i := 0; i < len(arr1); i++ {
// get the next number in arr1
num := arr1[i]
// check how many times the number occurs in arr2
count := 0
for j := 0; j < len(arr2); j++ {
if arr2[j] == num {
count++
}
}
// add the similarity to the total
similarity += num * count
}
return similarity
}
func main() {
// get file content into a variable
content := read_file()
// parse the content into two arrays
arr1, arr2 := parse_content(content)
// calculate the differences between the two arrays
differences := calculate_differences(arr1, arr2)
fmt.Println("Differences:", differences)
// calculate the similarity between the two arrays
similarity := calculate_similarity(arr1, arr2)
fmt.Println("Similarity:", similarity)
}