-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart_1.go
81 lines (68 loc) · 1.59 KB
/
part_1.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
package main
import (
"bufio"
"log"
"os"
"sort"
"strconv"
"strings"
)
// parseInput reads the input file and splits the integers into two lists
func parseInput() ([]int, []int) {
filePath := "D:/Projects/advent-of-code/2024/Go/Day1/input.txt"
// Open the file
file, err := os.Open(filePath)
if err != nil {
log.Fatalf("Failed to open file: %v", err)
}
defer file.Close()
// Initialize slices for data, left, and right
data := []int{}
left := []int{}
right := []int{}
// Read the file line by line
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
numbers := strings.Fields(line) // Split line into fields
for _, n := range numbers {
value, err := strconv.Atoi(n) // Convert string to int
if err == nil {
data = append(data, value)
}
}
}
if err := scanner.Err(); err != nil {
log.Fatalf("Error reading file: %v", err)
}
// Distribute elements into left and right slices
for index, value := range data {
if index%2 == 0 {
left = append(left, value)
} else {
right = append(right, value)
}
}
return left, right
}
// totalDistance calculates the sum of absolute differences between two sorted lists
func totalDistance() int {
// Get the left and right lists from parseInput
left, right := parseInput()
// Sort both lists
sort.Ints(left)
sort.Ints(right)
// Calculate the total distance
totalDifference := 0
for i := 0; i < len(left); i++ {
totalDifference += abs(left[i] - right[i])
}
return totalDifference
}
// abs returns the absolute value of an integer
func abs(x int) int {
if x < 0 {
return -x
}
return x
}