-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils_test.py
143 lines (114 loc) · 5.07 KB
/
utils_test.py
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
import pytest
from utils import IntCodeComputer
@pytest.mark.parametrize(
"intcode_program, expected_output",
[
("1,9,10,3,2,3,11,0,99,30,40,50", "3500,9,10,70,2,3,11,0,99,30,40,50"),
("1,0,0,0,99", "2,0,0,0,99"),
("2,3,0,3,99", "2,3,0,6,99"),
("2,4,4,5,99,0", "2,4,4,5,99,9801"),
("1,1,1,4,99,5,6,0,99", "30,1,1,4,2,5,6,0,99"),
("1002,4,3,4,33", "1002,4,3,4,99"),
("1101,100,-1,4,0", "1101,100,-1,4,99"),
],
)
def test_process_intcode_program(intcode_program, expected_output):
cpu = IntCodeComputer(intcode_program)
cpu.process()
assert str(cpu) == expected_output
def test_simple_input_output_program():
intcode_program = "3,0,4,0,99"
cpu = IntCodeComputer(intcode_program, input_value=1)
cpu.process()
assert cpu.captured_output[0] == 1
@pytest.mark.parametrize("input_value, expected_output", [(7, 0), (8, 1), (9, 0)])
def test_input_equal_to_8__position_mode(input_value, expected_output):
intcode_program = "3,9,8,9,10,9,4,9,99,-1,8"
cpu = IntCodeComputer(intcode_program, input_value=input_value)
cpu.process()
assert cpu.captured_output[0] == expected_output
@pytest.mark.parametrize("input_value, expected_output", [(7, 1), (8, 0), (9, 0)])
def test_input_less_than_8__position_mode(input_value, expected_output):
intcode_program = "3,9,7,9,10,9,4,9,99,-1,8"
cpu = IntCodeComputer(intcode_program, input_value=input_value)
cpu.process()
assert cpu.captured_output[0] == expected_output
@pytest.mark.parametrize("input_value, expected_output", [(7, 0), (8, 1), (9, 0)])
def test_input_equal_to_8__immediate_mode(input_value, expected_output):
intcode_program = "3,3,1108,-1,8,3,4,3,99"
cpu = IntCodeComputer(intcode_program, input_value=input_value)
cpu.process()
assert cpu.captured_output[0] == expected_output
@pytest.mark.parametrize("input_value, expected_output", [(7, 1), (8, 0), (9, 0)])
def test_input_less_than_8__immediate_mode(input_value, expected_output):
intcode_program = "3,3,1107,-1,8,3,4,3,99"
cpu = IntCodeComputer(intcode_program, input_value=input_value)
cpu.process()
assert cpu.captured_output[0] == expected_output
@pytest.mark.parametrize("input_value, expected_output", [(0, 0), (1, 1), (-1, 1)])
def test_jump__position_mode(input_value, expected_output):
intcode_program = "3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9"
cpu = IntCodeComputer(intcode_program, input_value=input_value)
cpu.process()
assert cpu.captured_output[0] == expected_output
@pytest.mark.parametrize("input_value, expected_output", [(0, 0), (1, 1), (-1, 1)])
def test_jump__immediate_mode(input_value, expected_output):
intcode_program = "3,3,1105,-1,9,1101,0,0,12,4,12,99,1"
cpu = IntCodeComputer(intcode_program, input_value=input_value)
cpu.process()
assert cpu.captured_output[0] == expected_output
@pytest.mark.parametrize(
"input_value, expected_output",
[(6, 999), (7, 999), (8, 1000), (9, 1001), (9, 1001)],
)
def test_multiple_operations(input_value, expected_output):
intcode_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"
)
cpu = IntCodeComputer(intcode_program, input_value=input_value)
cpu.process()
assert cpu.captured_output[0] == expected_output
@pytest.mark.parametrize(
"intcode_program, phase, expected_output",
[
("3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0", [4, 3, 2, 1, 0], 43210),
(
"3,23,3,24,1002,24,10,24,1002,23,-1,23,101,5,23,23,1,24,23,23,4,23,99,0,0",
[0, 1, 2, 3, 4],
54321,
),
(
"3,31,3,32,1002,32,10,32,1001,31,-2,31,1007,31,0,33,1002,33,7,33,1,33,31,31,1,32,31,31,4,31,99,0,0,0",
[1, 0, 4, 3, 2],
65210,
),
],
)
def test_multiple_inputs(intcode_program, phase, expected_output):
amplifier_a = IntCodeComputer(intcode_program, input_value=0, phase=phase[0])
amplifier_a.process()
output_a = amplifier_a.captured_output[-1]
amplifier_b = IntCodeComputer(intcode_program, input_value=output_a, phase=phase[1])
amplifier_b.process()
output_b = amplifier_b.captured_output[-1]
amplifier_c = IntCodeComputer(intcode_program, input_value=output_b, phase=phase[2])
amplifier_c.process()
output_c = amplifier_c.captured_output[-1]
amplifier_d = IntCodeComputer(intcode_program, input_value=output_c, phase=phase[3])
amplifier_d.process()
output_d = amplifier_d.captured_output[-1]
amplifier_e = IntCodeComputer(intcode_program, input_value=output_d, phase=phase[4])
amplifier_e.process()
output_e = amplifier_e.captured_output[-1]
assert output_e == expected_output
def test_update_relative_base():
relative_base = 2000
instructions = "109,19"
cpu = IntCodeComputer(program=instructions, relative_base=relative_base)
try:
cpu.process()
except Exception:
assert cpu.relative_base == 2019
relative_base = cpu.relative_base