-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAOC_Day14_Part2.py
89 lines (73 loc) · 2.12 KB
/
AOC_Day14_Part2.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
import copy
import math
import numpy as np
from itertools import product
File = open("Day14_Input.txt", "r")
Lines = File.readlines()
List = []
for i in range(0,36):
List.append(2**(35-i))
def getValue(Number):
value = ""
Delta = Number
for i in range(0,36):
if List[i] <= Delta:
Delta = Delta - List[i]
value += "1"
else:
value += "0"
return value
def CalculateNumber(IndexStr, Mask,memory,Number):
CountX = Mask.count("X")
print("Count X:",CountX)
Combinations= list(product([0, 1], repeat=CountX))
print(Combinations[0][0])
Numbers = []
NewMask = ""
#First calucate the mask:
for i in range(0, len(IndexStr)):
if Mask[i] == "1":
NewMask += '1'
elif Mask[i] == "0":
NewMask += IndexStr[i]
else:
NewMask += 'X'
# Create mask for each combination
for j in range(0,2**CountX):
print('j',j)
counter = 0
CombinationMask = ""
for ele in NewMask:
if ele == "X":
CombinationMask += str(Combinations[j][counter])
counter += 1
else:
CombinationMask += ele
print('NewMas ',CombinationMask)
print('IndexStr ',IndexStr)
NewIndex = 0
for i in range(0, len(IndexStr)):
if CombinationMask[i] == "1":
NewIndex += List[i]
else:
pass
print(NewIndex)
memory[NewIndex] = Number
return memory
memory = {}
for Line in Lines:
if "mask = " in Line:
Mask = Line.strip().split(" ")[2]
print(Mask)
if "mem" in Line:
Index = Line.strip().split(" ")[0].replace("mem[","").replace("]","")
Number = Line.strip().split(" ")[2]
print(Index,Number)
IndexStr = getValue(float(Index))
print(IndexStr)
memory = CalculateNumber(IndexStr, Mask,memory,Number)
print(memory)
Sum = 0
for ele in memory:
Sum += float(memory[ele])
print(Sum)