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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
# https://adventofcode.com/2020/day/4
#
# To run (on Linux):
# $ git clone https://github.com/akkartik/mu
# $ cd mu
# $ ./translate advent2020/4b.mu
# $ ./a.elf < input
#
# You'll need to register to download the 'input' file for yourself.
fn main -> _/ebx: int {
var curr-passport-field-count/esi: int <- copy 0
var valid-passport-count/edi: int <- copy 0
var line-storage: (stream byte 0x100) # 256 bytes
var line/ecx: (addr stream byte) <- address line-storage
var key-slice-storage: slice
var key-slice/edx: (addr slice) <- address key-slice-storage
var val-slice-storage: slice
var val-slice/ebx: (addr slice) <- address val-slice-storage
$main:line-loop: {
# read line from stdin
clear-stream line
read-line-from-real-keyboard line
# if line is empty (not even a newline), quit
var done?/eax: boolean <- stream-empty? line
compare done?, 0/false
break-if-!=
print-stream-to-real-screen line
# if line has just a newline, process passport
skip-chars-matching-whitespace line
var new-passport?/eax: boolean <- stream-empty? line
{
compare new-passport?, 0/false
break-if-=
compare curr-passport-field-count, 7
{
break-if-!=
valid-passport-count <- increment
print-string 0, "=> "
print-int32-decimal 0, valid-passport-count
print-string 0, "\n"
}
curr-passport-field-count <- copy 0
loop $main:line-loop
}
$main:word-loop: {
skip-chars-matching-whitespace line
var done?/eax: boolean <- stream-empty? line
compare done?, 0/false
break-if-!=
next-token line, 0x3a, key-slice # ':'
var dummy/eax: byte <- read-byte line # skip ':'
next-raw-word line, val-slice
print-slice-to-real-screen key-slice
print-string 0, " : "
print-slice-to-real-screen val-slice
print-string 0, "\n"
# treat cid as optional
var cid?/eax: boolean <- slice-equal? key-slice, "cid"
compare cid?, 0/false
loop-if-!=
# increment field count
curr-passport-field-count <- increment
# - validate fields one by one, setting curr-passport-field-count to impossibly high value to signal invalid
# byr
{
var byr?/eax: boolean <- slice-equal? key-slice, "byr"
compare byr?, 0/false
break-if-=
# 1920 <= byr <= 2002
var byr/eax: int <- parse-decimal-int-from-slice val-slice
compare byr, 0x780 # 1920
{
break-if->=
print-string 0, "invalid\n"
curr-passport-field-count <- copy 8
}
compare byr, 0x7d2 # 2002
{
break-if-<=
print-string 0, "invalid\n"
curr-passport-field-count <- copy 8
}
}
# iyr
{
var iyr?/eax: boolean <- slice-equal? key-slice, "iyr"
compare iyr?, 0/false
break-if-=
# 2010 <= iyr <= 2020
var iyr/eax: int <- parse-decimal-int-from-slice val-slice
compare iyr, 0x7da # 2010
{
break-if->=
print-string 0, "invalid\n"
curr-passport-field-count <- copy 8
}
compare iyr, 0x7e4 # 2020
{
break-if-<=
print-string 0, "invalid\n"
curr-passport-field-count <- copy 8
}
}
# eyr
{
var eyr?/eax: boolean <- slice-equal? key-slice, "eyr"
compare eyr?, 0/false
break-if-=
# 2020 <= eyr <= 2030
var eyr/eax: int <- parse-decimal-int-from-slice val-slice
compare eyr, 0x7e4 # 2020
{
break-if->=
print-string 0, "invalid\n"
curr-passport-field-count <- copy 8
}
compare eyr, 0x7ee # 2030
{
break-if-<=
print-string 0, "invalid\n"
curr-passport-field-count <- copy 8
}
}
# hgt
{
var hgt?/eax: boolean <- slice-equal? key-slice, "hgt"
compare hgt?, 0/false
break-if-=
# convert val
var s: (handle array byte)
var s2/eax: (addr handle array byte) <- address s
_slice-to-string val-slice, s2
var s3/eax: (addr array byte) <- lookup *s2
var s4/ebx: (addr array byte) <- copy s3
# check suffix
var start/edx: int <- length s4
start <- subtract 2 # luckily both 'in' and 'cm' have the same length
{
var suffix-h: (handle array byte)
var suffix-ah/ecx: (addr handle array byte) <- address suffix-h
substring s4, start, 2, suffix-ah
var suffix/eax: (addr array byte) <- lookup *suffix-ah
{
var match?/eax: boolean <- string-equal? suffix, "in"
compare match?, 0/false
break-if-=
# if suffix is "in", 59 <= val <= 96
var num-h: (handle array byte)
var num-ah/ecx: (addr handle array byte) <- address num-h
substring s4, 0, start, num-ah
var num/eax: (addr array byte) <- lookup *num-ah
var val/eax: int <- parse-decimal-int num
compare val, 0x3b # 59
{
break-if->=
print-string 0, "invalid\n"
curr-passport-field-count <- copy 8
}
compare val, 0x60 # 96
{
break-if-<=
print-string 0, "invalid\n"
curr-passport-field-count <- copy 8
}
loop $main:word-loop
}
{
var match?/eax: boolean <- string-equal? suffix, "cm"
compare match?, 0/false
break-if-=
# if suffix is "cm", 150 <= val <= 193
var num-h: (handle array byte)
var num-ah/ecx: (addr handle array byte) <- address num-h
substring s4, 0, start, num-ah
var num/eax: (addr array byte) <- lookup *num-ah
var val/eax: int <- parse-decimal-int num
compare val, 0x96 # 150
{
break-if->=
print-string 0, "invalid\n"
curr-passport-field-count <- copy 8
}
compare val, 0xc1 # 193
{
break-if-<=
print-string 0, "invalid\n"
curr-passport-field-count <- copy 8
}
loop $main:word-loop
}
print-string 0, "invalid\n"
curr-passport-field-count <- copy 8
loop $main:word-loop
}
}
# hcl
{
var hcl?/eax: boolean <- slice-equal? key-slice, "hcl"
compare hcl?, 0/false
break-if-=
# convert val
var s: (handle array byte)
var s2/eax: (addr handle array byte) <- address s
_slice-to-string val-slice, s2
var s3/eax: (addr array byte) <- lookup *s2
# check length
var len/ebx: int <- length s3
compare len, 7
{
break-if-=
print-string 0, "invalid\n"
curr-passport-field-count <- copy 8
loop $main:word-loop
}
# check first byte
{
var c/eax: (addr byte) <- index s3, 0
var c2/eax: byte <- copy-byte *c
compare c2, 0x23/hash
break-if-=
print-string 0, "invalid2\n"
curr-passport-field-count <- copy 8
loop $main:word-loop
}
# check remaining bytes
var i/ebx: int <- copy 1 # skip 0
{
compare i, 7
break-if->=
var c/eax: (addr byte) <- index s3, i
{
var c2/eax: byte <- copy-byte *c
var valid?/eax: boolean <- hex-digit? c2
compare valid?, 0
loop-if-= $main:word-loop
}
i <- increment
loop
}
}
# ecl
{
var ecl?/eax: boolean <- slice-equal? key-slice, "ecl"
compare ecl?, 0/false
break-if-=
var amb?/eax: boolean <- slice-equal? val-slice, "amb"
compare amb?, 0/false
loop-if-!= $main:word-loop
var blu?/eax: boolean <- slice-equal? val-slice, "blu"
compare blu?, 0/false
loop-if-!= $main:word-loop
var brn?/eax: boolean <- slice-equal? val-slice, "brn"
compare brn?, 0/false
loop-if-!= $main:word-loop
var gry?/eax: boolean <- slice-equal? val-slice, "gry"
compare gry?, 0/false
loop-if-!= $main:word-loop
var grn?/eax: boolean <- slice-equal? val-slice, "grn"
compare grn?, 0/false
loop-if-!= $main:word-loop
var hzl?/eax: boolean <- slice-equal? val-slice, "hzl"
compare hzl?, 0/false
loop-if-!= $main:word-loop
var oth?/eax: boolean <- slice-equal? val-slice, "oth"
compare oth?, 0/false
loop-if-!= $main:word-loop
print-string 0, "invalid\n"
curr-passport-field-count <- copy 8
}
# pid
{
var pid?/eax: boolean <- slice-equal? key-slice, "pid"
compare pid?, 0/false
break-if-=
# convert val
var s: (handle array byte)
var s2/eax: (addr handle array byte) <- address s
_slice-to-string val-slice, s2
var s3/eax: (addr array byte) <- lookup *s2
# check length
var len/eax: int <- length s3
compare len, 9
{
break-if-=
print-string 0, "invalid\n"
curr-passport-field-count <- copy 8
loop $main:word-loop
}
# check valid decimal int
# parse-decimal-int-from-slice currently returns 0 on invalid parse,
# which isn't ideal but suffices for our purposes
var val/eax: int <- parse-decimal-int-from-slice val-slice
compare val, 0
{
break-if->
print-string 0, "invalid\n"
curr-passport-field-count <- copy 8
}
}
loop
}
loop
}
# process final passport
compare curr-passport-field-count, 7
{
break-if-!=
valid-passport-count <- increment
}
print-int32-decimal 0, valid-passport-count
print-string 0, "\n"
return 0
}
|