-
Notifications
You must be signed in to change notification settings - Fork 0
/
day12_spec.rb
96 lines (93 loc) · 2.96 KB
/
day12_spec.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
87
88
89
90
91
92
93
94
95
96
require './day12.rb'
require 'rspec'
# RSpec.configure do |c|
# c.filter_run_including :focus => true
# end
describe OrbitalSystem do
describe 'first example' do
let(:moon1) { Moon.new_from_string('<x=-1, y=0, z=2>') }
let(:moon2) { Moon.new_from_string('<x=2, y=-10, z=-7>') }
let(:moon3) { Moon.new_from_string('<x=4, y=-8, z=8>') }
let(:moon4) { Moon.new_from_string('<x=3, y=5, z=-1>') }
subject { OrbitalSystem.new([moon1, moon2, moon3, moon4]) }
describe 'After 1 step' do
before { subject.step }
it 'should have moon1 at [2,-1,1]' do
expect(moon1.coordinates).to eq [2,-1,1]
end
it 'should have moon1 with velocity [3,-1,-1]' do
expect(moon1.velocities).to eq [3,-1,-1]
end
it 'should have moon2 at [3,-7,-4]' do
expect(moon2.coordinates).to eq [3,-7,-4]
end
it 'should have moon2 with velocity [1,3,3]' do
expect(moon2.velocities).to eq [1,3,3]
end
it 'should have moon3 at [1,-7,5]' do
expect(moon3.coordinates).to eq [1,-7,5]
end
it 'should have moon3 with velocity [-3,1,-3]' do
expect(moon3.velocities).to eq [-3,1,-3]
end
it 'should have moon4 at [2,2,0]' do
expect(moon4.coordinates).to eq [2,2,0]
end
it 'should have moon4 with velocity [-1,-3,1]' do
expect(moon4.velocities).to eq [-1,-3,1]
end
end
it 'should have a total energy of 179 after 10 steps' do
10.times { subject.step }
expect(subject.system_energy).to eq 179
end
end
end
describe Moon do
describe '#apply_gravity' do
subject { Moon.new(0,0,0) }
context 'apply 5,5,5' do
let(:moon) { Moon.new(1,1,1) }
it 'after step, velocity should be 1,1,1' do
subject.apply_gravity(moon)
subject.step
expect(subject.velocities).to eq [1,1,1]
end
end
end
describe '#total_energy' do
subject { Moon.new(1,1,1) }
it 'should return 0 with no velocity' do
expect(subject.total_energy).to eq 0
end
it 'should return 6 after applying gravity of a moon on (5,2,-5)' do
subject.apply_gravity(Moon.new(5,2,-5))
# velocity = 1,1,-1
subject.step
# position = 2,2,0
expect(subject.total_energy).to eq 12
end
it 'should return 48 after applying gravity of a moon on (5,2,-5) twice' do
subject.apply_gravity(Moon.new(5,2,-5))
# velocity = 1,1,-1
subject.step
# position 2,2,0
subject.apply_gravity(Moon.new(5,2,-5))
# velocity = 2,1,-2
subject.step
# position 4,3,-2
expect(subject.total_energy).to eq 45
end
it 'should return 15 after stepping through applying gravity of two moons' do
subject.apply_gravity(Moon.new(5,2,-5))
# velocity = 1,1,-1
subject.step
# position 2,2,0
subject.apply_gravity(Moon.new(-1,0,1))
# velocity = 0,0,0
subject.step
# position 2,2,0
expect(subject.total_energy).to eq 0
end
end
end