Add: 07->08
This commit is contained in:
122
2015/07.py
Normal file
122
2015/07.py
Normal file
@@ -0,0 +1,122 @@
|
||||
import re
|
||||
|
||||
# Fill Input
|
||||
|
||||
try:
|
||||
with open("07.txt", 'r', encoding='utf-8') as file:
|
||||
input = file.read()
|
||||
except Exception as e:
|
||||
print(f"An error occurred: {e}")
|
||||
|
||||
# Result
|
||||
def is_number(s:str):
|
||||
return ord('0')<= ord(s[0]) and ord(s[0]) <=ord('9')
|
||||
|
||||
dict = {}
|
||||
for line in input.split('\n'):
|
||||
if re_match := re.search(r"([a-z]+|[0-9]+) ([A-Z]+) ([a-z]+|[0-9]+) -> ([a-z]+)", line):
|
||||
v1 = re_match.group(1)
|
||||
op = re_match.group(2)
|
||||
v2 = re_match.group(3)
|
||||
rs = re_match.group(4)
|
||||
dict[rs] = [op,v1,v2]
|
||||
continue
|
||||
|
||||
if re_match := re.search(r"(NOT) ([a-z]+) -> ([a-z]+)", line):
|
||||
op = re_match.group(1)
|
||||
v1 = re_match.group(2)
|
||||
rs = re_match.group(3)
|
||||
dict[rs] = [op,v1,None]
|
||||
continue
|
||||
|
||||
|
||||
if re_match := re.search(r"([a-z]+|[0-9]+) -> ([a-z]+)", line):
|
||||
v1 = re_match.group(1)
|
||||
rs = re_match.group(2)
|
||||
dict[rs] = [None,v1,None]
|
||||
continue
|
||||
|
||||
# Part B
|
||||
#dict["b"]=[None,..., None]
|
||||
|
||||
to_dict = {}
|
||||
while len(dict):
|
||||
for key in dict:
|
||||
v = dict[key]
|
||||
|
||||
if not v[0] and (is_number(v[1]) or v[1] in to_dict):
|
||||
to_dict[key] = v
|
||||
|
||||
if (is_number(v[1]) or v[1] in to_dict ) and (not v[2] or is_number(v[2]) or v[2] in to_dict):
|
||||
to_dict[key] = v
|
||||
|
||||
|
||||
for key in to_dict:
|
||||
if key in dict:
|
||||
dict.pop(key)
|
||||
|
||||
|
||||
|
||||
|
||||
for key in to_dict:
|
||||
v = to_dict[key]
|
||||
|
||||
if not v[0] and is_number(v[1]):
|
||||
to_dict[key] = int(v[1])
|
||||
continue
|
||||
|
||||
if not v[0] and v[1] in to_dict:
|
||||
to_dict[key] = to_dict[v[1]]
|
||||
continue
|
||||
|
||||
if v[0] == "NOT":
|
||||
to_dict[key] = ~to_dict[v[1]]
|
||||
continue
|
||||
|
||||
if (v[1] in to_dict) and is_number(v[2]):
|
||||
match v[0]:
|
||||
case "LSHIFT":
|
||||
to_dict[key] = to_dict[v[1]] << int(v[2])
|
||||
continue
|
||||
case "RSHIFT":
|
||||
to_dict[key] = to_dict[v[1]] >> int(v[2])
|
||||
continue
|
||||
case "AND":
|
||||
to_dict[key] = to_dict[v[1]] & int(v[2])
|
||||
continue
|
||||
case "OR":
|
||||
to_dict[key] = to_dict[v[1]] | int(v[2])
|
||||
continue
|
||||
|
||||
if is_number(v[1]) and (v[2] in to_dict):
|
||||
match v[0]:
|
||||
case "LSHIFT":
|
||||
to_dict[key] = int(v[1]) << to_dict[v[2]]
|
||||
continue
|
||||
case "RSHIFT":
|
||||
to_dict[key] = int(v[1]) >> to_dict[v[2]]
|
||||
continue
|
||||
case "AND":
|
||||
to_dict[key] = int(v[1]) & to_dict[v[2]]
|
||||
continue
|
||||
case "OR":
|
||||
to_dict[key] = int(v[1]) | to_dict[v[2]]
|
||||
continue
|
||||
|
||||
if (v[1] in to_dict) and (v[2] in to_dict):
|
||||
match v[0]:
|
||||
case "LSHIFT":
|
||||
to_dict[key] = to_dict[v[1]] << to_dict[v[2]]
|
||||
continue
|
||||
case "RSHIFT":
|
||||
to_dict[key] = to_dict[v[1]] >> to_dict[v[2]]
|
||||
continue
|
||||
case "AND":
|
||||
to_dict[key] = to_dict[v[1]] & to_dict[v[2]]
|
||||
continue
|
||||
case "OR":
|
||||
to_dict[key] = to_dict[v[1]] | to_dict[v[2]]
|
||||
continue
|
||||
|
||||
|
||||
print(to_dict["a"])
|
||||
Reference in New Issue
Block a user