-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzle_14_a.py
50 lines (41 loc) · 1.08 KB
/
puzzle_14_a.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
import sys
matrix = []
mask = ''
total = 0
def decimalToBinary(n):
return bin(n).replace("0b", "")
def binaryToDecimal(binary):
binary1 = binary
decimal, i, n = 0, 0, 0
while(binary != 0):
dec = binary % 10
decimal = decimal + dec * pow(2, i)
binary = binary//10
i += 1
return decimal
def get_value(ind, val):
global mask
valstr = []
valstr = [char for char in (str(decimalToBinary(val)).rjust(len(mask),'0'))]
print valstr, "---"
for j in range(len(mask)):
if mask[j] != 'X':
valstr[j] = mask[j]
sm = ''.join(map(str, valstr))
print int(sm),"===="
return binaryToDecimal(int(sm))
for i in range(mx):
matrix.append(0)
for line in sys.stdin:
print line
line = line.strip()
(inst,value) = line.split(' = ')
if inst == 'mask':
mask = value
elif inst[0:3] == 'mem':
index = inst[4:]
index = int(index[:-1])
matrix[index] = get_value(index,int(value))
for i in range(mx):
total += matrix[i]
print total