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

Commit5bd2006

Browse files
committed
Added days 2019-16 and 2019-17
1 parent2ebcb1d commit5bd2006

File tree

2 files changed

+254
-0
lines changed

2 files changed

+254
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# -------------------------------- Input data ---------------------------------------- #
2+
importos,pathfinding
3+
4+
fromcomplex_utilsimport*
5+
6+
test_data= {}
7+
8+
test=1
9+
test_data[test]= {
10+
"input":"""12345678""",
11+
"expected": ["01029498 after 4 phases","Unknown"],
12+
}
13+
14+
test+=1
15+
test_data[test]= {
16+
"input":"""80871224585914546619083218645595""",
17+
"expected": ["24176176","Unknown"],
18+
}
19+
20+
test+=1
21+
test_data[test]= {
22+
"input":"""03036732577212944063491565474664""",
23+
"expected": ["Unknown","84462026"],
24+
}
25+
26+
test="real"
27+
input_file=os.path.join(
28+
os.path.dirname(__file__),
29+
"Inputs",
30+
os.path.basename(__file__).replace(".py",".txt"),
31+
)
32+
test_data[test]= {
33+
"input":open(input_file,"r+").read().strip(),
34+
"expected": ["27229269","26857164"],
35+
}
36+
37+
# -------------------------------- Control program execution ------------------------- #
38+
39+
case_to_test="real"
40+
part_to_test=2
41+
42+
# -------------------------------- Initialize some variables ------------------------- #
43+
44+
puzzle_input=test_data[case_to_test]["input"]
45+
puzzle_expected_result=test_data[case_to_test]["expected"][part_to_test-1]
46+
puzzle_actual_result="Unknown"
47+
48+
49+
# -------------------------------- Actual code execution ----------------------------- #
50+
51+
base_pattern= [0,1,0,-1]
52+
53+
ifpart_to_test==1:
54+
signal= [int(x)forxinpuzzle_input]
55+
56+
forphaseinrange(100):
57+
output= [0]*len(signal)
58+
foriinrange(len(signal)):
59+
pattern= []
60+
forjinrange(len(base_pattern)):
61+
pattern+= [base_pattern[j]]* (i+1)
62+
63+
whilelen(pattern)<len(signal)+1:
64+
pattern+=pattern
65+
delpattern[0]
66+
67+
output[i]=sum([pattern[j]*signal[j]forjinrange(len(signal))])
68+
output[i]=abs(output[i])%10
69+
signal=output[:]
70+
71+
puzzle_actual_result="".join(map(str,output[:8]))
72+
73+
74+
else:
75+
# The signal's length is 650 * 10000 = 6500000
76+
# The first 7 digits of the input are 5978261
77+
# Therefore, the first number to be calculated will ignore the first 5978261 of the input
78+
# Also, since 5978261 < 6500000 < 5978261*2, the part with '0, -1' in the pattern is after the signal's length
79+
# Therefore it can be ignored
80+
signal= [int(x)forxinpuzzle_input]*10**4
81+
start=int(puzzle_input[:7])
82+
signal=signal[start:]
83+
84+
sum_signal=sum([int(x)forxinpuzzle_input])%10
85+
len_signal=len(puzzle_input)
86+
87+
output= [0]*len(signal)
88+
89+
forphaseinrange(100):
90+
output[-1]=signal[-1]
91+
foriinrange(1,len(signal)):
92+
output[-i-1]=output[-i]+signal[-i-1]
93+
94+
signal= [x%10forxinoutput]
95+
96+
puzzle_actual_result="".join(map(str,signal[:8]))
97+
98+
99+
# -------------------------------- Outputs / results --------------------------------- #
100+
101+
print("Expected result : "+str(puzzle_expected_result))
102+
print("Actual result : "+str(puzzle_actual_result))

