-
Notifications
You must be signed in to change notification settings - Fork 0
/
day7.py
59 lines (46 loc) · 1.82 KB
/
day7.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
#!/usr/bin/env python3
import numpy as np
import regex as re
from collections import defaultdict
f = open("ressources/day7.txt", "r") #Open File
lines = f.readlines() #Separate in lines
#Q1
total = 0
equations = defaultdict(list)
for line in lines:
equation = re.findall(r"(\d+)", line.replace("\n", ""))
equations[int(equation[0])] = [int(eq) for eq in equation[1:]]
def checkEquations(total, current, numbers):
if len(numbers) == 0:
if current == total:
return True
return False
if current == 0:
current = numbers.pop(0)
return checkEquations(total, current, numbers.copy())
element = numbers.pop(0)
return checkEquations(total, current + element, numbers.copy()) or checkEquations(total, current*element, numbers.copy())
for equation in equations:
isOk = checkEquations(equation, 0, equations[equation])*equation
total += isOk
print("Total of correct equations is: {}".format(total))
#Q2
total = 0
equations = defaultdict(list)
for line in lines:
equation = re.findall(r"(\d+)", line.replace("\n", ""))
equations[int(equation[0])] = [int(eq) for eq in equation[1:]]
def checkEquations2(total, current, numbers):
if len(numbers) == 0:
if current == total:
return True
return False
if current == 0:
current = numbers.pop(0)
return checkEquations2(total, current, numbers.copy())
element = numbers.pop(0)
return checkEquations2(total, current + element, numbers.copy()) or checkEquations2(total, current*element, numbers.copy()) or checkEquations2(total, int(str(current)+str(element)), numbers.copy())
for equation in equations:
isOk = checkEquations2(equation, 0, equations[equation])*equation
total += isOk
print("Total of correct equations is: {}".format(total))