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

Commit297495a

Browse files
committed
Added days 2017-19 and 2017-20
1 parente56725c commit297495a

File tree

2 files changed

+199
-0
lines changed

2 files changed

+199
-0
lines changed

‎2017/19-A Series of Tubes.py‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
importos,pathfinding,string
3+
4+
test_data= {}
5+
6+
test=1
7+
test_data[test]= {"input":""".....|..........
8+
.....|..+--+....
9+
.....A..|..C....
10+
.F---|----E|--+.
11+
.....|..|..|..D.
12+
.....+B-+..+--+.""",
13+
"expected": ['ABCDEF','38'],
14+
}
15+
16+
test='real'
17+
input_file=os.path.join(os.path.dirname(__file__),'Inputs',os.path.basename(__file__).replace('.py','.txt'))
18+
test_data[test]= {"input":open(input_file,"r+").read(),
19+
"expected": ['UICRNSDOK','16064'],
20+
}
21+
22+
# -------------------------------- Control program execution -------------------------------- #
23+
24+
case_to_test='real'
25+
part_to_test=2
26+
verbose_level=1
27+
28+
# -------------------------------- Initialize some variables -------------------------------- #
29+
30+
puzzle_input=test_data[case_to_test]['input']
31+
puzzle_expected_result=test_data[case_to_test]['expected'][part_to_test-1]
32+
puzzle_actual_result='Unknown'
33+
34+
# -------------------------------- Actual code execution -------------------------------- #
35+
36+
37+
lines=puzzle_input.splitlines()
38+
iflines[len(lines)-1]=='':
39+
dellines[len(lines)-1]
40+
41+
width=max(len(line)forlineinlines)
42+
grid= {(x,y):lines[y][x].replace('.',' ')forxinrange(width)foryinrange(len(lines))}
43+
44+
direction= (0,1)
45+
x,y=lines[0].index('|'),0
46+
letters_seen=''
47+
steps_taken=1
48+
49+
cross_directions= {(0,1): [(1,0), (-1,0)], (0,-1): [(1,0), (-1,0)], (1,0): [(0,1), (0,-1)], (-1,0): [(0,1), (0,-1)]}
50+
51+
while (x,y)ingridandgrid[(x,y)]!=' ':
52+
new_cell=grid[(x,y)]
53+
54+
ifnew_cellinstring.ascii_uppercase:
55+
letters_seen+=new_cell
56+
elifnew_cell=='+':
57+
new_direction=cross_directions[direction][0]
58+
new_x,new_y=x+new_direction[0],y+new_direction[1]
59+
60+
if (new_x,new_y)ingrid:
61+
ifgrid[(new_x,new_y)]==' ':
62+
direction=cross_directions[direction][1]
63+
else:
64+
direction=new_direction
65+
else:
66+
direction=cross_directions[direction][1]
67+
68+
x,y=x+direction[0],y+direction[1]
69+
steps_taken+=1
70+
71+
ifpart_to_test==1:
72+
puzzle_actual_result=letters_seen
73+
else:
74+
puzzle_actual_result=steps_taken-1
75+
76+
77+
78+
79+
# -------------------------------- Outputs / results -------------------------------- #
80+
81+
ifverbose_level>=3:
82+
print ('Input : '+puzzle_input)
83+
print ('Expected result : '+str(puzzle_expected_result))
84+
print ('Actual result : '+str(puzzle_actual_result))
85+
86+
87+
88+

‎2017/20-Particle Swarm.py‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
importos
3+
4+
test_data= {}
5+
6+
test=1
7+
test_data[test]= {"input":"""p=<3,0,0>, v=<2,0,0>, a=<-1,0,0>
8+
p=<4,0,0>, v=<0,0,0>, a=<-2,0,0>""",
9+
"expected": ['Unknown','Unknown'],
10+
}
11+
12+
test+=1
13+
test_data[test]= {"input":"""p=<-6,0,0>, v=<3,0,0>, a=<0,0,0>
14+
p=<-4,0,0>, v=<2,0,0>, a=<0,0,0>
15+
p=<-2,0,0>, v=<1,0,0>, a=<0,0,0>
16+
p=<3,0,0>, v=<-1,0,0>, a=<0,0,0>""",
17+
"expected": ['Unknown','Unknown'],
18+
}
19+
20+
test='real'
21+
input_file=os.path.join(os.path.dirname(__file__),'Inputs',os.path.basename(__file__).replace('.py','.txt'))
22+
test_data[test]= {"input":open(input_file,"r+").read().strip(),
23+
"expected": ['Unknown','Unknown'],
24+
}
25+
26+
# -------------------------------- Control program execution -------------------------------- #
27+
28+
case_to_test='real'
29+
part_to_test=2
30+
verbose_level=1
31+
32+
# -------------------------------- Initialize some variables -------------------------------- #
33+
34+
puzzle_input=test_data[case_to_test]['input']
35+
puzzle_expected_result=test_data[case_to_test]['expected'][part_to_test-1]
36+
puzzle_actual_result='Unknown'
37+
38+
39+
# -------------------------------- Actual code execution -------------------------------- #
40+
41+
max_accel=10**6
42+
43+
ifpart_to_test==1:
44+
part_nr=0
45+
forstringinpuzzle_input.split('\n'):
46+
_,_,acceleration=string.split(' ')
47+
acceleration=list(map(int,acceleration[3:-1].split(',')))
48+
49+
ifmax_accel>sum(map(abs,acceleration)):
50+
max_accel=sum(map(abs,acceleration))
51+
closest_part=part_nr
52+
53+
part_nr+=1
54+
55+
puzzle_actual_result=closest_part
56+
57+
58+
59+
else:
60+
particles= {}
61+
collisions= []
62+
part_nr=0
63+
saved_len=0
64+
forstringinpuzzle_input.split('\n'):
65+
position,speed,acceleration=string.split(' ')
66+
position=list(map(int,position[3:-2].split(',')))
67+
speed=list(map(int,speed[3:-2].split(',')))
68+
acceleration=list(map(int,acceleration[3:-1].split(',')))
69+
70+
particles[part_nr]= [position,speed,acceleration]
71+
72+
part_nr+=1
73+
74+
foriinrange(10**4):
75+
collisions= []
76+
forpart_nrinparticles:
77+
position,speed,acceleration=particles[part_nr]
78+
speed= [speed[x]+acceleration[x]forxinrange (3)]
79+
position= [position[x]+speed[x]forxinrange (3)]
80+
particles[part_nr]= [position,speed,acceleration]
81+
collisions.append(position)
82+
83+
coordinates= [','.join(map(str,collision))forcollisionincollisions]
84+
85+
list_particles=list(particles.keys())
86+
forpart_nrinlist_particles:
87+
ifcollisions.count(particles[part_nr][0])>1:
88+
delparticles[part_nr]
89+
90+
ifi%10==0andlen(particles)==saved_len:
91+
break
92+
elifi%10==0:
93+
saved_len=len(particles)
94+
95+
print(i,len(particles))
96+
97+
puzzle_actual_result=len(particles)
98+
99+
100+
101+
102+
# -------------------------------- Outputs / results -------------------------------- #
103+
104+
ifverbose_level>=3:
105+
print ('Input : '+puzzle_input)
106+
print ('Expected result : '+str(puzzle_expected_result))
107+
print ('Actual result : '+str(puzzle_actual_result))
108+
109+
110+
111+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp