-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday6-2.p
121 lines (100 loc) · 3.15 KB
/
day6-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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
--- Part Two ---
Now, you just need to figure out how many orbital transfers you (YOU) need to take to get to Santa (SAN).
You start at the object YOU are orbiting; your destination is the object SAN is orbiting. An orbital transfer lets you move from any object to an object orbiting or orbited by that object.
For example, suppose you have the following map:
COM)B
B)C
C)D
D)E
E)F
B)G
G)H
D)I
E)J
J)K
K)L
K)YOU
I)SAN
Visually, the above map of orbits looks like this:
YOU
/
G - H J - K - L
/ /
COM - B - C - D - E - F
\
I - SAN
In this example, YOU are in orbit around K, and SAN is in orbit around I. To move from K to I, a minimum of 4 orbital transfers are required:
K to J
J to E
E to D
D to I
Afterward, the map of orbits looks like this:
G - H J - K - L
/ /
COM - B - C - D - E - F
\
I - SAN
\
YOU
What is the minimum number of orbital transfers required to move from the object YOU are orbiting to the object SAN is orbiting? (Between the objects they are orbiting - not between YOU and SAN.)
*/
ETIME(YES).
DEFINE TEMP-TABLE ttOrbit NO-UNDO
FIELD cCenter AS CHARACTER
FIELD cOrbiter AS CHARACTER
FIELD cPath AS CHARACTER
INDEX io IS UNIQUE cOrbiter
.
DEFINE BUFFER bttOrbit FOR ttOrbit.
DEFINE VARIABLE cCenter AS CHARACTER NO-UNDO.
DEFINE VARIABLE cPathSanta AS CHARACTER NO-UNDO.
DEFINE VARIABLE cPathYou AS CHARACTER NO-UNDO.
DEFINE VARIABLE i AS INTEGER NO-UNDO.
DEFINE VARIABLE iPathSanta AS INTEGER NO-UNDO.
DEFINE VARIABLE iPathYou AS INTEGER NO-UNDO.
DEFINE VARIABLE j AS INTEGER NO-UNDO.
INPUT FROM C:\User\JCCARDOT\Perso\Travail\aoc\aoc2019\day6.txt.
REPEAT:
CREATE ttOrbit.
IMPORT DELIMITER ")" ttOrbit.cCenter ttOrbit.cOrbiter.
END.
DELETE ttOrbit.
INPUT CLOSE.
FOR EACH ttOrbit WHERE ttOrbit.cOrbiter = "YOU" OR ttOrbit.cOrbiter = "SAN":
cCenter = ttOrbit.cCenter.
ttOrbit.cPath = cCenter.
DO WHILE TRUE:
FIND bttOrbit WHERE bttOrbit.cOrbiter = cCenter.
IF bttOrbit.cCenter = "COM" THEN LEAVE.
cCenter = bttOrbit.cCenter.
ttOrbit.cPath = cCenter + "," + ttOrbit.cPath.
END.
END.
FIND ttOrbit WHERE ttOrbit.cOrbiter = "YOU".
cPathYou = ttOrbit.cPath.
iPathYou = NUM-ENTRIES(cPathYou).
FIND ttOrbit WHERE ttOrbit.cOrbiter = "SAN".
cPathSanta = ttOrbit.cPath.
iPathSanta = NUM-ENTRIES(cPathSanta).
/* find first common center */
blkSearch:
DO i = iPathYou TO 1 BY -1:
cCenter = ENTRY(i, cPathYou).
DO j = iPathSanta TO 1 BY -1:
IF ENTRY(j, cPathSanta) = cCenter THEN
LEAVE blkSearch.
END.
END.
MESSAGE ETIME SKIP iPathYou - i + iPathSanta - j
VIEW-AS ALERT-BOX INFO BUTTONS OK.
/*
---------------------------
Information (Press HELP to view stack trace)
---------------------------
124
397
---------------------------
Aceptar Ayuda
---------------------------
*/