3
\$\begingroup\$

Input:

A grid of varying size only containing numbers in the range 00-99, the following four characters: ^; >; v; <, and spaces to fill (and new-lines of course).

Output:

The result of the sum (integer) when you follow the path.

How does this work?

You start at the arrow with the highest number, and follow the path the arrows point to.
For each of the four arrows we'll do a different arithmetic calculation:

  • > = Addition (+);
  • < = Subtraction (−);
  • ^ = Multiplication (* or × or ·);
  • v = Real / Calculator Division (/ or ÷);

(NOTE: The symbols between parenthesis are just added as clarification. If your programming language uses a completely different symbol for the arithmetic operation than the examples, then that is of course completely acceptable.)

Challenge rules:

  • You can choose the input (to some extent). A string-array would be most logical I think, but a symbol/newline-separated single string is also fine. (Full functions as input that are simply executed in the actual code snippet aren't allowed!)
  • Trailing spaces for the input are optional.
  • Numbers 0-9 will have a leading 0 so all numbers plus arrow are of an equal three-character length.
    The number & arrow will always have the following format: DDA (D being a digit; A being one of the four arrows).
  • You use the current value with the previous arithmetic operation (the example below explains this better).
  • The final arithmetic operation can be ignored because of the rule above.
  • It is possible to have DDA's in the input-grid that aren't being used in the path to the resulting sum.
  • There won't be any test-cases for arrows that form a loop.
  • There won't be any test-cases for dividing by 0.
  • There won't be any test-cases where dividing creates a decimal sum-result. You only have to worry about integers.
  • EDIT: Since there weren't any answer yet, I decided to change the grids slightly to perhaps make it easier. I know this is usually not recommended to do with challenges, but since no answers were given yet, I think this will make it slightly easier, giving more (/any) answers: The spaces between de DDA's are random. There could even be no spaces in between at all. All DDA's and spaces are grouped per three. Also, it is possible to have no spaces between DDA's at all.

Example:

Input:

94>      66v
   05v   08<
         43>
   03>   00^

Path it follows:

Path it follows   |   Current sum value
________________________________________
94>               |   94  (highest value)
66v               |   160 (+)
08<               |   20  (/)
05v               |   15  (-)
03>               |   5   (/)
00^               |   5   (+)
43>               |   215 (*)
                          '>' after 43 is ignored because it's the last number

Output: 215

General Rules:

  • This is , so shortest answer in bytes wins.
    Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
  • Standard rules apply for your answer, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters, full programs. Your call.
  • Default Loopholes are forbidden.
  • If possible, please add a link with a test for your code.
  • Also, please add an explanation if necessary.

Test cases:

94>      66v           ->     215
   05v   08<
         43>
   03>   00^


77>   80v55^02<        ->     -19
      20>   14^


22>11>15>80>14v        ->     -16
18^17>      10v
00^   90v43^02v
07^11<15<22v01<


99>98>97>96>95>94v     ->     102
93>92>   91v   03<
\$\endgroup\$
4
  • 1
    \$\begingroup\$ I keep finding -12 for the third test case... \$\endgroup\$ – Paul Picard Sep 5 '16 at 14:45
  • \$\begingroup\$ @PaulPicard Ah, I'm so sorry, there was a mistake in the grid.. 07< has been changed to 07^ to fix this. \$\endgroup\$ – Kevin Cruijssen Sep 5 '16 at 14:47
  • \$\begingroup\$ Could somebody please tell me what I'm misunderstanding with the second one? As I see it, it is: 77+80/20+14*2-55=-11.3 without order of operations. \$\endgroup\$ – Daniel Sep 6 '16 at 3:36
  • \$\begingroup\$ @Dopapp You start at the highest value, so it's 80/20+14*2-55=-19 \$\endgroup\$ – Kevin Cruijssen Sep 6 '16 at 6:15
1
\$\begingroup\$

Python 2, 409 390 377 bytes

def f(p):
 p=map(lambda s:map(lambda d:d==3*(' ',)and(0,-1)or(int(d[0]+d[1]),'><^v'.index(d[2])),zip(s[::3],s[1::3],s[2::3])),p);x,y=max([(x,y)for x in range(len(p[0]))for y in range(len(p))],key=lambda(x,y):p[y][x][0]);r=o=0
 try:
  while 1:
   1/(y*x>=0);d=p[y][x]
   if-1<d[1]:r=eval('r%sd[0]'%'+-*/'[o]);o=d[1];a=(-(o==1),1)[o<1];b=(-(o==2),1)[o>2]
   x+=a;y+=b
 except:print r

The second and third indent levels are a raw tab and a raw tab plus a space, respectively. Markdown mangles the raw tabs, so they've been replaced by two spaces.

Ungolfed:

def follow(path):
  # Split path's strs into DDAs ((0, -1) for blank, else (int, int))
  path = map(
    lambda s: map(
      lambda dda: (0, -1) if dda == (' ', ' ', ' ') else (int(dda[0] + dda[1]), '><^v'.index(dda[2])),
      zip(s[::3], s[1::3], s[2::3])
    ),
    path
  )

  # Find highest DDA
  x, y = max([(x, y) for x in range(len(path[0])) for y in range(len(path))], key=lambda (x, y): path[y][x][0])

  result = 0
  op = 0  # addition
  try:
    # Keep following the path until we get an IndexError
    while True:
      if y < 0 or x < 0: raise IndexError

      dda = path[y][x]
      if dda[1] != -1:
        # It's not empty - do the operation & change direction
        const = dda[0]

        result = eval('result %s const' % '+-*/'[op])

        op = dda[1]

        dx = 1 if op == 0 else -1 if op == 1 else 0
        dy = 1 if op == 3 else -1 if op == 2 else 0

      # Move to the next thing
      x += dx
      y += dy
  except IndexError:
    # We've exited the path - return
    print result

Ideone it! (all test cases)

\$\endgroup\$
0
\$\begingroup\$

Python 2, 449 bytes

i=input();p,d,a,o,l=[-1,0],">",0,"+",0
for y in range(len(i)):
 for x in range(len(i[0])):
  if i[y][x]in"><^v":
   v=int(i[y][x-2]+i[y][x-1])
   if v>l:l=v;p=[x-2,y]
while 1:
 if d==">":p[0]+=1
 if d=="<":p[0]-=1
 if d=="^":p[1]-=1
 if d=="v":p[1]+=1
 try:
  c=i[p[1]][p[0]]
  if c in"><^v":
   d=c
   if p[0]<0 or p[1]<0:1/0
   a=eval(str(a)+o+str(int(i[p[1]][p[0]-2]+i[p[1]][p[0]-1])));o={">":"+","<":"-","^":"*","v":"/"}[c]
 except:print a;break

This takes an array of strings as input

Ungolfed:

pointer = [-1,0]
dir = ">"
output=0
operation="+"

largest = 0
for y in range(len(input)):
    for x in range(len(input[0])):
        if input[y][x] in "><^v":
            value = int(input[y][x-2]+input[y][x-1])
            if value > largest: largest = value; pointer = [x-2, y]

while 1:
    if dir==">":pointer[0]+=1
    if dir=="<":pointer[0]-=1
    if dir=="^":pointer[1]-=1
    if dir=="v":pointer[1]+=1
    try:
        current = input[pointer[1]][pointer[0]]
        if current in "><^v":
            dir = current
            value = int(input[pointer[1]][pointer[0]-2]+input[pointer[1]][pointer[0]-1])
            
            if pointer[0]<0 or pointer[1]<0:1/0
            output=eval(str(output)+operation+str(value))
            
            if current==">":operation="+"
            if current=="<":operation="-"
            if current=="^":operation="*"
            if current=="v":operation="/"
    
    except:
        print output;break
\$\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.