-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun
executable file
·282 lines (254 loc) · 6.46 KB
/
run
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
#!/usr/bin/env ruby
#/ Usage: ./run [--local] [-p N] [[YYYY] DD | INPUT... | SCRIPT...]
#/ Run all scripts for the specified day of Advent of Code.
#/ Default is today.
#/ --local skips downloading the input.
#/ -p sets the max number of scripts that run in parallel. By default there is
#/ no limit.
#/
#/ For example, for day 1 of 2020, you might name inputs like:
#/ 2020/examples/01.simple.txt
#/ 2020/examples/01.complex.txt
#/ 2020/input/01.txt
#/ And scripts like:
#/ 2020/01.rb
#/ 2020/py/01.py
require "fileutils"
require "net/http"
require "thread"
require "uri"
def main(year: nil, day: nil, inputs: nil, scripts: nil, local: false, p: nil)
if year.nil?
year, day = get_date(day: day, inputs: inputs, scripts: scripts)
elsif day.nil?
raise ArgumentError, "Year specified without day."
end
inputs, scripts = scan_files(inputs: inputs, scripts: scripts, local: local, year: year, day: day)
if inputs.empty? || scripts.empty?
puts "!!! Nothing to do!"
exit 0
end
c = {}
t = []
q = []
t << str_bg("======= Line counts =======")
t << Thread.new { `wc -l #{scripts.join(" ")} 2>&1` }
inputs.each do |input|
t << str_bg("======= #{input} =======")
scripts.each do |script|
w = p.nil? ? nil : Queue.new
q << w if w
t << str_bg(">>> #{script} < #{input}")
t << run_bg(script: script, input: input, c: c, w: w)
t << w if w
t << str_bg("")
end
end
p.times { q.shift&.enq :go } unless p.nil?
t.each do |x|
case x
when Queue
q.shift&.enq :go
else
puts x.value
end
end
end
def get_date(day:, inputs:, scripts:)
if day
return Time.now.year, day
end
((inputs || []) + (scripts || [])).each do |filename|
if info = derive_date(filename)
return info
end
end
now = Time.now
[now.year, now.day]
end
def derive_date(filename)
year = nil
day = nil
filename.split("/").each do |part|
if year.nil? && part =~ /\A\d{4}\z/
year = part.to_i
end
if day.nil? && part =~ /\A\d{1,2}(\D|\z)/
day = part.to_i
end
end
if year && day
[year.to_i, day.to_i]
end
end
def scan_files(inputs:, scripts:, local:, year:, day:)
return [inputs, scripts] if inputs && scripts
found_inputs = []
found_scripts = []
Dir["**/*"].each do |file|
case typeof(file, year: year, day: day)
when :input
found_inputs << file
when :script
found_scripts << file
end
end
download_input = inputs.nil? && !local && !found_inputs.any? { |input| input =~ /input/ }
if download_input
begin
token = File.read(".token").strip
puts "Downloading input..."
uri = URI("https://adventofcode.com/#{year}/day/#{day}/input")
Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
req = Net::HTTP::Get.new(uri)
req["Cookie"] = "session=#{token}"
resp = http.request(req)
if resp.code != "200"
puts "Error downloading input (HTTP #{resp.code}):"
puts resp.body
else
input_file = sprintf("%04d/input/%02d.txt", year, day)
FileUtils.mkdir_p(File.dirname(input_file))
File.write(input_file, resp.body)
found_inputs << input_file
end
end
rescue Errno::ENOENT
puts "!!!HINT!!! Create a .token file with your session token and I will"
puts "!!!HINT!!! download the input file for you!"
end
end
[inputs || found_inputs, scripts || found_scripts]
end
def str_bg(str)
Thread.new { str }
end
def run_bg(script:, input:, c:, w:)
case File.extname(script)
when ".rb"
Thread.new { w&.deq; capture_output "ruby", script, in: input }
when ".py"
Thread.new { w&.deq; capture_output "python3", script, in: input }
when ".rs"
c_th = c[script]
if c_th.nil?
c_th = c[script] = compile_bg(script) { |out_file| ["rustc", "-O", "-o", out_file, script] }
end
Thread.new do
w&.deq
res = c_th.value
if res.ok?
# warm up
system res.bin, in: "/dev/null", out: "/dev/null", err: "/dev/null"
capture_output res.bin, in: input
else
res.output
end
end
when ".go"
c_th = c[script]
if c_th.nil?
c_th = c[script] = compile_bg(script) { |out_file| ["go", "build", "-o", out_file, script] }
end
Thread.new do
w&.deq
res = c_th.value
if res.ok?
# warm up
system res.bin, in: "/dev/null", out: "/dev/null", err: "/dev/null"
capture_output res.bin, in: input
else
res.output
end
end
else
puts "Unrecognized script extension."
end
end
def capture_output(*cmd, **opts)
cmd = [{"IS_REAL_INPUT" => "Y"}] + cmd if opts[:in] =~ /input/
capture_output2(*cmd, **opts).first
end
def capture_output2(*cmd, **opts)
r, w = IO.pipe
start = Time.now
pid = spawn(*cmd, **opts, out: w, err: w)
w.close
res = r.read
elapsed = Time.now - start
r.close
_, status = Process.wait2(pid)
[
sprintf("%s> elapsed: %d ms\n", res, 1000 * elapsed),
status,
]
end
def compile_bg(script)
bin = ".output/#{script}"
cmd = yield bin
Thread.new do
system "mkdir", "-p", File.dirname(bin)
output, status = capture_output2(*cmd)
Result.new \
status: status,
output: output,
bin: bin
end
end
class Result
def initialize(status:, output:, bin:)
@status = status
@output = output
@bin = bin
end
attr_reader :output, :bin
def ok?
@status.success?
end
end
def typeof(file, year:, day:)
parts = file.split("/")
return :wrong_year unless parts.any? { |part| part.to_i == year }
return :wrong_day unless parts.any? { |part| part.to_i == day }
return :input if parts.include?("input") || parts.include?("examples")
return :script
end
options = {}
if ARGV.delete("--local")
options[:local] = true
end
if i = ARGV.index("-p")
ARGV.delete_at(i)
options[:p] = ARGV.delete_at(i).to_i
if options[:p] < 1
raise "illegal value for -p"
end
end
def usage
File.readlines(__FILE__).grep(/^#\//).each { |line| puts line[3..-1] }
end
options = {}
until ARGV.empty?
case arg = ARGV.shift
when "--local"
options[:local] = true
when "-p"
options[:p] = ARGV.shift.to_i
when /\A-/
usage
exit 1
when /\A\d{4}\z/
options[:year] = arg.to_i
when /\A\d{1,2}\z/
options[:day] = arg.to_i
when /example/, /input/
(options[:inputs] ||= []) << arg
else
(options[:scripts] ||= []) << arg
end
end
if options[:year] && options[:day].nil?
usage
exit 1
end
main(**options)