Skip to content

Commit 37ef572

Browse files
committed
Added day 2020-18
1 parent 090d75d commit 37ef572

File tree

1 file changed

+219
-0
lines changed

1 file changed

+219
-0
lines changed

2020/18-Operation Order.py

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
# -------------------------------- Input data ---------------------------------------- #
2+
import os, grid, graph, dot, assembly, re, itertools, math
3+
from collections import Counter, deque, defaultdict
4+
5+
from compass import *
6+
7+
8+
# This functions come from https://github.com/mcpower/adventofcode - Thanks!
9+
def lmap(func, *iterables):
10+
return list(map(func, *iterables))
11+
12+
13+
def ints(s: str):
14+
return lmap(int, re.findall(r"-?\d+", s)) # thanks mserrano!
15+
16+
17+
def positive_ints(s: str):
18+
return lmap(int, re.findall(r"\d+", s)) # thanks mserrano!
19+
20+
21+
def floats(s: str):
22+
return lmap(float, re.findall(r"-?\d+(?:\.\d+)?", s))
23+
24+
25+
def positive_floats(s: str):
26+
return lmap(float, re.findall(r"\d+(?:\.\d+)?", s))
27+
28+
29+
def words(s: str):
30+
return re.findall(r"[a-zA-Z]+", s)
31+
32+
33+
test_data = {}
34+
35+
test = 1
36+
test_data[test] = {
37+
"input": """1 + 2 * 3 + 4 * 5 + 6""",
38+
"expected": ["71", "Unknown"],
39+
}
40+
41+
test += 1
42+
test_data[test] = {
43+
"input": """1 + (2 * 3) + (4 * (5 + 6))""",
44+
"expected": ["51", "51"],
45+
}
46+
47+
test += 1
48+
test_data[test] = {
49+
"input": """2 * 3 + (4 * 5)""",
50+
"expected": ["Unknown", "46"],
51+
}
52+
53+
test += 1
54+
test_data[test] = {
55+
"input": """5 * 9 * (7 * 3 * 3 + 9 * 3 + (8 + 6 * 4))""",
56+
"expected": ["Unknown", "669060"],
57+
}
58+
59+
test += 1
60+
test_data[test] = {
61+
"input": """4 * 2 + 3""",
62+
"expected": ["11", "20"],
63+
}
64+
65+
test = "real"
66+
input_file = os.path.join(
67+
os.path.dirname(__file__),
68+
"Inputs",
69+
os.path.basename(__file__).replace(".py", ".txt"),
70+
)
71+
test_data[test] = {
72+
"input": open(input_file, "r+").read(),
73+
"expected": ["3647606140187", "323802071857594"],
74+
}
75+
76+
77+
# -------------------------------- Control program execution ------------------------- #
78+
79+
case_to_test = "real"
80+
part_to_test = 2
81+
82+
# -------------------------------- Initialize some variables ------------------------- #
83+
84+
puzzle_input = test_data[case_to_test]["input"]
85+
puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1]
86+
puzzle_actual_result = "Unknown"
87+
88+
89+
# -------------------------------- Actual code execution ----------------------------- #
90+
91+
92+
def make_math_p1(vals):
93+
# #print ('Calculating', ''.join(map(str, vals)))
94+
i = 0
95+
if vals[0] != "(":
96+
value = int(vals[0])
97+
i = 1
98+
else:
99+
j = 0
100+
open_par = 1
101+
closed_par = 0
102+
while open_par != closed_par:
103+
j += 1
104+
if vals[i + j] == "(":
105+
open_par += 1
106+
elif vals[i + j] == ")":
107+
closed_par += 1
108+
109+
value = make_math_p1(vals[i + 1 : i + j])
110+
i += j + 1
111+
112+
# #print (value, i, ''.join(vals[i:]))
113+
while i < len(vals) and vals[i] != "":
114+
# #print (i, vals[i], value)
115+
if vals[i] == "(":
116+
j = 0
117+
open_par = 1
118+
closed_par = 0
119+
while open_par != closed_par:
120+
j += 1
121+
if vals[i + j] == "(":
122+
open_par += 1
123+
elif vals[i + j] == ")":
124+
closed_par += 1
125+
126+
if operator == "+":
127+
value += make_math_p1(vals[i + 1 : i + j])
128+
i += j
129+
else:
130+
value *= make_math_p1(vals[i + 1 : i + j])
131+
i += j
132+
elif vals[i] in ["+", "*"]:
133+
operator = vals[i]
134+
else:
135+
if operator == "+":
136+
value += int(vals[i])
137+
else:
138+
value *= int(vals[i])
139+
140+
i += 1
141+
# #print (''.join(vals), 'returns', value)
142+
return value
143+
144+
145+
def make_math_p2(vals):
146+
# #print ('Calculating', ''.join(map(str, vals)))
147+
init = vals.copy()
148+
i = 0
149+
150+
while len(vals) != 1:
151+
if "(" not in vals:
152+
plusses = [i for i, val in enumerate(vals) if val == "+"]
153+
for plus in plusses[::-1]:
154+
vals[plus - 1] = int(vals[plus - 1]) + int(vals[plus + 1])
155+
del vals[plus : plus + 2]
156+
157+
if "*" in vals:
158+
return math.prod(map(int, vals[::2]))
159+
else:
160+
return int(vals[0])
161+
else:
162+
i = min([i for i, val in enumerate(vals) if val == "("])
163+
j = 0
164+
open_par = 1
165+
closed_par = 0
166+
while open_par != closed_par:
167+
j += 1
168+
if vals[i + j] == "(":
169+
open_par += 1
170+
elif vals[i + j] == ")":
171+
closed_par += 1
172+
173+
vals[i] = make_math_p2(vals[i + 1 : i + j])
174+
del vals[i + 1 : i + j + 1]
175+
176+
# #print (init, 'returns', vals[0])
177+
return vals[0]
178+
179+
180+
if part_to_test == 1:
181+
number = 0
182+
for string in puzzle_input.split("\n"):
183+
if string == "":
184+
continue
185+
string = string.replace("(", " ( ").replace(")", " ) ").replace(" ", " ")
186+
if string[-1] == " ":
187+
string = string[:-1]
188+
if string[0] == " ":
189+
string = string[1:]
190+
191+
number += make_math_p1(string.split(" "))
192+
# #print ('-----')
193+
puzzle_actual_result = number
194+
195+
196+
else:
197+
number = 0
198+
for string in puzzle_input.split("\n"):
199+
if string == "":
200+
continue
201+
string = string.replace("(", " ( ").replace(")", " ) ").replace(" ", " ")
202+
if string[-1] == " ":
203+
string = string[:-1]
204+
if string[0] == " ":
205+
string = string[1:]
206+
207+
number += make_math_p2(string.split(" "))
208+
# #print ('-----')
209+
puzzle_actual_result = number
210+
211+
212+
# -------------------------------- Outputs / results --------------------------------- #
213+
214+
print("Case :", case_to_test, "- Part", part_to_test)
215+
print("Expected result : " + str(puzzle_expected_result))
216+
print("Actual result : " + str(puzzle_actual_result))
217+
# Date created: 2020-12-18 06:00:00.595135
218+
# Part 1: 2020-12-18 06:33:45
219+
# Part 2: 2020-12-18 06:58:36

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy