Content-Length: 664075 | pFad | http://github.com/Piratmac/adventofcode-python/commit/d83174eea20b11cbd8907f44627b579b097a8094

80 Added day 2020-19 · Piratmac/adventofcode-python@d83174e · GitHub
Skip to content

Commit d83174e

Browse files
committed
Added day 2020-19
1 parent 37ef572 commit d83174e

File tree

1 file changed

+235
-0
lines changed

1 file changed

+235
-0
lines changed

2020/19-Monster Messages.py

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
# -------------------------------- Input data ---------------------------------------- #
2+
import os, grid, graph, dot, assembly, re, itertools
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": """0: 4 1 5
38+
1: 2 3 | 3 2
39+
2: 4 4 | 5 5
40+
3: 4 5 | 5 4
41+
4: "a"
42+
5: "b"
43+
44+
ababbb
45+
bababa
46+
abbbab
47+
aaabbb
48+
aaaabbb""",
49+
"expected": ["2", "Unknown"],
50+
}
51+
52+
test += 1
53+
test_data[test] = {
54+
"input": """42: 9 14 | 10 1
55+
9: 14 27 | 1 26
56+
10: 23 14 | 28 1
57+
1: "a"
58+
11: 42 31
59+
5: 1 14 | 15 1
60+
19: 14 1 | 14 14
61+
12: 24 14 | 19 1
62+
16: 15 1 | 14 14
63+
31: 14 17 | 1 13
64+
6: 14 14 | 1 14
65+
2: 1 24 | 14 4
66+
0: 8 11
67+
13: 14 3 | 1 12
68+
15: 1 | 14
69+
17: 14 2 | 1 7
70+
23: 25 1 | 22 14
71+
28: 16 1
72+
4: 1 1
73+
20: 14 14 | 1 15
74+
3: 5 14 | 16 1
75+
27: 1 6 | 14 18
76+
14: "b"
77+
21: 14 1 | 1 14
78+
25: 1 1 | 1 14
79+
22: 14 14
80+
8: 42
81+
26: 14 22 | 1 20
82+
18: 15 15
83+
7: 14 5 | 1 21
84+
24: 14 1
85+
86+
abbbbbabbbaaaababbaabbbbabababbbabbbbbbabaaaa
87+
bbabbbbaabaabba
88+
babbbbaabbbbbabbbbbbaabaaabaaa
89+
aaabbbbbbaaaabaababaabababbabaaabbababababaaa
90+
bbbbbbbaaaabbbbaaabbabaaa
91+
bbbababbbbaaaaaaaabbababaaababaabab
92+
ababaaaaaabaaab
93+
ababaaaaabbbaba
94+
baabbaaaabbaaaababbaababb
95+
abbbbabbbbaaaababbbbbbaaaababb
96+
aaaaabbaabaaaaababaa
97+
aaaabbaaaabbaaa
98+
aaaabbaabbaaaaaaabbbabbbaaabbaabaaa
99+
babaaabbbaaabaababbaabababaaab
100+
aabbbbbaabbbaaaaaabbbbbababaaaaabbaaabba""",
101+
"expected": ["3", "12"],
102+
}
103+
104+
test = "real"
105+
input_file = os.path.join(
106+
os.path.dirname(__file__),
107+
"Inputs",
108+
os.path.basename(__file__).replace(".py", ".txt"),
109+
)
110+
test_data[test] = {
111+
"input": open(input_file, "r+").read(),
112+
"expected": ["198", "372"],
113+
}
114+
115+
116+
# -------------------------------- Control program execution ------------------------- #
117+
118+
case_to_test = "real"
119+
part_to_test = 2
120+
121+
# -------------------------------- Initialize some variables ------------------------- #
122+
123+
puzzle_input = test_data[case_to_test]["input"]
124+
puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1]
125+
puzzle_actual_result = "Unknown"
126+
127+
128+
# -------------------------------- Actual code execution ----------------------------- #
129+
130+
if part_to_test == 1:
131+
rules_raw, messages = puzzle_input.split("\n\n")
132+
133+
rules_with_subrules = {}
134+
regexes = {}
135+
for rule in rules_raw.split("\n"):
136+
if '"' in rule:
137+
regexes[int(rule.split(":")[0])] = rule.split('"')[1]
138+
else:
139+
nr, elements = rule.split(": ")
140+
nr = int(nr)
141+
rules_with_subrules[nr] = "( " + elements + " )"
142+
143+
while rules_with_subrules:
144+
for nr in regexes:
145+
for rule in rules_with_subrules:
146+
rules_with_subrules[rule] = rules_with_subrules[rule].replace(
147+
" " + str(nr) + " ", " ( " + regexes[nr] + " ) "
148+
)
149+
regexes.update(
150+
{
151+
rule: rules_with_subrules[rule]
152+
for rule in rules_with_subrules
153+
if len(ints(rules_with_subrules[rule])) == 0
154+
}
155+
)
156+
rules_with_subrules = {
157+
rule: rules_with_subrules[rule]
158+
for rule in rules_with_subrules
159+
if len(ints(rules_with_subrules[rule])) != 0
160+
}
161+
162+
regexes = {rule: regexes[rule].replace(" ", "") for rule in regexes}
163+
messages_OK = sum(
164+
[
165+
1
166+
for message in messages.split("\n")
167+
if re.match("^" + regexes[0] + "$", message)
168+
]
169+
)
170+
puzzle_actual_result = messages_OK
171+
172+
173+
else:
174+
rules_raw, messages = puzzle_input.split("\n\n")
175+
176+
rules_with_subrules = {}
177+
regexes = {}
178+
for rule in rules_raw.split("\n"):
179+
if "8:" in rule[:2]:
180+
rule = "8: 42 +"
181+
elif "11:" in rule[:3]:
182+
rule = "11: 42 31 "
183+
for i in range(
184+
2, 10
185+
): # Note: 10 is arbitraty - it works well with 5 as well.
186+
rule += "| " + "42 " * i + "31 " * i
187+
188+
if '"' in rule:
189+
regexes[int(rule.split(":")[0])] = rule.split('"')[1]
190+
else:
191+
nr, elements = rule.split(": ")
192+
nr = int(nr)
193+
rules_with_subrules[nr] = "( " + elements + " )"
194+
195+
while rules_with_subrules:
196+
for nr in regexes:
197+
for rule in rules_with_subrules:
198+
rules_with_subrules[rule] = rules_with_subrules[rule].replace(
199+
" " + str(nr) + " ", " ( " + regexes[nr] + " ) "
200+
)
201+
202+
regexes.update(
203+
{
204+
rule: rules_with_subrules[rule]
205+
.replace(" ", "")
206+
.replace("(a)", "a")
207+
.replace("(b)", "b")
208+
for rule in rules_with_subrules
209+
if len(ints(rules_with_subrules[rule])) == 0
210+
}
211+
)
212+
rules_with_subrules = {
213+
rule: rules_with_subrules[rule]
214+
for rule in rules_with_subrules
215+
if len(ints(rules_with_subrules[rule])) != 0
216+
}
217+
218+
regexes = {rule: regexes[rule] for rule in regexes}
219+
messages_OK = sum(
220+
[
221+
1
222+
for message in messages.split("\n")
223+
if re.match("^" + regexes[0] + "$", message)
224+
]
225+
)
226+
puzzle_actual_result = messages_OK
227+
228+
# -------------------------------- Outputs / results --------------------------------- #
229+
230+
print("Case :", case_to_test, "- Part", part_to_test)
231+
print("Expected result : " + str(puzzle_expected_result))
232+
print("Actual result : " + str(puzzle_actual_result))
233+
# Date created: 2020-12-19 06:00:00.865376
234+
# Part 1: 2020-12-19 06:24:39
235+
# Part 1: 2020-12-19 07:22:52

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/Piratmac/adventofcode-python/commit/d83174eea20b11cbd8907f44627b579b097a8094

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy