-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday6.rb
86 lines (69 loc) · 1.72 KB
/
day6.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
require_relative "spec_helper"
class Shoal
attr_reader :counts
def initialize(states)
@counts = states.tally
@counts.default = 0
end
def wait(days = 1)
days.times do
counts.transform_keys! { _1 - 1 }
counts[8] = counts[-1]
counts[6] += counts[-1]
counts.delete(-1)
end
self
end
def count
counts.values.sum
end
private
attr_reader :counts
end
RSpec.describe "Day 6" do
let(:example) { parse_nums("3,4,3,1,2") }
let(:input) { parse_nums(File.read("day6_input.txt").lines.first) }
def parse_nums(nums)
nums.split(",").map(&:to_i)
end
describe "computation" do
specify "simple" do
s = Shoal.new([5]).wait
expect(s.count).to eql 1
end
specify "birth" do
s = Shoal.new([0]).wait
expect(s.count).to eql 2
end
specify "several days - simple" do
s = Shoal.new([5]).wait(2)
expect(s.count).to eql 1
end
specify "several days - birth" do
s = Shoal.new([0]).wait(2)
expect(s.count).to eql 2
end
specify "severy days - severy births" do
expect(Shoal.new([0]).wait.count).to eql 2
expect(Shoal.new([0]).wait(8).count).to eql 3
expect(Shoal.new([0]).wait(10).count).to eql 4
end
end
specify "part 1 - example" do
s = Shoal.new(example)
expect(s.wait(18).count).to eql 26
expect(s.wait(80 - 18).count).to eql 5934
end
specify "part 1 - answer" do
s = Shoal.new(input)
expect(s.wait(80).count).to eql 350149
end
specify "part 2 - example" do
s = Shoal.new(example)
expect(s.wait(256).count).to eql 26_984_457_539
end
specify "part 2 - answer" do
s = Shoal.new(input)
expect(s.wait(256).count).to eql 1590327954513
end
end