24-1 finished
This commit is contained in:
41
24/24-01.py
41
24/24-01.py
@@ -2,14 +2,13 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from time import time
|
||||
from copy import deepcopy
|
||||
import re
|
||||
|
||||
start_time = time()
|
||||
|
||||
# file = "./input.txt"
|
||||
file = "./ex-small.txt"
|
||||
|
||||
|
||||
file = "./input.txt"
|
||||
# file = "./ex-small.txt"
|
||||
# file = "./ex-big.txt"
|
||||
|
||||
|
||||
@@ -30,7 +29,41 @@ def read_and_parse(input_file: str) -> tuple[dict[str:int], list[tuple[str, str,
|
||||
|
||||
if __name__ == "__main__":
|
||||
in_dict, in_list = read_and_parse(file)
|
||||
var_found = True
|
||||
while var_found:
|
||||
var_found = False
|
||||
temp = []
|
||||
for e in in_list:
|
||||
if e[0] in in_dict and e[2] in in_dict:
|
||||
var_found = True
|
||||
if e[1] == "AND":
|
||||
if in_dict[e[0]] == 1 and in_dict[e[2]] == 1:
|
||||
in_dict[e[3]] = 1
|
||||
else:
|
||||
in_dict[e[3]] = 0
|
||||
elif e[1] == "OR":
|
||||
if in_dict[e[0]] == 1 or in_dict[e[2]] == 1:
|
||||
in_dict[e[3]] = 1
|
||||
else:
|
||||
in_dict[e[3]] = 0
|
||||
elif e[1] == "XOR":
|
||||
if in_dict[e[0]] != in_dict[e[2]]:
|
||||
in_dict[e[3]] = 1
|
||||
else:
|
||||
in_dict[e[3]] = 0
|
||||
else:
|
||||
temp.append(e)
|
||||
in_list = deepcopy(temp)
|
||||
|
||||
print()
|
||||
print(in_dict)
|
||||
print(in_list)
|
||||
|
||||
z_keys = sorted([key for key in in_dict if key.startswith("z")], reverse=True)
|
||||
print(z_keys)
|
||||
sol = ""
|
||||
for z in z_keys:
|
||||
sol += str(in_dict[z])
|
||||
print(sol)
|
||||
print(f'Solution Part1: {int(sol, 2)}')
|
||||
print(f'Runtime: {time() - start_time:.2f} s')
|
||||
|
||||
Reference in New Issue
Block a user