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

Commit6fa48bf

Browse files
committed
Added days 2018-05, 2018-06, 2018-07 and update on pathfinding lib
1 parent6d33d35 commit6fa48bf

File tree

4 files changed

+814
-0
lines changed

4 files changed

+814
-0
lines changed

‎2018/05-Alchemical Reduction.py‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
importos
3+
4+
test_data= {}
5+
6+
test=1
7+
test_data[test]= {"input":"""dabAcCaCBAcCcaDA""",
8+
"expected": ['Unknown','Unknown'],
9+
}
10+
11+
test='real'
12+
input_file=os.path.join(os.path.dirname(__file__),'Inputs',os.path.basename(__file__).replace('.py','.txt'))
13+
test_data[test]= {"input":open(input_file,"r+").read().strip(),
14+
"expected": ['9390','5898'],
15+
}
16+
17+
# -------------------------------- Control program execution -------------------------------- #
18+
19+
case_to_test='real'
20+
part_to_test=2
21+
verbose_level=1
22+
23+
# -------------------------------- Initialize some variables -------------------------------- #
24+
25+
puzzle_input=test_data[case_to_test]['input']
26+
puzzle_expected_result=test_data[case_to_test]['expected'][part_to_test-1]
27+
puzzle_actual_result='Unknown'
28+
29+
30+
# -------------------------------- Actual code execution -------------------------------- #
31+
32+
ifpart_to_test==1:
33+
string=puzzle_input
34+
prev_len=0
35+
whileprev_len!=len(string):
36+
prev_len=len(string)
37+
forletterin'azertyuiopmlkjhgfdsqwxcvbn':
38+
string=string.replace(letter+letter.upper(),'')
39+
string=string.replace(letter.upper()+letter,'')
40+
41+
puzzle_actual_result=len(string)
42+
43+
44+
else:
45+
shortest_len=10**6
46+
forletterin'azertyuiopmlkjhgfdsqwxcvbn':
47+
48+
string=puzzle_input.replace(letter,'').replace(letter.upper(),'')
49+
prev_len=0
50+
whileprev_len!=len(string):
51+
prev_len=len(string)
52+
forletterin'azertyuiopmlkjhgfdsqwxcvbn':
53+
string=string.replace(letter+letter.upper(),'')
54+
string=string.replace(letter.upper()+letter,'')
55+
56+
shortest_len=min(shortest_len,len(string))
57+
58+
puzzle_actual_result=shortest_len
59+
60+
61+
# -------------------------------- Outputs / results -------------------------------- #
62+
63+
ifverbose_level>=3:
64+
print ('Input : '+puzzle_input)
65+
print ('Expected result : '+str(puzzle_expected_result))
66+
print ('Actual result : '+str(puzzle_actual_result))
67+
68+
69+
70+

‎2018/06-Chronal Coordinates.py‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
importos,numpyasnp
3+
fromcollectionsimportCounter
4+
5+
test_data= {}
6+
7+
test=1
8+
test_data[test]= {"input":"""1, 1
9+
1, 6
10+
8, 3
11+
3, 4
12+
5, 5
13+
8, 9""",
14+
"expected": ['17','16'],
15+
}
16+
17+
test='real'
18+
input_file=os.path.join(os.path.dirname(__file__),'Inputs',os.path.basename(__file__).replace('.py','.txt'))
19+
test_data[test]= {"input":open(input_file,"r+").read().strip(),
20+
"expected": ['4060','36136'],
21+
}
22+
23+
# -------------------------------- Control program execution -------------------------------- #
24+
25+
case_to_test='real'
26+
part_to_test=2
27+
verbose_level=1
28+
29+
# -------------------------------- Initialize some variables -------------------------------- #
30+
31+
puzzle_input=test_data[case_to_test]['input']
32+
puzzle_expected_result=test_data[case_to_test]['expected'][part_to_test-1]
33+
puzzle_actual_result='Unknown'
34+
35+
36+
# -------------------------------- Actual code execution -------------------------------- #
37+
38+
ifpart_to_test==1:
39+
dots= []
40+
forstringinpuzzle_input.split('\n'):
41+
ifstring=='':
42+
continue
43+
44+
x,y=map(int,string.split(', '))
45+
46+
dots.append((x,y))
47+
48+
grid= {}
49+
min_x,max_x=min(dots)[0],max(dots)[0]
50+
min_y,max_y=min(dots,key=lambdad:d[1])[1],max(dots,key=lambdad:d[1])[1]
51+
forxinrange (min_x-1,max_x+1):
52+
foryinrange (min_y-1,max_y+1):
53+
min_distance=min([abs(x-dot[0])+abs(y-dot[1])fordotindots])
54+
fori,dotinenumerate(dots):
55+
ifabs(x-dot[0])+abs(y-dot[1])==min_distance:
56+
ifgrid.get((x,y),-1)!=-1:
57+
grid[(x,y)]=-1
58+
break
59+
grid[(x,y)]=i
60+
61+
corners=set([-1])
62+
corners=corners.union(grid[x,min_y]forxinrange(min_x-1,max_x+1))
63+
corners=corners.union(grid[x,max_y]forxinrange(min_x-1,max_x+1))
64+
corners=corners.union(grid[min_x,y]foryinrange(min_y-1,max_y+1))
65+
corners=corners.union(grid[max_x,y]foryinrange(min_y-1,max_y+1))
66+
67+
puzzle_actual_result=next(x[1]forxinCounter(grid.values()).most_common()ifx[0]notincorners)
68+
69+
70+
71+
72+
else:
73+
dots= []
74+
forstringinpuzzle_input.split('\n'):
75+
ifstring=='':
76+
continue
77+
78+
x,y=map(int,string.split(', '))
79+
80+
dots.append((x,y))
81+
82+
grid= {}
83+
min_x,max_x=min(dots)[0],max(dots)[0]
84+
min_y,max_y=min(dots,key=lambdad:d[1])[1],max(dots,key=lambdad:d[1])[1]
85+
forxinrange (min_x-1,max_x+1):
86+
foryinrange (min_y-1,max_y+1):
87+
fordotindots:
88+
grid[(x,y)]=grid.get((x,y),0)+abs(x-dot[0])+abs(y-dot[1])
89+
90+
puzzle_actual_result=sum(1forxingridifgrid[x]<10000)
91+
92+
93+
94+
# -------------------------------- Outputs / results -------------------------------- #
95+
96+
ifverbose_level>=3:
97+
print ('Input : '+puzzle_input)
98+
print ('Expected result : '+str(puzzle_expected_result))
99+
print ('Actual result : '+str(puzzle_actual_result))
100+
101+
102+
103+

