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

Commit55681fc

Browse files
committed
Added 2021-01, 2021-02, 2021-03
1 parent7675ea5 commit55681fc

File tree

9 files changed

+2467
-0
lines changed

9 files changed

+2467
-0
lines changed

‎2021/01-Sonar Sweep.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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":"""199
38+
200
39+
208
40+
210
41+
200
42+
207
43+
240
44+
269
45+
260
46+
263""",
47+
"expected": ["7","5"],
48+
}
49+
50+
test="real"
51+
input_file=os.path.join(
52+
os.path.dirname(__file__),
53+
"Inputs",
54+
os.path.basename(__file__).replace(".py",".txt"),
55+
)
56+
test_data[test]= {
57+
"input":open(input_file,"r+").read(),
58+
"expected": ["1766","1797"],
59+
}
60+
61+
62+
# -------------------------------- Control program execution ------------------------- #
63+
64+
case_to_test="real"
65+
part_to_test=2
66+
67+
# -------------------------------- Initialize some variables ------------------------- #
68+
69+
puzzle_input=test_data[case_to_test]["input"]
70+
puzzle_expected_result=test_data[case_to_test]["expected"][part_to_test-1]
71+
puzzle_actual_result="Unknown"
72+
73+
74+
# -------------------------------- Actual code execution ----------------------------- #
75+
76+
# Conver integer to 36-character binary
77+
# str_value = "{0:>036b}".format(value)
78+
# Convert binary string to number
79+
# value = int(str_value, 2)
80+
81+
82+
ifpart_to_test==1:
83+
val=ints(puzzle_input)
84+
puzzle_actual_result=sum(
85+
[1ifval[n]>val[n-1]else0forninrange(1,len(val))]
86+
)
87+
88+
89+
else:
90+
val=ints(puzzle_input)
91+
puzzle_actual_result=sum(
92+
[
93+
1ifsum(val[n-2 :n+1])>sum(val[n-3 :n])else0
94+
forninrange(3,len(val))
95+
]
96+
)
97+
# puzzle_actual_result = [(sum(val[n-2:n+1]) , sum(val[n-3:n])) for n in range(3, len(val))]
98+
99+
100+
# -------------------------------- Outputs / results --------------------------------- #
101+
102+
print("Case :",case_to_test,"- Part",part_to_test)
103+
print("Expected result : "+str(puzzle_expected_result))
104+
print("Actual result : "+str(puzzle_actual_result))
105+
# Date created: 2021-12-01 08:11:26.495595
106+
# Part 1: 2021-12-01 08:15:45
107+
# Part 2: 2021-12-01 08:20:37

‎2021/02-Dive.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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":"""forward 5
38+
down 5
39+
forward 8
40+
up 3
41+
down 8
42+
forward 2""",
43+
"expected": ["150","900"],
44+
}
45+
46+
test="real"
47+
input_file=os.path.join(
48+
os.path.dirname(__file__),
49+
"Inputs",
50+
os.path.basename(__file__).replace(".py",".txt"),
51+
)
52+
test_data[test]= {
53+
"input":open(input_file,"r+").read(),
54+
"expected": ["1962940","1813664422"],
55+
}
56+
57+
58+
# -------------------------------- Control program execution ------------------------- #
59+
60+
case_to_test="real"
61+
part_to_test=2
62+
63+
# -------------------------------- Initialize some variables ------------------------- #
64+
65+
puzzle_input=test_data[case_to_test]["input"]
66+
puzzle_expected_result=test_data[case_to_test]["expected"][part_to_test-1]
67+
puzzle_actual_result="Unknown"
68+
69+
70+
# -------------------------------- Actual code execution ----------------------------- #
71+
72+
# Conver integer to 36-character binary
73+
# str_value = "{0:>036b}".format(value)
74+
# Convert binary string to number
75+
# value = int(str_value, 2)
76+
77+
dirs= {"forward":1,"down":-1j,"up":+1j}
78+
79+
position=0
80+
aim=0
81+
ifpart_to_test==1:
82+
forstringinpuzzle_input.split("\n"):
83+
direction,delta=string.split(" ")
84+
position+=dirs[direction]*int(delta)
85+
86+
puzzle_actual_result=int(abs(position.imag)*abs(position.real))
87+
88+
89+
else:
90+
forstringinpuzzle_input.split("\n"):
91+
direction,delta=string.split(" ")
92+
ifdirection=="down"ordirection=="up":
93+
aim+=dirs[direction]*int(delta)
94+
else:
95+
position+=int(delta)
96+
position+=int(delta)*abs(aim.imag)*1j
97+
98+
print(string,aim,position)
99+
100+
puzzle_actual_result=int(abs(position.imag)*abs(position.real))
101+
102+
103+
# -------------------------------- Outputs / results --------------------------------- #
104+
105+
print("Case :",case_to_test,"- Part",part_to_test)
106+
print("Expected result : "+str(puzzle_expected_result))
107+
print("Actual result : "+str(puzzle_actual_result))
108+
# Date created: 2021-12-02 07:43:32.238803
109+
# Part 1: 2021-12-02 07:46:00
110+
# Part 2: 2021-12-02 07:50:10

‎2021/03-Binary Diagnostic.py

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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":"""00100
38+
11110
39+
10110
40+
10111
41+
10101
42+
01111
43+
00111
44+
11100
45+
10000
46+
11001
47+
00010
48+
01010""",
49+
"expected": ["198","230"],
50+
}
51+
52+
test="real"
53+
input_file=os.path.join(
54+
os.path.dirname(__file__),
55+
"Inputs",
56+
os.path.basename(__file__).replace(".py",".txt"),
57+
)
58+
test_data[test]= {
59+
"input":open(input_file,"r+").read(),
60+
"expected": ["3985686","2555739"],
61+
}
62+
63+
64+
# -------------------------------- Control program execution ------------------------- #
65+
66+
case_to_test="real"
67+
part_to_test=2
68+
69+
# -------------------------------- Initialize some variables ------------------------- #
70+
71+
puzzle_input=test_data[case_to_test]["input"]
72+
puzzle_expected_result=test_data[case_to_test]["expected"][part_to_test-1]
73+
puzzle_actual_result="Unknown"
74+
75+
76+
# -------------------------------- Actual code execution ----------------------------- #
77+
78+
# Conver integer to 36-character binary
79+
# str_value = "{0:>036b}".format(value)
80+
# Convert binary string to number
81+
# value = int(str_value, 2)
82+
83+
84+
length_binary=len(puzzle_input.split("\n")[0])
85+
86+
gamma= [0]*length_binary
87+
epsilon= [0]*length_binary
88+
counts= [0]*length_binary
89+
90+
91+
defcount_binary(source):
92+
zero= [0]*len(source[0])
93+
ones= [0]*len(source[0])
94+
forstringinsource:
95+
foriinrange(length_binary):
96+
zero[i]+=1-int(string[i])
97+
ones[i]+=int(string[i])
98+
99+
return (zero,ones)
100+
101+
102+
ifpart_to_test==1:
103+
forstringinpuzzle_input.split("\n"):
104+
foriinrange(length_binary):
105+
counts[i]+=int(string[i])
106+
107+
foriinrange(length_binary):
108+
ifcounts[i]>=len(puzzle_input.split("\n"))//2:
109+
gamma[i]=1
110+
else:
111+
epsilon[i]=1
112+
113+
gamma=int("".join(map(str,gamma)),2)
114+
epsilon=int("".join(map(str,epsilon)),2)
115+
116+
puzzle_actual_result= (gamma,epsilon,gamma*epsilon)[2]
117+
118+
119+
else:
120+
oxygen=puzzle_input.split("\n")
121+
co2=puzzle_input.split("\n")
122+
123+
foriinrange(length_binary):
124+
iflen(oxygen)!=1:
125+
zero,ones=count_binary(oxygen)
126+
127+
ifones[i]>=zero[i]:
128+
oxygen= [nforninoxygenifint(n[i])==1]
129+
else:
130+
oxygen= [nforninoxygenifint(n[i])==0]
131+
132+
iflen(co2)!=1:
133+
zero,ones=count_binary(co2)
134+
ifones[i]>=zero[i]:
135+
co2= [nforninco2ifint(n[i])==0]
136+
else:
137+
co2= [nforninco2ifint(n[i])==1]
138+
139+
iflen(oxygen)!=1orlen(co2)!=1:
140+
print("error")
141+
142+
oxygen=int("".join(map(str,oxygen)),2)
143+
co2=int("".join(map(str,co2)),2)
144+
145+
puzzle_actual_result= (oxygen,co2,oxygen*co2)[2]
146+
147+
# -------------------------------- Outputs / results --------------------------------- #
148+
149+
print("Case :",case_to_test,"- Part",part_to_test)
150+
print("Expected result : "+str(puzzle_expected_result))
151+
print("Actual result : "+str(puzzle_actual_result))
152+
# Date created: 2021-12-03 08:08:06.750713
153+
# Part 1: 2021-12-03 08:14:39
154+
# Part 2: 2021-12-03 08:25:28

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp