2024/day1/puzzle1.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
package main import ( "bufio" "fmt" "os" "slices" "strconv" "strings" ) func main() { var left []int var right []int file, _ := os.Open("input1.txt") defer file.Close() scanner := bufio.NewScanner(file) for scanner.Scan() { line := scanner.Text() fields := strings.Split(line, " ") l, _ := strconv.Atoi(fields[0]) left = append(left, l) r, _ := strconv.Atoi(fields[1]) right = append(right, r) slices.Sort(left) slices.Sort(right) } var total int for i := range left { d := difference(left[i], right[i]) total += d } fmt.Println(total) } // calc difference between 2 inties func difference(int1 int, int2 int) int { s := []int{int1, int2} slices.Sort(s) return s[1] - s[0] }