-
Notifications
You must be signed in to change notification settings - Fork 0
/
day5_spec.rb
190 lines (179 loc) · 6.59 KB
/
day5_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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
require './day5.rb'
require 'rspec'
# RSpec.configure do |c|
# c.filter_run_including :focus => true
# end
describe IntcodeProgram do
context 'day2 tests' do
it '1,0,0,0,99 becomes 2,0,0,0,99' do
expect(IntcodeProgram.new('1,0,0,0,99').run).to eq '2,0,0,0,99'
end
it '2,3,0,3,99 becomes 2,3,0,6,99' do
expect(IntcodeProgram.new('2,3,0,3,99').run).to eq '2,3,0,6,99'
end
it '2,4,4,5,99,0 becomes 2,4,4,5,99,9801' do
expect(IntcodeProgram.new('2,4,4,5,99,0').run).to eq '2,4,4,5,99,9801'
end
it '1,1,1,4,99,5,6,0,99 becomes 30,1,1,4,2,5,6,0,99' do
expect(IntcodeProgram.new('1,1,1,4,99,5,6,0,99').run).to eq '30,1,1,4,2,5,6,0,99'
end
it 'supports the day2 example' do
expect(IntcodeProgram.new('1,9,10,3,2,3,11,0,99,30,40,50').run).to eq '3500,9,10,70,2,3,11,0,99,30,40,50'
end
end
context 'day5 part 1 tests' do
it 'can process complex opcodes' do
expect(IntcodeProgram.new('1002,4,3,4,33').run).to eq '1002,4,3,4,99'
end
it 'supports negative values' do
expect(IntcodeProgram.new('1101,100,-1,4,0').run).to eq '1101,100,-1,4,99'
end
it 'uses opcode 3 to read input' do
# reads input into position 3, which is the first position paramter to an add, put the result in 0
expect(IntcodeProgram.new('3,3,1,0,2,0,99', input:2).run).to eq '2,3,1,2,2,0,99'
end
it 'uses opcode 4 to produce output' do
# outputs position 2 (the 99)
subject = IntcodeProgram.new('4,2,99')
subject.run
expect(subject.output).to eq [99]
end
end
context 'day5 part 2 tests' do
describe '"equal to 8" program' do
let(:program) { '3,9,8,9,10,9,4,9,99,-1,8' }
it 'should output 1 when input is 8' do
subject = IntcodeProgram.new(program, input: 8)
subject.run
expect(subject.output.first).to eq 1
end
it 'should output 0 when input is greater than 8' do
subject = IntcodeProgram.new(program, input: 354)
subject.run
expect(subject.output.first).to eq 0
end
it 'should output 0 when input is less than 8' do
subject = IntcodeProgram.new(program, input: 2)
subject.run
expect(subject.output.first).to eq 0
end
end
describe '"less than 8" program' do
let(:program) { '3,9,7,9,10,9,4,9,99,-1,8' }
it 'should output 1 when input less than 8' do
subject = IntcodeProgram.new(program, input: 4)
subject.run
expect(subject.output.first).to eq 1
end
it 'should output 0 when input is greater than 8' do
subject = IntcodeProgram.new(program, input: 354)
subject.run
expect(subject.output.first).to eq 0
end
it 'should output 0 when input is equal to 8' do
subject = IntcodeProgram.new(program, input: 8)
subject.run
expect(subject.output.first).to eq 0
end
end
describe '"equal to 8 (immediate)" program' do
let(:program) { '3,3,1108,-1,8,3,4,3,99' }
it 'should output 1 when input is 8' do
subject = IntcodeProgram.new(program, input: 8)
subject.run
expect(subject.output.first).to eq 1
end
it 'should output 0 when input is greater than 8' do
subject = IntcodeProgram.new(program, input: 354)
subject.run
expect(subject.output.first).to eq 0
end
it 'should output 0 when input is less than 8' do
subject = IntcodeProgram.new(program, input: 2)
subject.run
expect(subject.output.first).to eq 0
end
end
describe '"less than 8 (immediate)" program' do
let(:program) { '3,3,1107,-1,8,3,4,3,99' }
it 'should output 1 when input less than 8' do
subject = IntcodeProgram.new(program, input: 4)
subject.run
expect(subject.output.first).to eq 1
end
it 'should output 0 when input is greater than 8' do
subject = IntcodeProgram.new(program, input: 354)
subject.run
expect(subject.output.first).to eq 0
end
it 'should output 0 when input is equal to 8' do
subject = IntcodeProgram.new(program, input: 8)
subject.run
expect(subject.output.first).to eq 0
end
end
describe '"test non-zero" program' do
let(:program) { '3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9' }
it 'should output 0 when input is 0' do
subject = IntcodeProgram.new(program, input: 0)
subject.run
expect(subject.output.first).to eq 0
end
it 'should output 1 when input is greater than 0' do
subject = IntcodeProgram.new(program, input: 354)
subject.run
expect(subject.output.first).to eq 1
end
it 'should output 1 when input is less than 0' do
subject = IntcodeProgram.new(program, input: -2)
subject.run
expect(subject.output.first).to eq 1
end
end
describe '"test non-zero (immediate)" program' do
let(:program) { '3,3,1105,-1,9,1101,0,0,12,4,12,99,1' }
it 'should output 0 when input is 0' do
subject = IntcodeProgram.new(program, input: 0)
subject.run
expect(subject.output.first).to eq 0
end
it 'should output 1 when input is greater than 0' do
subject = IntcodeProgram.new(program, input: 100)
subject.run
expect(subject.output.first).to eq 1
end
it 'should output 1 when input is less than 0' do
subject = IntcodeProgram.new(program, input: -20)
subject.run
expect(subject.output.first).to eq 1
end
end
describe 'larger example program' do
let(:program) { '3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99' }
it 'should output 1000 when input is 8' do
subject = IntcodeProgram.new(program, input: 8)
subject.run
expect(subject.output.first).to eq 1000
end
it 'should output 1001 when input is greater than 8' do
subject = IntcodeProgram.new(program, input: 3534)
subject.run
expect(subject.output.first).to eq 1001
end
it 'should output 999 when input is less than 8' do
subject = IntcodeProgram.new(program, input: 5)
subject.run
expect(subject.output.first).to eq 999
end
end
end
describe '#parameter_mode' do
subject { IntcodeProgram.new('') }
it 'should return :immediate for a 1' do
expect(subject.parameter_mode(1003, 1)).to eq :immediate
end
it 'should return :position for a 0' do
expect(subject.parameter_mode(10003, 1)).to eq :position
end
end
end