-
Notifications
You must be signed in to change notification settings - Fork 0
/
d2.go
76 lines (67 loc) · 1.51 KB
/
d2.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
package main
import (
"math"
"strconv"
"strings"
)
func d2isReportSafe(levels []string) bool {
var lastDiff int64
for i := 1; i < len(levels); i++ {
last, _ := strconv.ParseInt(levels[i-1], 10, 64)
curr, _ := strconv.ParseInt(levels[i], 10, 64)
diff := curr - last
if math.Abs(float64(diff)) > 3 {
return false
}
if diff == 0 {
return false
}
if (diff < 0 && lastDiff > 0) || (diff > 0 && lastDiff < 0) {
return false
}
lastDiff = diff
}
return true
}
func d2isReportSafeDampened(levels []string, idx int) bool {
// yeah i know this is horrible but i literally woke up 10 minutes ago
// and my adhd meds havent kicked in yet and i dont wanna think
// i just want THE GOLD STAR (or better the kolibri)
if idx == len(levels) {
return false
}
var dampened []string
// fuck yeah i am doing it this way. don't cry
for i, level := range levels {
if i == idx {
continue
}
dampened = append(dampened, level)
}
if d2isReportSafe(dampened) {
return true
}
return d2isReportSafeDampened(levels, idx+1)
}
func (*methods) D2P1(input string) string {
reports := strings.Split(input, "\n")
var safe int
for _, r := range reports {
levels := strings.Split(r, " ")
if d2isReportSafe(levels) {
safe++
}
}
return strconv.Itoa(safe)
}
func (*methods) D2P2(input string) string {
reports := strings.Split(input, "\n")
var safe int
for _, r := range reports {
levels := strings.Split(r, " ")
if d2isReportSafeDampened(levels, -1) {
safe++
}
}
return strconv.Itoa(safe)
}