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

Commitb99833b

Browse files
committed
Added day 2020-09
1 parent9726372 commitb99833b

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

‎2020/09-Encoding Error.py

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# -------------------------------- Input data ---------------------------------------- #
2+
importos,grid,graph,dot,assembly,re,itertools
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":"""35
38+
20
39+
15
40+
25
41+
47
42+
40
43+
62
44+
55
45+
65
46+
95
47+
102
48+
117
49+
150
50+
182
51+
127
52+
219
53+
299
54+
277
55+
309
56+
576""",
57+
"expected": ["127","62"],
58+
}
59+
60+
test="real"
61+
input_file=os.path.join(
62+
os.path.dirname(__file__),
63+
"Inputs",
64+
os.path.basename(__file__).replace(".py",".txt"),
65+
)
66+
test_data[test]= {
67+
"input":open(input_file,"r+").read(),
68+
"expected": ["1504371145","183278487"],
69+
}
70+
71+
72+
# -------------------------------- Control program execution ------------------------- #
73+
74+
case_to_test="real"
75+
part_to_test=2
76+
77+
# -------------------------------- Initialize some variables ------------------------- #
78+
79+
puzzle_input=test_data[case_to_test]["input"]
80+
puzzle_expected_result=test_data[case_to_test]["expected"][part_to_test-1]
81+
puzzle_actual_result="Unknown"
82+
83+
84+
# -------------------------------- Actual code execution ----------------------------- #
85+
86+
preamble=25ifcase_to_test=="real"else5
87+
88+
numbers=ints(puzzle_input)
89+
sums= []
90+
forvalsinitertools.combinations(numbers[:preamble],2):
91+
sums.append(sum(vals))
92+
93+
i=0
94+
whileTrue:
95+
sums= []
96+
forvalsinitertools.combinations(numbers[i :i+preamble],2):
97+
sums.append(sum(vals))
98+
ifnumbers[i+preamble]notinsums:
99+
puzzle_actual_result=numbers[i+preamble]
100+
break
101+
i+=1
102+
103+
ifpart_to_test==2:
104+
invalid_number=puzzle_actual_result
105+
puzzle_actual_result="Unknown"
106+
107+
forainrange(len(numbers)):
108+
number_sum=numbers[a]
109+
ifnumber_sum<invalid_number:
110+
forbinrange(1,len(numbers)-a):
111+
number_sum+=numbers[a+b]
112+
print(a,b,number_sum,invalid_number)
113+
ifnumber_sum==invalid_number:
114+
puzzle_actual_result=min(numbers[a :a+b+1])+max(
115+
numbers[a :a+b+1]
116+
)
117+
break
118+
ifnumber_sum>invalid_number:
119+
break
120+
ifpuzzle_actual_result!="Unknown":
121+
break
122+
123+
# -------------------------------- Outputs / results --------------------------------- #
124+
125+
print("Case :",case_to_test,"- Part",part_to_test)
126+
print("Expected result : "+str(puzzle_expected_result))
127+
print("Actual result : "+str(puzzle_actual_result))
128+
# Date created: 2020-12-09 06:14:55.183250
129+
# Solve part 1: 2020-12-09 06:20:49
130+
# Solve part 2: 2020-12-09 06:29:07

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp