File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #!/usr/bin/env python
2+ # -*- coding: utf-8 -*-
3+
4+ from math import cos , sin , pi
5+
6+ split_data = True
7+ completed = True
8+ raw_data = None # Not To be touched
9+
10+ def part1 (data ):
11+ px , py = 0 , 0
12+ moveMap = {
13+ 'N' : (0 , 1 ),
14+ 'E' : (1 , 0 ),
15+ 'S' : (0 , - 1 ),
16+ 'W' : (- 1 , 0 )
17+ }
18+ directions = [* moveMap .keys ()]
19+ facing = 1
20+ for line in data :
21+ command , unit = line [:1 ], int (line [1 :])
22+ if command in moveMap :
23+ dx , dy = moveMap [command ]
24+ px += dx * unit
25+ py += dy * unit
26+ elif command == 'F' :
27+ dx , dy = moveMap [directions [facing % 4 ]]
28+ px += dx * unit
29+ py += dy * unit
30+ elif command == 'R' :
31+ facing += unit // 90
32+ elif command == 'L' :
33+ facing -= unit // 90
34+
35+ return abs (px ) + abs (py )
36+
37+ def part2 (data ):
38+ px , py = 0 , 0
39+ wx , wy = 10 , 1
40+ moveMap = {
41+ 'N' : (0 , 1 ),
42+ 'E' : (1 , 0 ),
43+ 'S' : (0 , - 1 ),
44+ 'W' : (- 1 , 0 )
45+ }
46+
47+ for line in data :
48+ command , unit = line [:1 ], int (line [1 :])
49+ if command in moveMap :
50+ dx , dy = moveMap [command ]
51+ wx += dx * unit
52+ wy += dy * unit
53+ elif command == 'F' :
54+ px += wx * unit
55+ py += wy * unit
56+ elif command == 'L' :
57+ tx = cos (pi * unit / 180 ) * wx - sin (pi * unit / 180 ) * wy
58+ ty = sin (pi * unit / 180 ) * wx + cos (pi * unit / 180 ) * wy
59+ wx , wy = round (tx ), round (ty )
60+ elif command == 'R' :
61+ tx = cos (pi * unit / 180 ) * wx + sin (pi * unit / 180 ) * wy
62+ ty = - sin (pi * unit / 180 ) * wx + cos (pi * unit / 180 ) * wy
63+ wx , wy = round (tx ), round (ty )
64+
65+ return abs (px ) + abs (py )
You can’t perform that action at this time.
0 commit comments