summaryrefslogtreecommitdiff
path: root/day13.go
blob: f76767437bab28c9aa637557d42410bd1631ec6c (plain)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main

import (
	"bufio"
	"fmt"
	"log"
	"os"
	"strconv"
)

type (
	t     interface{}
	empty struct{}
)

type packet struct {
	data []t
}

func intOrList(v t) int {
	switch v.(type) {
	case int:
		return 0
	case []t:
		return 1
	default:
		return -1
	}
}

type compcode int

const (
	cont       compcode = -1
	rightOrder compcode = 0
	wrongOrder compcode = 1
)

func comparePackets(p1, p2 *packet, comp func(a, b int) compcode) (int, compcode) {
	var cp func(i int, ok compcode, a, b []t) (int, compcode)

	cp = func(i int, ok compcode, a, b []t) (int, compcode) {
		fmt.Println("Recurse", i, a, b, ok)
		if al, bl := i >= len(a), i >= len(b); al {
			if bl && al {
				fmt.Println("Both terminated at the same time", a, b, cont)
				return i, cont
			}
			if bl && !al {
				fmt.Println("Right terminated first", a, b, false)
				return i, wrongOrder
			}
			fmt.Println("Left terminated first", a, b, true)
			return i, rightOrder
		}

		if al, bl := i >= len(a), i >= len(b); bl {
			if bl && al {
				fmt.Println("Both terminated at the same time", a, b, cont)
				return i, cont
			}
			if bl && !al {
				fmt.Println("Right terminated first", a, b, false)
				return i, wrongOrder
			}
			fmt.Println("Left terminated first", a, b, true)
			return i, rightOrder
		}

		at, bt := intOrList(a[i]), intOrList(b[i])
		code := cont
		if at == 1 && bt == 1 {
			_, code = cp(0, ok, a[i].([]t), b[i].([]t))
		} else if at == 1 && bt == 0 {
			_, code = cp(0, ok, a[i].([]t), []t{b[i]})
		} else if at == 0 && bt == 1 {
			_, code = cp(0, ok, []t{a[i]}, b[i].([]t))
		} else if at == 0 && bt == 0 {
			code = comp(a[i].(int), b[i].(int))
		} else {
			log.Fatal("Unexpected input types")
		}

		if code != cont {
			return i, code
		}

		return cp((i + 1), code, a, b)
	}
	return cp(0, -1, p1.data, p2.data)
}

func parsePacket(b []byte) *packet {
	var parseList func(list []t, b []byte) []t

	parseList = func(list []t, b []byte) []t {
		for i := 0; i < len(b); i++ {
			if b[i] == '[' && b[i+1] == ']' {
				list = append(list, []t{})
			} else if b[i] == '[' {
				j := 1
				subchildren := 1
				for subchildren > 0 {
					if i+j >= len(b) {
						break
					}
					if b[i+j] == '[' {
						subchildren += 1
					}
					if b[i+j] == ']' {
						subchildren = subchildren - 1
					}
					j++
				}
				list = append(list, parseList([]t{}, b[i+1:i+j-1]))
				i = i + j
			} else {
				j := 0
				for ; i+j < len(b); j++ {
					stop := false
					switch b[i+j] {
					case ']':
						stop = true
					case ',':
						stop = true
					}
					if stop {
						break
					}
				}
				s := string(b[i : i+j])
				if len(s) == 0 {
					s = string(b[len(b)-1])
				}
				num, err := strconv.Atoi(s)
				if err != nil {
					continue
				}
				list = append(list, num)
				i = i + j
			}
		}
		return list
	}

	return &packet{data: parseList([]t{}, b[1:len(b)-1])}
}

func main() {
	f, err := os.Open("day13.txt")
	if err != nil {
		log.Fatal("Could not read input file")
	}

	s := bufio.NewScanner(f)
	packets := []*packet{}
	for s.Scan() {
		if len(s.Bytes()) > 0 {
			packets = append(packets, parsePacket(s.Bytes()))
		}
	}
	runningSum := 0
	for i := 0; i < len(packets); i += 2 {
		fmt.Println()
		fmt.Println(packets[i])
		fmt.Println(packets[i+1])
		_, code := comparePackets(packets[i], packets[i+1], func(a, b int) compcode {
			if a == b {
				fmt.Println("comp: continue", a, b)
				return cont
			}
			if a < b {
				fmt.Println("comp: rightOrder", a, b)
				return rightOrder
			} else {
				fmt.Println("comp: wrongOrder", a, b)
				return wrongOrder
			}
		})
		if code == rightOrder {
			fmt.Println(code, i/2+1)
			runningSum += (i / 2) + 1
		}
	}
	fmt.Println("Part 1:", runningSum)

	// fmt.Println("Part 2:", decoderKey)
}