-
Notifications
You must be signed in to change notification settings - Fork 1
/
12.go
51 lines (41 loc) · 945 Bytes
/
12.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"reflect"
)
func process(data interface{}, ignoreRed bool) int {
total := 0
dataType := reflect.TypeOf(data)
if dataType == reflect.TypeOf(make([]interface{}, 0)) {
for _, item := range data.([]interface{}) {
total += process(item, ignoreRed)
}
return total
}
if dataType == reflect.TypeOf(make(map[string]interface{}, 0)) {
for _, item := range data.(map[string]interface{}) {
if ignoreRed && item == "red" {
return 0
}
total += process(item, ignoreRed)
}
return total
}
if dataType == reflect.TypeOf(0.0) {
return int(data.(float64))
}
return 0
}
func main() {
data, err := ioutil.ReadFile("input/12.txt")
if err != nil {
log.Fatal(err)
}
var result []interface{}
json.Unmarshal(data, &result)
fmt.Println(process(result, false))
fmt.Println(process(result, true))
}