-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7_b.rb
64 lines (51 loc) · 1.33 KB
/
7_b.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
57
58
59
60
61
62
63
64
# typed: true
# frozen_string_literal: true
class Level
class << self
def run(input)
crabs = CrabAlignment.new
crabs.seed(input.split(',').map(&:to_i))
puts "Fuel Consumption: #{crabs.analyze!}"
end
end
end
class CrabAlignment
extend T::Sig
sig { returns(Hash) }
attr_accessor :crabs
sig { returns(Integer) }
attr_accessor :min_x
sig { returns(Integer) }
attr_accessor :max_x
sig { returns(Integer) }
attr_accessor :alignment
sig { void }
def initialize
@crabs = Hash.new(0)
end
sig { params(new_crabs: T::Array[Integer]).returns(T.self_type) }
def seed(new_crabs)
new_crabs.each do |crab|
@crabs[crab] += 1
end
@min_x, @max_x = crabs.keys.sort.minmax
self
end
sig { returns(Integer) }
def analyze!
fuel_consumption = (min_x..max_x).each_with_object({}) do |position, results|
results[position] = fuel_consumption_at(position)
print '.'
break results if results[position - 1] && results[position] > results[position - 1]
end
print "\n"
@alignment = fuel_consumption.min_by(&:last).last
end
private
sig { params(position: Integer).returns(Integer) }
def fuel_consumption_at(position)
crabs.inject(0) do |acc, (cur_key, cur_val)|
acc + (cur_val * (0..(cur_key - position).abs).inject(:+))
end
end
end