-
Notifications
You must be signed in to change notification settings - Fork 0
/
day08.sed
executable file
·217 lines (198 loc) · 5.27 KB
/
day08.sed
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
#!/bin/sed -nf
# HOLD SPACE:
# a||||| b------ c d| (meaning a=5, b=-6, c=0, d=1)
G
# Swap lines (hold first) because that's how my brain works, and add the
# special MAX register
s/^\([^\n]*\)\n\([^\n]*\)$/\2\n\1/
1 s/^/MAX /
# Convert dec to inc
s/dec -/inc /
s/dec /inc -/
# Create any new registers we need
/ \([a-z]*\)[^a-z].*\n\1 inc/ ! {
s/\n\([a-z]*\) /\1 &/
}
/ \([a-z]*\)[^a-z].*if \1 / ! {
s/\n.* if \([a-z]*\) /\1 &/
}
:starttally
s/\( -\?|*\)0/\1)/g
s/\( -\?|*\)1/\1)|/g
s/\( -\?|*\)2/\1)||/g
s/\( -\?|*\)3/\1)|||/g
s/\( -\?|*\)4/\1)||||/g
s/\( -\?|*\)5/\1)|||||/g
s/\( -\?|*\)6/\1)||||||/g
s/\( -\?|*\)7/\1)|||||||/g
s/\( -\?|*\)8/\1)||||||||/g
s/\( -\?|*\)9/\1)|||||||||/g
s/)//g
s/\( -\?\)\(|*\)\([0-9]\)/\1\2\2\2\2\2\2\2\2\2\2\3/g
/[0-9]/ b starttally
s/-|/-/g
:negatives
s/-|/--/g
t negatives
/==/ {
/ \([a-z]*\)\([-|]*\) .*if \1 == \2$/ b true
b false
}
/!=/ {
/ \([a-z]*\)\([-|]*\) .*if \1 != \2$/ b false
b true
}
/<=/ {
# Register positive, compared to positive: if the latter has more
/ \([a-z]*\)\(|\+\) .*if \1 <= \2/ b true
/ \([a-z]*\)\(|\+\)|* .*if \1 <= \2$/ b false
# Register negative, compared to negative: if the former has more
/ \([a-z]*\)\(-\+\)-* .*if \1 <= \2$/ b true
/ \([a-z]*\)\(-\+\) .*if \1 <= \2/ b false
# Register positive, compared to nonpositive: false
/ \([a-z]*\)\(|\+\) .*if \1 <= -*$/ b false
# Register negative, compared to nonnegative: true
/ \([a-z]*\)\(-\+\) .*if \1 <= |*$/ b true
# Register zero, compared to nonnegative: true
/ \([a-z]*\) .*if \1 <= |*$/ b true
# Register zero, compared to negative: false
/ \([a-z]*\) .*if \1 <= -\+$/ b false
b fail
}
/</ {
# Register positive, compared to positive: if the latter has more
/ \([a-z]*\)\(|\+\) .*if \1 < \2|/ b true
/ \([a-z]*\)\(|\+\)|* .*if \1 < \2$/ b false
# Register negative, compared to negative: if the former has more
/ \([a-z]*\)\(-\+\)-\+ .*if \1 < \2$/ b true
/ \([a-z]*\)\(-\+\) .*if \1 < \2/ b false
# Register positive, compared to nonpositive: false
/ \([a-z]*\)\(|\+\) .*if \1 < -*$/ b false
# Register negative, compared to nonnegative: true
/ \([a-z]*\)\(-\+\) .*if \1 < |*$/ b true
# Register zero, compared to positive: true
/ \([a-z]*\) .*if \1 < |\+$/ b true
# Register zero, compared to nonpositive: false
/ \([a-z]*\) .*if \1 < -*$/ b false
b fail
}
/>=/ {
# Register negative, compared to negative: if the latter has more
/ \([a-z]*\)\(-\+\) .*if \1 >= \2/ b true
/ \([a-z]*\)\(-\+\)-* .*if \1 >= \2$/ b false
# Register positive, compared to positive: if the former has more
/ \([a-z]*\)\(|\+\)|* .*if \1 >= \2$/ b true
/ \([a-z]*\)\(|\+\) .*if \1 >= \2/ b false
# Register negative, compared to nonnegative: false
/ \([a-z]*\)\(-\+\) .*if \1 >= |*$/ b false
# Register positive, compared to nonpositive: true
/ \([a-z]*\)\(|\+\) .*if \1 >= -*$/ b true
# Register zero, compared to nonpositive: true
/ \([a-z]*\) .*if \1 >= -*$/ b true
# Register zero, compared to positive: false
/ \([a-z]*\) .*if \1 >= |\+$/ b false
b fail
}
/>/ {
# Register negative, compared to negative: if the latter has more
/ \([a-z]*\)\(-\+\) .*if \1 > \2-/ b true
/ \([a-z]*\)\(-\+\)-* .*if \1 > \2$/ b false
# Register positive, compared to positive: if the former has more
/ \([a-z]*\)\(|\+\)|\+ .*if \1 > \2$/ b true
/ \([a-z]*\)\(|\+\) .*if \1 > \2/ b false
# Register negative, compared to nonnegative: false
/ \([a-z]*\)\(-\+\) .*if \1 > |*$/ b false
# Register positive, compared to nonpositive: true
/ \([a-z]*\)\(|\+\) .*if \1 > -*$/ b true
# Register zero, compared to negative: true
/ \([a-z]*\) .*if \1 > -\+$/ b true
# Register zero, compared to nonnegative: false
/ \([a-z]*\) .*if \1 > |*$/ b false
b fail
}
:fail
s/$/ ***FAIL/
p
q
:true
s/ \([a-z]*\)\([-+_| ]\+.*\)\n\1 inc \([-|]*\) if .*$/ \1\3\2/
# Update max; there can be at most one register larger than it
s/MAX\(|*\)\( .*[a-z]\)\(\1|\+\) /MAX\3\2\3 /
:false
s/\n.*$//
s/+/|/g
s/_/-/g
:cleanup
s/-|\||-//
t cleanup
l
h
# If at end, print.
$ {
# Remove MAX; we'll deal with it later
s/MAX|* / /
s/[a-z]//g
/|/ {
# We have a positive number.
s/-//g
:posmax
s/ \+/ /g
s/ \(|\+\) \1|/ \1|/
s/|\(|\+\) \1 /|\1 /
s/ \(|\+\) \1 / \1 /
/| |/ b posmax
}
/|/ ! {
# We have a negative number.
:negmax
s/ \+/ /g
s/ \(-*\) \1-* / \1 /
s/ -*\(-*\) \1 / \1 /
/- -/ b negmax
s/-/|/g
s/^/-/
}
s/ //g
# Now convert to a number and print.
s/$/)/
:startprint
s/||||||||||/+/g
s/|||||||||/9/
s/||||||||/8/
s/|||||||/7/
s/||||||/6/
s/|||||/5/
s/||||/4/
s/|||/3/
s/||/2/
s/|/1/
s/\(+\|^\))/\10)/
s/\([0-9]\))/)\1/
s/+/|/g
/|/ b startprint
s/)//
p
# Grab MAX and do the same with it.
x
# And print!
s/ .*//
s/MAX//
s/$/)/
:startprint2
s/||||||||||/+/g
s/|||||||||/9/
s/||||||||/8/
s/|||||||/7/
s/||||||/6/
s/|||||/5/
s/||||/4/
s/|||/3/
s/||/2/
s/|/1/
s/\(+\|^\))/\10)/
s/\([0-9]\))/)\1/
s/+/|/g
/|/ b startprint2
s/)//
p
}