-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path003_gc_golf.rb
executable file
·56 lines (44 loc) · 1.31 KB
/
003_gc_golf.rb
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
#!/usr/bin/env ruby
# frozen_string_literal: true
# New in Ruby 3.3.0 - we can enable YJIT without command-line options
RubyVM::YJIT.enable
Agg = Struct.new(:name, :min, :max, :sum, :count) do
def initialize(name)
super(name, Float::INFINITY, -Float::INFINITY, 0, 0)
end
def add(value)
self.min = value if value < min
self.max = value if value > max
self.sum += value
self.count += 1
end
def to_s
# chop! trailing ";" from name for output
"#{name.chop!}=#{min}/#{(sum / count).round(1)}/#{max}"
end
end
stations = Hash.new { |h, k| h[k] = Agg.new(k) }
def count_allocated
before = GC.stat(:total_allocated_objects)
begin
yield
ensure
after = GC.stat(:total_allocated_objects)
puts "allocated: #{after - before}"
end
end
File.open(ARGV[0], "rb") do |f|
f.each_line do |line|
# line = "Vancouver;13.5\n"
i = line.index(";")
# i = 9
# Slice needs a length, but we want to take everything until the end of the line.
# Rather than calculating it, the rules say the value will never be longer than "-99.9",
# so we can allow for 5 characters + "\n".
value = line.slice!(i + 1, 6)
# value = "13.5\n"
# line = "Vancouver;"
stations[line].add(value.to_f)
end
end
puts "{#{stations.keys.sort.map { |name| stations[name] }.join(", ")}}"