day 7 done
This commit is contained in:
65
2024/07/07.py
Normal file
65
2024/07/07.py
Normal file
@@ -0,0 +1,65 @@
|
||||
def binerator(value,length):
|
||||
out = "{0:b}".format(value)
|
||||
while len(out) < length:
|
||||
out = "0" + out
|
||||
return out
|
||||
|
||||
def trinerator(value,length):
|
||||
out = ""
|
||||
while value > 0:
|
||||
out = str(int(value)%3) + out
|
||||
value = value//3
|
||||
while len(out) < length:
|
||||
out = "0" + out
|
||||
return out
|
||||
|
||||
def firstTestResult(test):
|
||||
out = 0
|
||||
for i in range(0,pow(2,len(test[1])-1)):
|
||||
bin = binerator(i,len(test[1])-1)
|
||||
total = int(test[1][0])
|
||||
for j in range(0,len(bin)):
|
||||
match bin[j:j+1]:
|
||||
case "0":
|
||||
total += int(test[1][j+1])
|
||||
case "1":
|
||||
total *= int(test[1][j+1])
|
||||
case _:
|
||||
print("THIS IS AN ERROR")
|
||||
if str(total) == test[0]:
|
||||
return 1
|
||||
return 0
|
||||
|
||||
def secondTestResult(test):
|
||||
out = 0
|
||||
for i in range(0,pow(3,len(test[1])-1)):
|
||||
bin = trinerator(i,len(test[1])-1)
|
||||
total = int(test[1][0])
|
||||
for j in range(0,len(bin)):
|
||||
match bin[j:j+1]:
|
||||
case "0":
|
||||
total += int(test[1][j+1])
|
||||
case "1":
|
||||
total *= int(test[1][j+1])
|
||||
case "2":
|
||||
total = int(str(total)+test[1][j+1])
|
||||
case _:
|
||||
print("THIS IS AN ERROR")
|
||||
if str(total) == test[0]:
|
||||
return 1
|
||||
return 0
|
||||
|
||||
tests = []
|
||||
with open("input07.txt") as file:
|
||||
for line in file:
|
||||
tests.append([line.split(":")[0],line.split(":")[1][1:-1].split(" ")])
|
||||
out = 0
|
||||
for test in tests:
|
||||
if firstTestResult(test) >= 1:
|
||||
out += int(test[0])
|
||||
print("first challenge: "+str(out))
|
||||
out = 0
|
||||
for test in tests:
|
||||
if secondTestResult(test) >= 1:
|
||||
out += int(test[0])
|
||||
print("second challenge: "+str(out))
|
||||
Reference in New Issue
Block a user