diff --git a/2015/07.py b/2015/07.py new file mode 100644 index 0000000..cd20e44 --- /dev/null +++ b/2015/07.py @@ -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"]) diff --git a/2015/08.py b/2015/08.py new file mode 100644 index 0000000..e4c452c --- /dev/null +++ b/2015/08.py @@ -0,0 +1,37 @@ +import re + +# Fill Input + +try: + with open("08.txt", 'r', encoding='utf-8') as file: + input = file.read() +except Exception as e: + print(f"An error occurred: {e}") + +# Result +res1, res2 = 0, 0 +for line in input.split('\n'): +#for line in ["\"\"", "\"abc\"", "\"aaa\\\"aaa\"", "\"\\x27\""]: + #res2 = 0 + it = iter(line) + while c:=next(it, None): + match c: + case '\"': + res1+=1 + res2+= 1 + pass + case '\\': + res1+=1 + res2+=1 + match c:= next(it): + case 'x': + next(it) + next(it) + res1+=2 + case '\"': + res2+=1 + case _: + pass + res2+=2 + print(res1, res2+len(line)) +print(res1, res2) diff --git a/2015/09.py b/2015/09.py new file mode 100644 index 0000000..a86ba39 --- /dev/null +++ b/2015/09.py @@ -0,0 +1,58 @@ +import re + +# Fill Input + +try: + with open("09.txt", 'r', encoding='utf-8') as file: + input = file.read() +except Exception as e: + print(f"An error occurred: {e}") + +# Result +def perm(X, visited = []): + if len(visited) == len(X): + return [visited] + + res = [] + + for a in X: + if a not in visited: + for i in perm(X, [*visited, a]): + res.append(i) + + return res; + +path = {} +for line in input.split('\n'): + if not (re_match := re.search(r"(\w+) to (\w+) = (\d+)", line)): + break + + f = re_match.group(1) + t = re_match.group(2) + d = int(re_match.group(3)) + + if f in path: + path[f][t] = d + else: + path[f] = {t:d} + + if t in path: + path[t][f] = d + else: + path[t] = {f:d} + + +res = [] +for p in perm(list(path)): + sum = 0; + last = None + for c in p: + if not last: + last = c + continue + + sum += path[last][c] + last = c + res.append(sum) + +print(min(res), max(res))