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

Commit37a05ef

Browse files
committed
Added day 2016-01
1 parent5bacbbb commit37a05ef

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

‎2016/01-No Time for a Taxicab.py‎

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
importos
3+
4+
test_data= {}
5+
6+
test=1
7+
test_data[test]= {"input":"""R2, L3""",
8+
"expected": ['5','Unknown'],
9+
}
10+
11+
test+=1
12+
test_data[test]= {"input":"""R2, R2, R2""",
13+
"expected": ['2','Unknown'],
14+
}
15+
16+
test+=1
17+
test_data[test]= {"input":"""R5, L5, R5, R3""",
18+
"expected": ['12','Unknown'],
19+
}
20+
21+
test+=1
22+
test_data[test]= {"input":"""R8, R4, R4, R8""",
23+
"expected": ['Unknown','4'],
24+
}
25+
26+
test='real'
27+
input_file=os.path.join(os.path.dirname(__file__),'Inputs',os.path.basename(__file__).replace('.py','.txt'))
28+
test_data[test]= {"input":open(input_file,"r+").read().strip(),
29+
"expected": ['Unknown','Unknown'],
30+
}
31+
32+
# -------------------------------- Control program execution -------------------------------- #
33+
34+
case_to_test='real'
35+
part_to_test=2
36+
verbose_level=1
37+
38+
# -------------------------------- Initialize some variables -------------------------------- #
39+
40+
puzzle_input=test_data[case_to_test]['input']
41+
puzzle_expected_result=test_data[case_to_test]['expected'][part_to_test-1]
42+
puzzle_actual_result='Unknown'
43+
44+
45+
# -------------------------------- Actual code execution -------------------------------- #
46+
47+
x,y= (0,0)
48+
locations_visited= [(x,y)]
49+
direction=0
50+
ifpart_to_test==1:
51+
forstringinpuzzle_input.split(', '):
52+
ifstring[0]=='R':
53+
direction+=90
54+
else:
55+
direction-=90
56+
direction=direction%360
57+
58+
ifdirection==0:
59+
y+=int(string[1:])
60+
elifdirection==180:
61+
y-=int(string[1:])
62+
elifdirection==90:
63+
x-=int(string[1:])
64+
elifdirection==270:
65+
x+=int(string[1:])
66+
puzzle_actual_result=abs(x)+abs(y)
67+
68+
else:
69+
forstringinpuzzle_input.split(', '):
70+
ifstring[0]=='R':
71+
direction+=90
72+
else:
73+
direction-=90
74+
direction=direction%360
75+
76+
(new_x,new_y)= (x,y)
77+
78+
ifdirection==0:
79+
new_y+=int(string[1:])
80+
elifdirection==180:
81+
new_y-=int(string[1:])
82+
elifdirection==90:
83+
new_x+=int(string[1:])
84+
elifdirection==270:
85+
new_x-=int(string[1:])
86+
87+
forx1inrange(min(x,new_x),max(x,new_x)+1):
88+
fory1inrange (min(y,new_y),max(y,new_y)+1):
89+
if (x1,y1)== (x,y):
90+
continue
91+
if (x1,y1)inlocations_visitedandpuzzle_actual_result=='Unknown':
92+
print (x1,y1)
93+
puzzle_actual_result=abs(x1)+abs(y1)
94+
break
95+
locations_visited.append((x1,y1))
96+
(x,y)= (new_x,new_y)
97+
98+
99+
# -------------------------------- Outputs / results -------------------------------- #
100+
101+
ifverbose_level>=3:
102+
print ('Input : '+puzzle_input)
103+
print ('Expected result : '+str(puzzle_expected_result))
104+
print ('Actual result : '+str(puzzle_actual_result))
105+
106+
107+
108+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp