-
Notifications
You must be signed in to change notification settings - Fork 0
/
d3.go
74 lines (69 loc) · 1.46 KB
/
d3.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
package main
import (
"strconv"
"strings"
)
// yes. fuck regex, fuck token based parsing
// split split split
// i woke up after some silly dreams and the very next minute i grabbed my laptop
func (*methods) D3P1(input string) string {
parts := strings.Split(input, "mul(")
var total int64
for i, p := range parts {
if i == 0 {
continue
}
parts := strings.Split(p, ")")
parts = strings.Split(parts[0], ",")
if len(parts) != 2 {
continue
}
n1, err := strconv.ParseInt(parts[0], 10, 64)
if err != nil {
continue
}
n2, err := strconv.ParseInt(parts[1], 10, 64)
if err != nil {
continue
}
total += n1 * n2
}
return strconv.FormatInt(total, 10)
}
// i hope you are enjoying the beauty of golang error handling
func (*methods) D3P2(input string) string {
parts := strings.Split(input, "don't()")
var filtered string
for i, p := range parts {
if i == 0 {
filtered += p
continue
}
parts := strings.Split(p, "do()")
if len(parts) > 1 {
filtered += strings.Join(parts[1:], "")
}
}
parts = strings.Split(filtered, "mul(")
var total int64
for i, p := range parts {
if i == 0 {
continue
}
parts := strings.Split(p, ")")
parts = strings.Split(parts[0], ",")
if len(parts) != 2 {
continue
}
n1, err := strconv.ParseInt(parts[0], 10, 64)
if err != nil {
continue
}
n2, err := strconv.ParseInt(parts[1], 10, 64)
if err != nil {
continue
}
total += n1 * n2
}
return strconv.FormatInt(total, 10)
}