-
Notifications
You must be signed in to change notification settings - Fork 3
/
day13.go
131 lines (118 loc) · 2.66 KB
/
day13.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
package main
import (
_ "embed"
"fmt"
"sort"
"strings"
"time"
)
//go:embed input.txt
var inputDay string
const (
VALUE = 0
LIST = 1
)
type node struct {
id int
value int
children []node
}
func parseInt(s string, i int) (int, int) {
res := 0
for i < len(s) {
if s[i] == ',' || s[i] == ']' || s[i] == '[' {
return res, i
}
res = res*10 + int(s[i]-'0')
i++
}
return res, i
}
func parseList(s string, i int) (node, int) {
var current []node
i++ // skip [
for s[i] != ']' {
if s[i] == '[' {
n, j := parseList(s, i)
current = append(current, n)
i = j
} else if s[i] == ',' {
i++
} else {
v, j := parseInt(s, i)
current = append(current, node{id: VALUE, value: v})
i = j
}
}
i++ // skip ]
return node{id: LIST, children: current}, i
}
func compare(a, b node) int {
if a.id == VALUE && b.id == VALUE {
return b.value - a.value
}
if a.id == LIST && b.id == LIST {
for i := 0; i < len(a.children) && i < len(b.children); i++ {
res := compare(a.children[i], b.children[i])
if res != 0 {
return res
}
}
return len(b.children) - len(a.children)
}
if a.id == VALUE && b.id == LIST {
return compare(node{id: LIST, children: []node{a}}, b)
}
if a.id == LIST && b.id == VALUE {
return compare(a, node{id: LIST, children: []node{b}})
}
return 0
}
func Part1(input string) int {
input = strings.TrimSuffix(input, "\n")
parts := strings.Split(input, "\n\n")
index := 1
res := 0
for _, part := range parts {
lines := strings.Split(part, "\n")
t1, _ := parseList(lines[0], 0)
t2, _ := parseList(lines[1], 0)
b := compare(t1, t2) > 0
if b {
res += index
}
index++
}
return res
}
func Part2(input string) int {
input = strings.TrimSuffix(input, "\n")
lines := strings.Split(input, "\n")
lines = append(lines, "[[2]]")
lines = append(lines, "[[6]]")
var l []node
for _, line := range lines {
if len(line) == 0 {
continue
}
t, _ := parseList(line, 0)
l = append(l, t)
}
sort.Slice(l, func(i, j int) bool { return compare(l[i], l[j]) > 0 })
t1, _ := parseList("[[2]]", 0)
t2, _ := parseList("[[6]]", 0)
//i1 := sort.Search(len(l), func(i int) bool { return compare(l[i], t1) <= 0 })
//i2 := sort.Search(len(l), func(i int) bool { return compare(l[i], t2) <= 0 })
i1, _ := sort.Find(len(l), func(i int) int { return compare(l[i], t1) })
i2, _ := sort.Find(len(l), func(i int) int { return compare(l[i], t2) })
return (i1 + 1) * (i2 + 1)
}
func main() {
fmt.Println("--2022 day 13 solution--")
start := time.Now()
fmt.Println("part1: ", Part1(inputDay))
fmt.Println(time.Since(start))
start = time.Now()
fmt.Println("part2: ", Part2(inputDay))
fmt.Println(time.Since(start))
}