6
\$\begingroup\$

Inspired by a chat conversation I lurked.

I am an adventurer on the far away land of Zendikar. I truly enjoy traveling the land. Bushwhacking, weathering the roil and encountering strange and exotic flora and fauna are the highlights of any good adventure. Unfortunately, my duties as an adventurer do not always allow me to stop and smell the lotuses. Occasionally I am crunched for time and must hop on my kitesail and fly to my destination. I just glide over the land without being able to enjoy it.

The Challenge

Write a program that will let me know if I can choose not to fly. The program will take three inputs: my current location, my destination, and the maximum permitted travel time in days. Using the information below, determine if my travel time on foot is less than my maximum allowed travel time. If it is and I can travel on foot, return a truthy value. If the land travel time is as long as or longer than the maximum time, return a falsey value.

Guul Draz is two days away from Sejiri, its only neighboring territory.

Sejiri is one day away from Murasa and two days from Guul Draz.

Murasa is one day from Bala Ged, one day from Akoum, two days from Tazeem, and one day from Sejiri.

Bala Ged is two days from Akoum and one day from Murasa.

Akoum is one day from Ondu, one day from Murasa and two days from Bala Ged.

Ondu is one day from Akoum, its only neighboring territory.

Tazeem is two days from Murasa, its only neighboring territory.

Every location is connected, but not always directly. For example, going from Tazeem to anywhere requires you to travel to Murasa first. There are no real branches in the path either. Protip: Only go to Bala Ged if it is one of your endpoints. Here is a rough map if you're lost:

                        Bala Ged --
                           |       \
Guul Draz ---- Sejiri -- Murasa -- Akoum -- Ondu
                            |
                            |
                         Tazeem   

Scoring

This is code golf, so shortest program wins. Standard loopholes apply.

Test Cases

Guul Draz
Ondu
5

False

Bala Ged
Akoum
3

True

Tazeem
Sejiri
1

False
\$\endgroup\$
  • \$\begingroup\$ Wait, I actually won this one? Someone who knows an actual golfing language could win in like 20 bytes. \$\endgroup\$ – lirtosiast Oct 7 '15 at 20:18
  • \$\begingroup\$ @ThomasKwa Well it's been a while and nobody's done it. If a shorter one comes along I'll change the accepted answer. \$\endgroup\$ – Mike Bufardeci Oct 7 '15 at 20:24
8
\$\begingroup\$

TI-BASIC, 84 82 bytes

Prompt Str1,Str2,X
"GSMAOBT->Str3
{-3,-1,0,1,2,i,-2i
Ans(inString(Str3,sub(Str1,1,1))-Ans(inString(Str3,sub(Str2,1,1
X>real(Ans-iAns

I store the positions of each city on the complex plane, then calculate the Manhattan distance. Bala Ged is at 0+1i, Guul Draz is at -3+0i, and so on.

There would be a 75-ish byte solution if quotes could be in strings, but one of TI-BASIC's limitations prevents that.

\$\endgroup\$
  • \$\begingroup\$ How can "GSMAOBT"allow you to establish which locations are directly connected to which? \$\endgroup\$ – DavidC Sep 28 '15 at 17:40
  • \$\begingroup\$ @DavidCarraher The travel time happens to be equal to the Manhattan distance between some positions I assign them. So Guul Draz can be at (-3,0), Sejiri at (-1,0) and so on. \$\endgroup\$ – lirtosiast Sep 28 '15 at 17:58
  • \$\begingroup\$ Clever idea! Congrats. \$\endgroup\$ – DavidC Sep 28 '15 at 18:31
4
\$\begingroup\$

Mathematica 153

This checks whether the required time to travel (the GraphDistance in Edgeweights, i.e. days), is less than or equal to the available time to travel, also in days. This sort of approach should work with much larger graphs, corresponding to a great many locations and connections.

s=StringTake;g=Graph[UndirectedEdge@@@Partition[Characters@"GSSMMBMAMTBAAO",2], 
EdgeWeight->{2,1,1,1,2,1,1}]
f[a_,b_,n_]:=GraphDistance[g,a~s~1,b~s~1]<=n
\$\endgroup\$
1
\$\begingroup\$

Ruby 113/112

More bytes than TI Basic, but less characters.

v=0
a=(0..1).map{c=gets[0];(d="BT".index(c))&&v+=1+d;"GGSMAO".index(c.tr('BT',?M))}
p gets.to_i>(a[0]-a[1]).abs+v

v=0
a=(0..1).map{c=gets[0];(d="BT".index(c))&&v+=1+d;"OAMSSGGGBBBBBBT".index(c)%6}
p gets.to_i>(a[0]-a[1]).abs+v

Two slightly different solutions, on the same principle:

There's no point in considering the direct route between Bala Ged and Akoum, because the route via Murasa is the same length.

The main left-right string of places can be considered by taking the indices in a string and finding the difference between them.

For B and T, we need to add an extra 1 or 2. This is held in the variable v. The two versions differ in the way that B and T are made to give the same index in the main string as M. In the first, it's done by replacement B or T --> M in the input. In the second it's done by modulo (I prefer the first one, but the second one's a byte shorter.)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.