-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.tiny
72 lines (60 loc) · 2.01 KB
/
routes.tiny
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
##################################################################
#
# Tiny script to find routes between european countries.
#
##################################################################
#
# The countries 'database'
#
countries = {}
#
# Function to declare that country a boarders on country b
#
fn borders a b {
countries[a] = b :+ countries[a] |> distinct
countries[b] = a :+ countries[b] |> distinct
}
#
# Populate the database
#
borders( 'Belgium', 'France')
borders( 'France', 'Spain')
borders( 'Germany', 'Austria')
borders( 'Germany', 'Denmark')
borders( 'Denmark', 'Sweden')
borders( 'Germany', 'Switzerland')
borders( 'Netherlands', 'Belgium')
borders( 'Netherlands', 'Germany')
borders( 'Spain', 'Portugal')
borders( 'Sweden', 'Norway')
borders( 'Sweden', 'Finland')
borders( 'Norway', 'Finland')
borders( 'Italy', 'Switzerland')
borders( 'Italy', 'Austria')
#
# Find a route between countries a and b
#
fn route a b checked=[] ->
if (countries[a] :> b) {
[a, b]
}
else {
checked = a ++ checked
shortest = []
foreach (c in countries[a].where{checked !:> it}) {
result = route(c, b, checked)
if (result) {
result = a ++ result
if (shortest.Count == 0 || shortest.Count > result.Count)
{
shortest = result
}
}
}
shortest
}
route('Austria', 'Spain').join(' -> ') |> println
route('Denmark', 'Portugal').join(' -> ') |> println
route('Portugal', 'Switzerland').join(' -> ') |> println
route('Portugal', 'Norway').join(' -> ') |> println
route('Belgium', 'Italy').join(' -> ') |> println