-
Notifications
You must be signed in to change notification settings - Fork 1
/
aoc.ps
164 lines (155 loc) · 4.76 KB
/
aoc.ps
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
%!PS
% Copyright 2024 Google LLC
%
% Use of this source code is governed by an MIT-style
% license that can be found in the LICENSE file or at
% https://opensource.org/licenses/MIT.
(cob/bootstrap.ps) runlibfile
(cob/ansi.ps) cob.require
(cob/arraylist.ps) cob.require
(cob/core.ps) cob.require
(cob/file.ps) cob.require
(cob/log.ps) cob.require
(cob/performance.ps) cob.require
(cob/string.ps) cob.require
/aoc.Context 16 dict def
aoc.Context begin
/log.Enabled false def % -v on command line reenables
/ARGUMENTS where { %ifelse
/ARGUMENTS get dup empty? { pop [ (%stdin) ] } if
[ exch { %forall
dup dup (-v) eq exch (--verbose) eq or { %ifelse
/log.Enabled true def pop
} { %else
dup (-) eq { pop (%stdin) } if
} ifelse
} forall ]
/filenames exch def
} { %else
% /filenames [ (%stdin) ] def
/filenames [] def
} ifelse
/STDOUT (%stdout) (w) file def
/results alist def
/inputlines [] def
/curfilename () def
/dayname /DayNotSet def
/curpart /PartNotSet def
/timings 2 dict def
/expected 2 dict begin /part1 () def /part2 () def currentdict end def
end
/aoc.runday { % /DayN aoc.runday -
dup load type /dicttype ne { /dict cvx /typecheck signalerror } if
aoc.Context /dayname 3 -1 roll put
aoc.Context /filenames get { %forall
aoc.Context begin
dup (-) eq { pop (%stderr) } if
/curfilename exch def
curfilename readfile splitlines /inputlines exch def
2 dict begin
/part1 () def /part2 () def
curfilename (.txt) endswith? { %ifelse
curfilename 0 curfilename length 4 sub getinterval (.expected) cat
dup fileexists? { %if
readfile splitlines { %forall
(:) search { %ifelse
cvn 3 -1 roll trimspace def pop
% TODO if expected has (\n) sequence replace with (\\n)
} { pop } ifelse
} forall
} if
} if
currentdict
end /expected exch def
end %aoc.Context
/part1 aoc.runpart /part2 aoc.runpart
% [ /part1 /part2 ] { %forall
% aoc.runpart
% } forall
} forall
} bind def %/aoc.runday
/aoc.runpart { % /partN aoc.runpart result
dup aoc.Context begin
/curpart exch def
inputlines
[ (Running) dayname curpart (on) curfilename
1 indexfrommark length (lines) ] log.info
% stack: /partN input, will be just answer so subtract 1
count 1 sub /expectedcount exch def
timings curpart timing.start put
end
% stack: /partN [ input lines ]
{ % stopped
aoc.Context /dayname get load begin
exch cvx exec
} stopped { handleerror /ERROR } if
end
aoc.Context begin
timings curpart get timing.finish
count expectedcount ne {
count [ (WARNING: unexpected stack count) 3 -1 roll
(expected) expectedcount ] log.info
pstack
} if
expected curpart get curpart aoc.result
/curresult exch def
curpart tostring print (: ) print curresult /got get = STDOUT flushfile
% TODO more details on expected vs actual error
% [ curresult /symbol get curresult /outcome get ] log.info
curresult aoc.resultmessage log.info
[ curpart (took) timings curpart get /fmttime get
(and) timings curpart get /fmtmem get ] log.info
(========================================) log.info
end
} bind def %/aoc.runpart
/aoc.result { % got want /partN aoc.result dict
% TODO time measurement
8 dict begin
/part exch def
/want exch def
/got exch tostring def
got (TODO) eq { %ifelse
/outcome /todo def
/symbol (❗) def
/bgcolor ansi.BGbrightcyan def
} { %else
want empty? { %ifelse
/outcome /unknown def
/symbol (❓) def
/bgcolor ansi.BGbrightmagenta def
} { %else
got want eq { %ifelse
/outcome /success def
/symbol (✅) def
/bgcolor ansi.BGbrightgreen def
} { %else
/outcome /failure def
/symbol (❌) def
/bgcolor ansi.BGbrightred def
} ifelse
} ifelse
} ifelse
currentdict
end
} bind def %/aoc.result
/aoc.resultmessage { % aoc.result aoc.resultmessage string
begin
outcome /failure eq { %ifelse
(got ) got tostring (, want ) want 4 cat
} { %else
outcome /todo eq { %ifelse
want () eq { (implement it) } { (implement it, want ) want cat } ifelse
} { %else
(got ) got tostring cat
} ifelse
} ifelse
[ exch symbol bgcolor ansi.FGblack outcome tostring toupper ansi.Reset 4 cat
counttomark -1 roll ] ( ) join
% symbol ( ) outcome tostring toupper ( ) 5 -1 roll 5 cat
end
} bind def %/aoc.resultmessage
/aoc.addfile { % filename aoc.addfile -
aoc.Context begin
[ exch filenames aload length 1 add -1 roll ] /filenames exch def
end
} bind def %/aoc.addfile