Skip to content

Commit 686c70c

Browse files
committed
add day 2
1 parent 94f9a32 commit 686c70c

File tree

2 files changed

+1076
-0
lines changed

2 files changed

+1076
-0
lines changed

d2.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package main
2+
3+
import (
4+
"math"
5+
"strconv"
6+
"strings"
7+
)
8+
9+
func d2isReportSafe(levels []string) bool {
10+
var lastDiff int64
11+
for i := 1; i < len(levels); i++ {
12+
last, _ := strconv.ParseInt(levels[i-1], 10, 64)
13+
curr, _ := strconv.ParseInt(levels[i], 10, 64)
14+
diff := curr - last
15+
if math.Abs(float64(diff)) > 3 {
16+
return false
17+
}
18+
if diff == 0 {
19+
return false
20+
}
21+
if (diff < 0 && lastDiff > 0) || (diff > 0 && lastDiff < 0) {
22+
return false
23+
}
24+
lastDiff = diff
25+
}
26+
return true
27+
}
28+
29+
func d2isReportSafeDampened(levels []string, idx int) bool {
30+
// yeah i know this is horrible but i literally woke up 10 minutes ago
31+
// and my adhd meds havent kicked in yet and i dont wanna think
32+
// i just want THE STAR
33+
if idx == len(levels) {
34+
return false
35+
}
36+
var dampened []string
37+
// fuck yeah i am doing it this way. don't cry
38+
for i, level := range levels {
39+
if i == idx {
40+
continue
41+
}
42+
dampened = append(dampened, level)
43+
}
44+
if d2isReportSafe(dampened) {
45+
return true
46+
}
47+
return d2isReportSafeDampened(levels, idx+1)
48+
}
49+
50+
func (*methods) D2P1(input string) string {
51+
reports := strings.Split(input, "\n")
52+
53+
var safe int
54+
for _, r := range reports {
55+
levels := strings.Split(r, " ")
56+
if d2isReportSafe(levels) {
57+
safe++
58+
}
59+
}
60+
61+
return strconv.Itoa(safe)
62+
}
63+
64+
func (*methods) D2P2(input string) string {
65+
reports := strings.Split(input, "\n")
66+
67+
var safe int
68+
for _, r := range reports {
69+
levels := strings.Split(r, " ")
70+
if d2isReportSafeDampened(levels, -1) {
71+
safe++
72+
}
73+
}
74+
75+
return strconv.Itoa(safe)
76+
}

0 commit comments

Comments
 (0)