‎2019/17-Set and Forget.py‎

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
# -------------------------------- Input data ---------------------------------------- #
2+
importos,pathfinding,IntCode
3+
4+
fromcomplex_utilsimport*
5+
6+
test_data= {}
7+
8+
test="real"
9+
input_file=os.path.join(
10+
os.path.dirname(__file__),
11+
"Inputs",
12+
os.path.basename(__file__).replace(".py",".txt"),
13+
)
14+
test_data[test]= {
15+
"input":open(input_file,"r+").read().strip(),
16+
"expected": ["5068","1415975"],
17+
}
18+
19+
# -------------------------------- Control program execution ------------------------- #
20+
21+
case_to_test="real"
22+
part_to_test=2
23+
verbose_level=0
24+
25+
# -------------------------------- Initialize some variables ------------------------- #
26+
27+
puzzle_input=test_data[case_to_test]["input"]
28+
puzzle_expected_result=test_data[case_to_test]["expected"][part_to_test-1]
29+
puzzle_actual_result="Unknown"
30+
31+
32+
# -------------------------------- Actual code execution ----------------------------- #
33+
34+
position=0
35+
36+
droid=IntCode.IntCode(puzzle_input)
37+
droid.run()
38+
grid= []
39+
foroutputindroid.outputs:
40+
ifchr(output)=="#":
41+
grid.append(position)
42+
elifchr(output)in ["^","v",">","<"]:
43+
droid_pos= [position,accent_to_dir[chr(output)]]
44+
45+
ifchr(output)=="\n":
46+
position=j* (position.imag-1)
47+
else:
48+
position+=1
49+
50+
51+
ifpart_to_test==1:
52+
alignment_parameter=0
53+
forxinrange(1,int(max_real(grid))):
54+
foryinrange(int(min_imag(grid)),-1):
55+
ifx+y*jingrid:
56+
ifall([x+y*j+diringridfordirindirections_straight]):
57+
alignment_parameter+=x*-y
58+
59+
puzzle_actual_result=alignment_parameter
60+
61+
62+
else:
63+
steps= []
64+
visited= []
65+
66+
# Find the path, in the long form (L,12,R,8,.....)
67+
whileTrue:
68+
position,direction=droid_pos
69+
visited.append(position)
70+
ifposition+directioningrid:
71+
steps[-1]+=1
72+
droid_pos[0]+=droid_pos[1]
73+
else:
74+
option= [
75+
(turn[0].upper(),direction*relative_directions[turn])
76+
forturninrelative_directions
77+
ifposition+direction*relative_directions[turn]ingrid
78+
ifposition+direction*relative_directions[turn]notinvisited
79+
]
80+
iflen(option)>1:
81+
print("error")
82+
raiseException(position,direction,option)
83+
84+
ifoption:
85+
option=option[0]
86+
steps+= [option[0],1]
87+
droid_pos[1]=option[1]
88+
droid_pos[0]+=droid_pos[1]
89+
else:
90+
break
91+
92+
steps=list(map(str,steps))
93+
steps_inline=",".join(steps)
94+
95+
# Shorten the path
96+
subprograms= []
97+
nb_to_letter= {0:"A",1:"B",2:"C"}
98+
99+
offset=0
100+
foriinrange(3):
101+
whilelen(subprograms)==i:
102+
nb_steps=min(20,len(steps)-offset)
103+
subprogram=steps[offset :offset+nb_steps]
104+
subprogram_inline=",".join(subprogram)
105+
106+
# The limits of 3 is arbitrary
107+
while (
108+
steps_inline.count(subprogram_inline)<3orlen(subprogram_inline)>20
109+
):
110+
# Shorten subprogram for test
111+
iflen(subprogram)<=2:
112+
break
113+
else:
114+
ifsubprogram[-1]in ("A","B","C"):
115+
delsubprogram[-1]
116+
else:
117+
delsubprogram[-2:]
118+
119+
subprogram_inline=",".join(subprogram)
120+
121+
# Found one!
122+
ifsteps_inline.count(subprogram_inline)>=3andlen(subprogram)>2:
123+
subprograms.append(subprogram_inline)
124+
steps_inline=steps_inline.replace(subprogram_inline,nb_to_letter[i])
125+
steps=steps_inline.split(",")
126+
else:
127+
ifsteps[offset]in ["A","B","C"]:
128+
offset+=1
129+
else:
130+
offset+=2
131+
offset=0
132+
133+
# Now send all that to the robot
134+
droid.instructions[0]=2
135+
inputs= (
136+
steps_inline+"\n"+"\n".join(subprograms)+"\nn\n"
137+
)# the last n is for the video
138+
forletterininputs:
139+
droid.add_input(ord(letter))
140+
droid.restart()
141+
droid.run()
142+
143+
puzzle_actual_result=droid.outputs.pop()
144+
ifverbose_level:
145+
foroutputindroid.outputs:
146+
print(chr(output),end="")
147+
148+
149+
# -------------------------------- Outputs / results --------------------------------- #
150+
151+
print("Expected result : "+str(puzzle_expected_result))
152+
print("Actual result : "+str(puzzle_actual_result))

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp