-
Notifications
You must be signed in to change notification settings - Fork 0
/
day1.go
78 lines (65 loc) · 1.37 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
76
77
78
package main
import (
"bufio"
"os"
"slices"
"sort"
"strconv"
"strings"
"time"
)
func day1() {
list1, list2 := loadInputDay1()
day1part1(list1, list2)
day1part2(list1, list2)
}
func loadInputDay1() ([]int, []int) {
defer timeTrack(time.Now(), "Loading/Parsing Input")
file, err := os.Open("inputs/day1.txt")
if err != nil {
panic(err)
}
scanner := bufio.NewScanner(file)
list1 := make([]int, 0)
list2 := make([]int, 0)
for scanner.Scan() {
var line = scanner.Text()
split := strings.Split(line, " ")
if len(split) == 2 {
var val1, _ = strconv.Atoi(split[0])
var val2, _ = strconv.Atoi(split[1])
list1 = append(list1, val1)
list2 = append(list2, val2)
}
}
return list1, list2
}
func day1part1(list1, list2 []int) {
defer timeTrack(time.Now(), "Part1")
sort.Ints(list1)
sort.Ints(list2)
var totalDifference = 0
for i := 0; i < len(list1); i++ {
lineDifference := list1[i] - list2[i]
totalDifference += max(lineDifference, -lineDifference)
}
println(totalDifference)
}
func day1part2(list1, list2 []int) {
defer timeTrack(time.Now(), "Part2")
var similarityScore = 0
for i := 0; i < len(list1); i++ {
var n = list1[i]
position, found := slices.BinarySearch(list2, n)
if !found {
continue
}
var count = 0
for list2[position] == n {
count++
position++
}
similarityScore += n * count
}
println(similarityScore)
}