Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit37ef572

Browse files
committed
Added day 2020-18
1 parent090d75d commit37ef572

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+
importos,grid,graph,dot,assembly,re,itertools,math
3+
fromcollectionsimportCounter,deque,defaultdict
4+
5+
fromcompassimport*
6+
7+
8+
# This functions come from https://github.com/mcpower/adventofcode - Thanks!
9+
deflmap(func,*iterables):
10+
returnlist(map(func,*iterables))
11+
12+
13+
defints(s:str):
14+
returnlmap(int,re.findall(r"-?\d+",s))# thanks mserrano!
15+
16+
17+
defpositive_ints(s:str):
18+
returnlmap(int,re.findall(r"\d+",s))# thanks mserrano!
19+
20+
21+
deffloats(s:str):
22+
returnlmap(float,re.findall(r"-?\d+(?:\.\d+)?",s))
23+
24+
25+
defpositive_floats(s:str):
26+
returnlmap(float,re.findall(r"\d+(?:\.\d+)?",s))
27+
28+
29+
defwords(s:str):
30+
returnre.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+
defmake_math_p1(vals):
93+
# #print ('Calculating', ''.join(map(str, vals)))
94+
i=0
95+
ifvals[0]!="(":
96+
value=int(vals[0])
97+
i=1
98+
else:
99+
j=0
100+
open_par=1
101+
closed_par=0
102+
whileopen_par!=closed_par:
103+
j+=1
104+
ifvals[i+j]=="(":
105+
open_par+=1
106+
elifvals[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+
whilei<len(vals)andvals[i]!="":
114+
# #print (i, vals[i], value)
115+
ifvals[i]=="(":
116+
j=0
117+
open_par=1
118+
closed_par=0
119+
whileopen_par!=closed_par:
120+
j+=1
121+
ifvals[i+j]=="(":
122+
open_par+=1
123+
elifvals[i+j]==")":
124+
closed_par+=1
125+
126+
ifoperator=="+":
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+
elifvals[i]in ["+","*"]:
133+
operator=vals[i]
134+
else:
135+
ifoperator=="+":
136+
value+=int(vals[i])
137+
else:
138+
value*=int(vals[i])
139+
140+
i+=1
141+
# #print (''.join(vals), 'returns', value)
142+
returnvalue
143+
144+
145+
defmake_math_p2(vals):
146+
# #print ('Calculating', ''.join(map(str, vals)))
147+
init=vals.copy()
148+
i=0
149+
150+
whilelen(vals)!=1:
151+
if"("notinvals:
152+
plusses= [ifori,valinenumerate(vals)ifval=="+"]
153+
forplusinplusses[::-1]:
154+
vals[plus-1]=int(vals[plus-1])+int(vals[plus+1])
155+
delvals[plus :plus+2]
156+
157+
if"*"invals:
158+
returnmath.prod(map(int,vals[::2]))
159+
else:
160+
returnint(vals[0])
161+
else:
162+
i=min([ifori,valinenumerate(vals)ifval=="("])
163+
j=0
164+
open_par=1
165+
closed_par=0
166+
whileopen_par!=closed_par:
167+
j+=1
168+
ifvals[i+j]=="(":
169+
open_par+=1
170+
elifvals[i+j]==")":
171+
closed_par+=1
172+
173+
vals[i]=make_math_p2(vals[i+1 :i+j])
174+
delvals[i+1 :i+j+1]
175+
176+
# #print (init, 'returns', vals[0])
177+
returnvals[0]
178+
179+
180+
ifpart_to_test==1:
181+
number=0
182+
forstringinpuzzle_input.split("\n"):
183+
ifstring=="":
184+
continue
185+
string=string.replace("("," ( ").replace(")"," ) ").replace(" "," ")
186+
ifstring[-1]==" ":
187+
string=string[:-1]
188+
ifstring[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+
forstringinpuzzle_input.split("\n"):
199+
ifstring=="":
200+
continue
201+
string=string.replace("("," ( ").replace(")"," ) ").replace(" "," ")
202+
ifstring[-1]==" ":
203+
string=string[:-1]
204+
ifstring[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)

[8]ページ先頭

©2009-2025 Movatter.jp