‎2018/07-The Sum of Its Parts.py‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
importos
3+
4+
test_data= {}
5+
6+
test=1
7+
test_data[test]= {"input":"""Step C must be finished before step A can begin.
8+
Step C must be finished before step F can begin.
9+
Step A must be finished before step B can begin.
10+
Step A must be finished before step D can begin.
11+
Step B must be finished before step E can begin.
12+
Step D must be finished before step E can begin.
13+
Step F must be finished before step E can begin.""",
14+
"expected": ['CABDFE','CABFDE'],
15+
}
16+
17+
test='real'
18+
input_file=os.path.join(os.path.dirname(__file__),'Inputs',os.path.basename(__file__).replace('.py','.txt'))
19+
test_data[test]= {"input":open(input_file,"r+").read().strip(),
20+
"expected": ['OVXCKZBDEHINPFSTJLUYRWGAMQ','955'],
21+
}
22+
23+
# -------------------------------- Control program execution -------------------------------- #
24+
25+
case_to_test=1
26+
part_to_test=1
27+
verbose_level=1
28+
29+
# -------------------------------- Initialize some variables -------------------------------- #
30+
31+
puzzle_input=test_data[case_to_test]['input']
32+
puzzle_expected_result=test_data[case_to_test]['expected'][part_to_test-1]
33+
puzzle_actual_result='Unknown'
34+
35+
36+
# -------------------------------- Actual code execution -------------------------------- #
37+
38+
deflist_remove (remove_list,element):
39+
try:
40+
remove_list.remove(element)
41+
returnremove_list
42+
exceptValueError:
43+
returnremove_list
44+
45+
ifpart_to_test==1:
46+
predecessors= {}
47+
dots= []
48+
forstringinpuzzle_input.split('\n'):
49+
_,source,_,_,_,_,_,target,*_=string.split(' ')
50+
ifnottargetinpredecessors:
51+
predecessors[target]= [source]
52+
else:
53+
predecessors[target].append(source)
54+
55+
dots.append(target)
56+
dots.append(source)
57+
58+
dots=set(dots)
59+
60+
path=''
61+
whilelen(path)!=len(dots):
62+
next_dot=sorted(xforxindotsifxnotinpredecessorsandxnotinpath)[0]
63+
path+=next_dot
64+
predecessors= {x:list_remove(predecessors[x],next_dot)forxinpredecessors}
65+
predecessors= {x:predecessors[x]forxinpredecessorsiflen(predecessors[x])}
66+
67+
puzzle_actual_result=path
68+
69+
70+
71+
72+
else:
73+
predecessors= {}
74+
dots= []
75+
forstringinpuzzle_input.split('\n'):
76+
_,source,_,_,_,_,_,target,*_=string.split(' ')
77+
ifnottargetinpredecessors:
78+
predecessors[target]= [source]
79+
else:
80+
predecessors[target].append(source)
81+
82+
dots.append(target)
83+
dots.append(source)
84+
85+
dots=set(dots)
86+
87+
88+
path=''
89+
construction= []
90+
tick=0
91+
whilelen(path)!=len(dots):
92+
tick=0iflen(construction)==0elsemin(x[2]forxinconstruction)
93+
finished= [xforxinconstructionifx[2]==tick]
94+
path+=''.join(x[0]forxinsorted(finished))
95+
predecessors= {x:list(set(predecessors[x])-set(path))forxinpredecessors}
96+
predecessors= {x:predecessors[x]forxinpredecessorsiflen(predecessors[x])}
97+
98+
construction=list(set(construction)-set(finished))
99+
in_construction= [x[0]forxinconstruction]
100+
101+
next_dots=sorted(xforxindotsifxnotinpredecessorsandxnotinpathandxnotinin_construction)
102+
workers_busy=sum(1forworkerinconstructionifworker[1]<=tickandworker[2]>=tick)
103+
104+
iflen(next_dots)andworkers_busy<5:
105+
next_dots=sorted(next_dots)[:5-workers_busy]
106+
construction+= [(next_dot,tick,tick+ord(next_dot)-ord('A')+60+1)fornext_dotinnext_dots]
107+
108+
109+
110+
puzzle_actual_result=tick
111+
112+
113+
114+
# -------------------------------- Outputs / results -------------------------------- #
115+
116+
ifverbose_level>=3:
117+
print ('Input : '+puzzle_input)
118+
print ('Expected result : '+str(puzzle_expected_result))
119+
print ('Actual result : '+str(puzzle_actual_result))
120+
121+
122+
123+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp