-
Notifications
You must be signed in to change notification settings - Fork 0
/
p1.rb
73 lines (67 loc) · 1.62 KB
/
p1.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
# method that handle the operation on the provided register
def exec_operation(operation, register, i, j)
case operation
when 'CLEAR'
register[i] = 0
when 'SET'
register[i] = 1
when 'OR'
register[i] = or_operation(register, i, j)
when 'AND'
register[i] = and_operation(register, i, j)
end
end
# helper methods that handle the bitwise AND operation
def and_operation(s, i, j)
if s[i] == '?' && s[j] == '?'
'?'
elsif s[i] == '?' || s[j] == '?'
if s[i] == '?'
if s[j] == 1
return '?'
else
return 0
end
else
if s[i] == 1
return '?'
else
return 0
end
end
else
s[i] & s[j]
end
end
# handle bitwise OR operation
def or_operation(s, i, j)
if s[i] == 1 || s[j] == 1
1
elsif s[i] == 0 && s[j] == 0
0
else
'?'
end
end
# read the input and perform the operation
ans = []
number_of_operations = 0
register = []
ARGF.each_line do |line|
# save the current resutl and exit
if line === '0' || line === 0
ans << register.join
break
end
# handle different set of test input and store the current register to ans array
if number_of_operations.zero?
number_of_operations = line.to_i
ans << register.join
register = ['?'] * 32
else
number_of_operations -= 1
operation, i, j = line.split(" ")
exec_operation(operation, register, 31 - i.to_i, 31 - j.to_i)
end
end
ans[1..-1].each {|o| puts o}