File tree 5 files changed +2084
-0
lines changed
5 files changed +2084
-0
lines changed Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env pypy3
2
+
3
+ import sys
4
+
5
+ NUMKNOTS = 2
6
+ DIRECTIONS = {
7
+ 'R' : 1 ,
8
+ 'L' : - 1 ,
9
+ 'U' : 1j ,
10
+ 'D' : - 1j ,
11
+ }
12
+
13
+ def update (z1 , z2 ):
14
+ dz = z1 - z2
15
+ if abs (dz ) < 2 : # ceil(sqrt(2))
16
+ return 0
17
+ return complex (* (part / abs (part ) if part else 0
18
+ for part in (dz .real , dz .imag )))
19
+
20
+ def main ():
21
+ knots = [0 + 0j ] * NUMKNOTS
22
+ visited = {knots [- 1 ]}
23
+ for line in sys .stdin :
24
+ direction , magnitude = line .split ()
25
+ for _ in range (int (magnitude )):
26
+ knots [0 ] += DIRECTIONS [direction ]
27
+ for i in range (1 , NUMKNOTS ):
28
+ knots [i ] += update (knots [i - 1 ], knots [i ])
29
+ visited .add (knots [- 1 ])
30
+
31
+ print (len (visited ))
32
+
33
+ if __name__ == '__main__' :
34
+ main ()
Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env pypy3
2
+
3
+ import sys
4
+
5
+ NUMKNOTS = 10
6
+ DIRECTIONS = {
7
+ 'R' : 1 ,
8
+ 'L' : - 1 ,
9
+ 'U' : 1j ,
10
+ 'D' : - 1j ,
11
+ }
12
+
13
+ def update (z1 , z2 ):
14
+ dz = z1 - z2
15
+ if abs (dz ) < 2 : # ceil(sqrt(2))
16
+ return 0
17
+ return complex (* (part / abs (part ) if part else 0
18
+ for part in (dz .real , dz .imag )))
19
+
20
+ def main ():
21
+ knots = [0 + 0j ] * NUMKNOTS
22
+ visited = {knots [- 1 ]}
23
+ for line in sys .stdin :
24
+ direction , magnitude = line .split ()
25
+ for _ in range (int (magnitude )):
26
+ knots [0 ] += DIRECTIONS [direction ]
27
+ for i in range (1 , NUMKNOTS ):
28
+ knots [i ] += update (knots [i - 1 ], knots [i ])
29
+ visited .add (knots [- 1 ])
30
+
31
+ print (len (visited ))
32
+
33
+ if __name__ == '__main__' :
34
+ main ()
You can’t perform that action at this time.
0 commit comments