-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
130 lines (117 loc) · 2.83 KB
/
main.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"embed"
"fmt"
"math"
"slices"
"strconv"
"strings"
)
type LocationId = uint
var data embed.FS
func main() {
locationList1, locationList2 := loadLocationLists()
listDifference := reconcileLists(locationList1, locationList2)
fmt.Printf("Total distance of location lists is %d\n", listDifference)
similarityScore := calculateSimilarityScore(locationList1, locationList2)
fmt.Printf("Similarity score is %d\n", similarityScore)
reports := loadReports()
safeReports := filterSafeReports(reports)
fmt.Printf("Total safe reports %d\n", safeReports)
}
func loadLocationLists() ([]LocationId, []LocationId) {
lines, rErr := readLines("./data/location_input.txt")
if rErr != nil {
panic(rErr)
}
list1 := make([]LocationId, len(lines))
list2 := make([]LocationId, len(lines))
for i, line := range lines {
parts := strings.Split(line, " ")
v1, _ := strconv.Atoi(parts[0])
v2, _ := strconv.Atoi(parts[1])
list1[i] = LocationId(v1)
list2[i] = LocationId(v2)
}
return list1, list2
}
func reconcileLists(list1 []LocationId, list2 []LocationId) uint {
slices.Sort(list1)
slices.Sort(list2)
var totalDiffs uint = 0
for i := 0; i < len(list1); i++ {
if list1[i] > list2[i] {
totalDiffs = totalDiffs + uint(list1[i]-list2[i])
} else {
totalDiffs = totalDiffs + uint(list2[i]-list1[i])
}
}
return totalDiffs
}
func calculateSimilarityScore(list1 []LocationId, list2 []LocationId) uint {
freqMap := make(map[LocationId]uint)
for _, value := range list2 {
if count, ok := freqMap[value]; ok {
count++
freqMap[value] = count
} else {
freqMap[value] = 1
}
}
simScore := uint(0)
for _, value := range list1 {
if count, ok := freqMap[value]; ok {
simScore += value * count
}
}
return simScore
}
func loadReports() []string {
reports, rErr := readLines("./data/report_input.txt")
if rErr != nil {
panic(rErr)
}
return reports
}
type LevelDirection = int
var (
LevelDirectionUnset LevelDirection = 2
LevelDirectionIncreasing = 1
LevelDirectionDecreasing = 0
)
func filterSafeReports(reports []string) int {
safeCount := 0
for _, report := range reports {
direction := LevelDirectionUnset
safe := true
levels := strings.Split(report, " ")
for i := 0; i < len(levels)-1; i++ {
curr, _ := strconv.Atoi(levels[i])
next, _ := strconv.Atoi(levels[i+1])
diff := curr - next
absDiff := math.Abs(float64(diff))
if absDiff < 1 || absDiff > 3 {
safe = false
break
}
var diffDirection LevelDirection
if diff > 0 {
diffDirection = LevelDirectionIncreasing
} else {
diffDirection = LevelDirectionDecreasing
}
if direction != LevelDirectionUnset {
if diffDirection != direction {
safe = false
break
}
} else {
direction = diffDirection
}
}
if safe {
safeCount++
}
}
return safeCount
}