-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
106 lines (82 loc) · 1.56 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
package main
import (
lib "github.com/teivah/advent-of-code"
"io"
"strings"
)
func fs1(input io.Reader) int {
s := lib.ReaderToString(input)
directions := strings.Split(s, ",")
d := make(map[string]int)
for _, direction := range directions {
d[direction]++
}
// Horizontal
remove(d, "n", "s")
// Diagonal 1
remove(d, "ne", "sw")
// Diagonal 2
remove(d, "nw", "se")
clean(d, "ne", "s")
clean(d, "ne", "nw")
clean(d, "se", "n")
clean(d, "se", "sw")
clean(d, "sw", "n")
clean(d, "sw", "se")
clean(d, "nw", "s")
clean(d, "nw", "ne")
sum := 0
for _, v := range d {
sum += v
}
return sum
}
func remove(d map[string]int, a, b string) {
min := lib.Min(d[a], d[b])
d[a] -= min
d[b] -= min
}
func clean(d map[string]int, a, b string) {
min := lib.Min(d[a], d[b])
if d[a] == min {
d[a] = 0
} else {
d[b] = 0
}
}
func fs2(input io.Reader) int {
s := lib.ReaderToString(input)
directions := strings.Split(s, ",")
d := make(map[string]int)
max := 0
for _, direction := range directions {
d[direction]++
max = lib.Max(max, distance(d))
}
return max
}
func distance(d map[string]int) int {
res := make(map[string]int, len(d))
for k, v := range d {
res[k] = v
}
// Horizontal
remove(res, "n", "s")
// Diagonal 1
remove(res, "ne", "sw")
// Diagonal 2
remove(res, "nw", "se")
clean(res, "ne", "s")
clean(res, "ne", "nw")
clean(res, "se", "n")
clean(res, "se", "sw")
clean(res, "sw", "n")
clean(res, "sw", "se")
clean(res, "nw", "s")
clean(res, "nw", "ne")
sum := 0
for _, v := range res {
sum += v
}
return sum
}