Rename folders

This commit is contained in:
Javier-Orestis MANTZIOS
2025-01-08 14:00:10 +02:00
parent fa11582a25
commit 89ca6f60df
31 changed files with 0 additions and 0 deletions

71
01/main.go Normal file
View 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))
}