-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6_a.rb
58 lines (49 loc) · 1023 Bytes
/
6_a.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
# typed: true
# frozen_string_literal: true
class Level
class << self
def run(input)
ecosystem = Ecosystem.new
ecosystem.lanternfish = input.split(',').map(&:to_i)
ecosystem.simulate(80)
puts "Lanternfish: #{ecosystem.lanternfish.length}"
end
end
end
class Ecosystem
extend T::Sig
sig { returns(T::Array[Integer]) }
attr_accessor :lanternfish
sig { void }
def initialize
@lanternfish = []
end
sig { params(new_fish: T::Array[Integer]).returns(T.self_type) }
def seed_lanternfish(new_fish)
@lanternfish = new_fish
self
end
sig { params(days: Integer).void }
def simulate(days)
days.times { simulate_day }
end
sig { returns(Integer) }
def total_lanternfish
lanternfish.length
end
private
sig { void }
def simulate_day
added = []
lanternfish.map! do |fish|
new_day = (fish - 1)
if new_day == -1
added << 8
6
else
new_day
end
end
@lanternfish += added
end
end