aboutsummaryrefslogtreecommitdiff
path: root/2021/16/bits/scanner.go
blob: 296705d3871939189b450699a4de02a50339b5ad (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package bits

import (
	"bufio"
	"fmt"
	"io"
)

type Bits struct {
	Version   byte
	TypeID    byte
	Value     int
	Operators []*Bits
	Len       int
}

// Scanner represents a lexical scanner.
type Scanner struct {
	r            *bufio.Reader
	buff         byte
	readBits     byte
	parsingDepth int
}

// NewScanner returns a new instance of Scanner.
func NewScanner(r io.Reader) *Scanner {
	return &Scanner{r: bufio.NewReader(r)}
}

// read reads the next bits from the bufferred reader.
// Returns the rune(0) if an error occurs (or io.EOF is returned).
func (s *Scanner) readBit() (bool, error) {
	var ret bool
	s.readBits++
	switch s.readBits {
	case 2:
		ret = s.buff&0b0100 != 0
	case 3:
		ret = s.buff&0b0010 != 0
	case 4:
		ret = s.buff&0b0001 != 0
	default:
		var err error
		s.buff, err = s.r.ReadByte()
		if err != nil || s.buff == '\n' {
			return false, err
		}
		if s.buff <= '9' {
			s.buff -= '0'
		} else {
			s.buff = s.buff - 'A' + 10
		}
		if err != nil {
			return false, err
		}
		s.readBits = 1
		ret = s.buff&0b1000 != 0
	}
	if ret {
		fmt.Print("1")
	} else {
		fmt.Print("0")
	}
	return ret, nil
}

// Scan returns the next Bits packet
func (s *Scanner) Scan() *Bits {
	var bits Bits
	for i := 0; i < 3; i++ {
		bits.Version <<= 1
		b, err := s.readBit()
		if err != nil {
			return nil
		}
		if b {
			bits.Version++
		}
	}
	fmt.Println("Version:", bits.Version)
	for i := 0; i < 3; i++ {
		bits.TypeID <<= 1
		b, err := s.readBit()
		if err != nil {
			return nil
		}
		if b {
			bits.TypeID++
		}
	}
	bits.Len += 6
	fmt.Println("TypeID:", bits.TypeID)
	if bits.TypeID == 4 {
		for {
			keepGoing, err := s.readBit()
			if err != nil {
				return nil
			}
			var buff byte
			for i := 0; i < 4; i++ {
				buff <<= 1
				b, err := s.readBit()
				if err != nil {
					return nil
				}
				if b {
					buff++
				}
			}
			bits.Len += 5
			bits.Value = bits.Value<<4 + int(buff)
			if !keepGoing {
				break
			}
		}
		fmt.Println("Value:", bits.Value)
	} else { // operator
		b, err := s.readBit()
		if err != nil {
			return nil
		}
		bits.Len++
		if b {
			var buff int
			for i := 0; i < 11; i++ {
				buff <<= 1
				b, err := s.readBit()
				if err != nil {
					return nil
				}
				if b {
					buff++
				}
			}
			bits.Len += 11
			fmt.Println("SubPackets type 1, nb of subpackets:", buff)
			s.parsingDepth++
			for i := 0; i < buff; i++ {
				subBits := s.Scan()
				if subBits == nil {
					return nil
				}
				bits.Operators = append(bits.Operators, subBits)
				bits.Len += subBits.Len
			}
			s.parsingDepth--
		} else {
			var buff int
			for i := 0; i < 15; i++ {
				buff <<= 1
				b, err := s.readBit()
				if err != nil {
					return nil
				}
				if b {
					buff++
				}
			}
			bits.Len += 15
			fmt.Println("SubPackets type 0 of len:", buff)
			s.parsingDepth++
			for buff > 0 {
				subBits := s.Scan()
				if subBits == nil {
					return nil
				}
				bits.Operators = append(bits.Operators, subBits)
				bits.Len += subBits.Len
				buff -= subBits.Len
				fmt.Println("remaining bits:", buff)
			}
			s.parsingDepth--
		}
	}
	if s.parsingDepth == 0 {
		bits.Len += 4 - int(s.readBits)
		s.readBits = 0
	}
	switch bits.TypeID {
	case 0:
		for _, sub := range bits.Operators {
			bits.Value += sub.Value
		}
	case 1:
		bits.Value = 1
		for _, sub := range bits.Operators {
			bits.Value *= sub.Value
		}
	case 2:
		bits.Value = bits.Operators[0].Value
		for _, sub := range bits.Operators {
			if bits.Value > sub.Value {
				bits.Value = sub.Value
			}
		}
	case 3:
		bits.Value = bits.Operators[0].Value
		for _, sub := range bits.Operators {
			if bits.Value < sub.Value {
				bits.Value = sub.Value
			}
		}
	case 5:
		if bits.Operators[0].Value > bits.Operators[1].Value {
			bits.Value = 1
		}
	case 6:
		if bits.Operators[0].Value < bits.Operators[1].Value {
			bits.Value = 1
		}
	case 7:
		if bits.Operators[0].Value == bits.Operators[1].Value {
			bits.Value = 1
		}
	}
	return &bits
}