-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday1.go
75 lines (62 loc) · 1.13 KB
/
day1.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
// [Day 1: Historian Hysteria](https://adventofcode.com/2024/day/1)
package main
import (
"bufio"
"fmt"
"os"
"sort"
"strconv"
"strings"
)
func main() {
inputFile := "input.txt"
if len(os.Args) >= 2 {
inputFile = os.Args[1]
}
file, err := os.Open(inputFile)
if err != nil {
fmt.Println("error:", err)
return
}
defer file.Close()
var left, right []int
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
parts := strings.Fields(line)
a, _ := strconv.Atoi(parts[0])
b, _ := strconv.Atoi(parts[1])
left = append(left, a)
right = append(right, b)
}
if err := scanner.Err(); err != nil {
fmt.Println("error:", err)
return
}
// sort the lists
sort.Ints(left)
sort.Ints(right)
// part 1
part1 := 0
for i := 0; i < len(left); i++ {
part1 += abs(left[i] - right[i])
}
fmt.Println(part1)
// part 2
rightCount := make(map[int]int)
for _, v := range right {
rightCount[v]++
}
part2 := 0
for _, a := range left {
part2 += a * rightCount[a]
}
fmt.Println(part2)
}
// abs returns the absolute value of x.
func abs(x int) int {
if x < 0 {
return -x
}
return x
}