-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
135 lines (118 loc) · 2.72 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"encoding/json"
"fmt"
. "math"
utils "github.com/baspar/adventofcode2022/internal"
"github.com/baspar/adventofcode2022/internal/math"
)
const (
CorrectOrder = iota
ContinueChecking
IncorrectOrder
)
type DayImpl struct {
comparisons []Comparison
}
type Node struct {
val *int
array []Node
marked bool
}
func (n Node) IsArray() bool {
return n.val == nil
}
func (n Node) ToArrayNode() Node {
if n.IsArray() {
return n
} else {
return Node{array: []Node{n}}
}
}
func (left Node) isInOrderWith(right Node) int {
if left.IsArray() || right.IsArray() {
leftArray, rightArray := left.ToArrayNode(), right.ToArrayNode()
for i := 0; i < math.Min(len(leftArray.array), len(rightArray.array)); i++ {
if status := leftArray.array[i].isInOrderWith(rightArray.array[i]); status == ContinueChecking {
continue
} else {
return status
}
}
if len(rightArray.array) == len(leftArray.array) {
return ContinueChecking
} else if len(rightArray.array) > len(leftArray.array) {
return CorrectOrder
} else {
return IncorrectOrder
}
}
if *left.val == *right.val {
return ContinueChecking
} else if *left.val < *right.val {
return CorrectOrder
} else {
return IncorrectOrder
}
}
func (left Node) IsInOrderWith(right Node) bool {
return left.isInOrderWith(right) != IncorrectOrder
}
func from(items []any) (node Node) {
for _, item := range items {
var child Node
if arr, isArr := item.([]any); isArr {
child.array = append(node.array, from(arr))
} else if n, isNumber := item.(float64); isNumber {
asInt := int(Round(n))
child.val = &asInt
}
node.array = append(node.array, child)
}
return node
}
func From(line string) Node {
var items []any
json.Unmarshal([]byte(line), &items)
return from(items)
}
type Comparison struct {
left Node
right Node
}
func (d *DayImpl) Init(lines []string) error {
for i := 0; i < len(lines); i += 3 {
d.comparisons = append(d.comparisons, Comparison{
left: From(lines[i]),
right: From(lines[i+1]),
})
}
return nil
}
func (d *DayImpl) Part1() (string, error) {
sum := 0
for i, comparison := range d.comparisons {
if comparison.left.IsInOrderWith(comparison.right) {
sum += i + 1
}
}
return fmt.Sprint(sum), nil
}
func (d *DayImpl) Part2() (string, error) {
specialNode1, positionNode1 := From("[[2]]"), 1
specialNode2, positionNode2 := From("[[6]]"), 2
for _, comparison := range d.comparisons {
for _, node := range []Node{comparison.left, comparison.right} {
if node.IsInOrderWith(specialNode1) {
positionNode1++
}
if node.IsInOrderWith(specialNode2) {
positionNode2++
}
}
}
return fmt.Sprint(positionNode1 * positionNode2), nil
}
func main() {
utils.Run(&DayImpl{}, false)
}