-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvent
executable file
·132 lines (111 loc) · 3.33 KB
/
advent
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
#!/usr/bin/env ruby
require "fileutils"
require "bundler/setup"
require "concurrent"
require "concurrent-edge"
if ARGV[0] == "get"
require "tzinfo"
AOC_TIMEZONE = TZInfo::Timezone.get("US/Eastern")
AOC_OFFSET = AOC_TIMEZONE.current_period.utc_total_offset / 60 / 60
LOCAL_OFFSET = Time.now.utc_offset / 60 / 60
# hack to download input
now = Time.now.getlocal("%+.2d:00" % AOC_OFFSET)
year = (ARGV[2] || now.year).to_i
day = (ARGV[1] || (1 if now.month != 12) || now.day + 1).to_i
out = "%4d/%02d/input" % [year, day]
offset = LOCAL_OFFSET - AOC_OFFSET
tmon = 12
tday = day.to_i
if offset < 0
tday -= 1
offset += 24
end
if tday == 0
tmon -= 1
tday = 30
end
unlock_time = Time.local(year, tmon, tday, offset)
if File.exist?(out)
day += 1
out = "%4d/%02d/input" % [year, day]
end
FileUtils.mkdir_p("%4d/%02d" % [year, day])
unless File.exist?(out)
url = "https://adventofcode.com/%d/day/%d/input" % [year, day]
puts "Waiting for #{unlock_time}" if Time.now < unlock_time
loop do
wait = (unlock_time - Time.now).to_i
break if wait < 0
if wait < 100
print "\r%d seconds to go... " % [wait]
sleep 1
elsif wait < 100 * 60
print "\r%d+ minutes to go... " % [wait / 60]
sleep 60
else
print "\r%d+ hours to go... " % [wait / 60 / 60]
sleep 60 * 60
end
puts
end
puts "> curl #{url}"
session = File.read(".session").chomp
`curl -o #{out} -b session=#{session} #{url}`
`open https://adventofcode.com/#{"%d/day/%d" % [year, day]}`
end
exit
end
require "./lib/runner"
## ARGV can be provided in any order, [year, day, lang] being distinct.
# Extract language, default all
lang = ARGV.detect { |v| v =~ /^[a-z]+/ }
ARGV.delete(lang)
ext = lang ? Runner::LANG_EXTENSION[lang] : "*"
# Extract year, default latest
year = ARGV.detect { |v| v =~ /\A2[0-9]{3}\z/ }
ARGV.delete(year)
year ||= "*" # Dir["20*"].last
# Extract day, default whole-year
day = ARGV.detect { |v| v =~ /\A\d?\d\z/ }
day = "0#{day}" if day && day.to_i < 10
day = "*" if day.nil?
# Scan all relevant files to assemble row/column headers
runners = Dir["#{year}/#{day}/*.#{ext}"].map { |script| Runner.for(script) }.compact
langs = runners.map(&:lang).sort.uniq
dates = runners.map { |runner| [runner.year, runner.day] }.sort.uniq
totals = {}
# Render table
puts "Date " + langs.map { |lang| "%10s" % lang }.join(" ")
thread_pool = Concurrent::Throttle.new(Concurrent.processor_count - 1)
results = {}
# Fork all processes
dates.each do |year, day|
runs = langs.map do |lang|
results[[year, day, lang]] = thread_pool.future do
Runner.find(year, day, lang).run
end
end
end
runs = []
# Collect values, output table
dates.each do |year, day|
runs = langs.map do |lang|
results[[year, day, lang]].value.tap do |run|
next if run.nil?
totals[lang] ||= 0.0
run.write! if ARGV.include?("--save")
totals[lang] += run.duration
end
end.compact
date = ("%4d-%02d" % [year, day]).ljust(10)
puts [date, runs.map(&:success_duration_s)].flatten.join(" ")
end
if dates.size > 1
puts
# Render Summary
puts " " + langs.map { |lang| "%10s" % lang }.join(" ")
puts "Total Time:" + langs.map { |lang| "%9.2fs" % totals[lang] }.join(" ")
else
puts
puts runs.map(&:solutions)
end