-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12-2.p
86 lines (73 loc) · 3.97 KB
/
day12-2.p
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
--- Part Two ---
Before you can give the destination to the captain, you realize that the actual action meanings were printed on the back of the instructions the whole time.
Almost all of the actions indicate how to move a waypoint which is relative to the ship's position:
Action N means to move the waypoint north by the given value.
Action S means to move the waypoint south by the given value.
Action E means to move the waypoint east by the given value.
Action W means to move the waypoint west by the given value.
Action L means to rotate the waypoint around the ship left (counter-clockwise) the given number of degrees.
Action R means to rotate the waypoint around the ship right (clockwise) the given number of degrees.
Action F means to move forward to the waypoint a number of times equal to the given value.
The waypoint starts 10 units east and 1 unit north relative to the ship. The waypoint is relative to the ship; that is, if the ship moves, the waypoint moves with it.
For example, using the same instructions as above:
F10 moves the ship to the waypoint 10 times (a total of 100 units east and 10 units north), leaving the ship at east 100, north 10. The waypoint stays 10 units east and 1 unit north of the ship.
N3 moves the waypoint 3 units north to 10 units east and 4 units north of the ship. The ship remains at east 100, north 10.
F7 moves the ship to the waypoint 7 times (a total of 70 units east and 28 units north), leaving the ship at east 170, north 38. The waypoint stays 10 units east and 4 units north of the ship.
R90 rotates the waypoint around the ship clockwise 90 degrees, moving it to 4 units east and 10 units south of the ship. The ship remains at east 170, north 38.
F11 moves the ship to the waypoint 11 times (a total of 44 units east and 110 units south), leaving the ship at east 214, south 72. The waypoint stays 4 units east and 10 units south of the ship.
After these operations, the ship's Manhattan distance from its starting position is 214 + 72 = 286.
Figure out where the navigation instructions actually lead. What is the Manhattan distance between that location and the ship's starting position?
*/
DEFINE VARIABLE cLine AS CHARACTER NO-UNDO.
DEFINE VARIABLE cAction AS CHARACTER NO-UNDO.
DEFINE VARIABLE iArgument AS INTEGER NO-UNDO.
DEFINE VARIABLE X AS INTEGER NO-UNDO.
DEFINE VARIABLE Y AS INTEGER NO-UNDO.
DEFINE VARIABLE Xwp AS INTEGER INITIAL 10 NO-UNDO.
DEFINE VARIABLE Ywp AS INTEGER INITIAL 1 NO-UNDO.
DEFINE VARIABLE iTmp AS INTEGER NO-UNDO.
ETIME(YES).
INPUT FROM C:\Work\aoc\aoc2020\day12.txt.
REPEAT:
IMPORT UNFORMATTED cLine.
cAction = SUBSTRING(cLine,1,1).
iArgument = INTEGER(SUBSTRING(cLine,2)).
IF cAction = "F" THEN ASSIGN
X = X + Xwp * iArgument
Y = Y + Ywp * iArgument.
ELSE IF cAction = "N" THEN ASSIGN
Ywp = Ywp + iArgument.
ELSE IF cAction = "S" THEN ASSIGN
Ywp = Ywp - iArgument.
ELSE IF cAction = "E" THEN ASSIGN
Xwp = Xwp + iArgument.
ELSE IF cAction = "W" THEN ASSIGN
Xwp = Xwp - iArgument.
ELSE IF (cAction = "L" AND iArgument = 90) OR (cAction = "R" AND iArgument = 270) THEN ASSIGN
iTmp = Xwp
Xwp = - Ywp
Ywp = + iTmp.
ELSE IF (cAction = "L" AND iArgument = 180) OR (cAction = "R" AND iArgument = 180) THEN ASSIGN
iTmp = Ywp
Xwp = - Xwp
Ywp = - iTmp.
ELSE IF (cAction = "L" AND iArgument = 270) OR (cAction = "R" AND iArgument = 90) THEN ASSIGN
iTmp = Xwp
Xwp = + Ywp
Ywp = - iTmp.
/* MESSAGE X Y SKIP Xwp Ywp VIEW-AS ALERT-BOX INFO BUTTONS OK. */
END.
INPUT CLOSE.
MESSAGE ETIME SKIP ABS(X) + ABS(Y)
VIEW-AS ALERT-BOX INFO BUTTONS OK.
/*
---------------------------
Renseignement (Press HELP to view stack trace)
---------------------------
6
58637
---------------------------
OK Aide
---------------------------
*/