Rename folders
This commit is contained in:
71
01/main.go
Normal file
71
01/main.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"slices"
|
||||
)
|
||||
|
||||
func totalDistance(firstList, secondList []int) int {
|
||||
slices.Sort(firstList)
|
||||
slices.Sort(secondList)
|
||||
// fmt.Println(thirdList)
|
||||
result := 0
|
||||
for i := range firstList {
|
||||
if firstList[i] > secondList[i] {
|
||||
result = result + firstList[i] - secondList[i]
|
||||
} else {
|
||||
result = result + secondList[i] - firstList[i]
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func similarityScore(firstList, secondList []int) int {
|
||||
result := 0
|
||||
appearances := []int{}
|
||||
for i:=range firstList {
|
||||
count := 0
|
||||
for j:=range secondList {
|
||||
if firstList[i] == secondList[j] {
|
||||
count = count + 1
|
||||
}
|
||||
}
|
||||
appearances = append(appearances, count)
|
||||
result = result + firstList[i] * count
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func main() {
|
||||
filePath := "input"
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
fmt.Printf("Error opening file: %v\n", err)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
var firstList []int
|
||||
var secondList []int
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
// Split the line into parts
|
||||
line := scanner.Text()
|
||||
parts := strings.Fields(line) // Fields splits by whitespace
|
||||
// Convert parts to integers
|
||||
first, err1 := strconv.Atoi(parts[0])
|
||||
second, err2 := strconv.Atoi(parts[1])
|
||||
if err1 != nil || err2 != nil {
|
||||
fmt.Printf("Error parsing numbers on line: %s\n", line)
|
||||
continue
|
||||
}
|
||||
firstList = append(firstList, first)
|
||||
secondList = append(secondList, second)
|
||||
}
|
||||
fmt.Println(totalDistance(firstList, secondList))
|
||||
fmt.Println(similarityScore(firstList, secondList))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user