-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfollow_that_spy.m
64 lines (51 loc) · 1.39 KB
/
follow_that_spy.m
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
#import <Foundation/Foundation.h>
/**
* @author Jonathan Bradley Whited
* @see https://www.codewars.com/kata/follow-that-spy/objc
* @rank 6 kyu
*/
NSString* findRoutes(NSArray* routes) {
NSMutableDictionary* dict = [[NSMutableDictionary alloc] init];
for(id route in routes) {
dict[route[0]] = route[1];
}
NSMutableArray* realRoutes = [[NSMutableArray alloc] init];
for(id route in routes) {
id from = route[0];
id to = nil;
[realRoutes addObject:from];
while((to = dict[from]) != nil) {
[realRoutes addObject:to];
from = to;
to = nil;
}
if([realRoutes count] != ([routes count] + 1)) {
// Try again
[realRoutes removeAllObjects];
}
else {
break;
}
}
NSString* realRoute = [realRoutes componentsJoinedByString:@", "];
[dict release];
[realRoutes release];
return realRoute;
}
/*
// @"MNL, TAG, CEB, TAC, BOR"
findRoutes(@[@[@"MNL",@"TAG"],
@[@"CEB",@"TAC"],
@[@"TAG",@"CEB"],
@[@"TAC",@"BOR"]]);
// @"Halifax, Montreal, Toronto, Chicago, Winnipeg, Seattle"
findRoutes(@[@[@"Chicago" ,@"Winnipeg"],
@[@"Halifax" ,@"Montreal"],
@[@"Montreal",@"Toronto"],
@[@"Toronto" ,@"Chicago"],
@[@"Winnipeg",@"Seattle"]]);
NSArray *args = [[NSProcessInfo processInfo] arguments];
for(id arg in args) {
NSLog(@"%@", arg);
}
*/