Skip to main content
More golfing
Source Link
Sherlock9
  • 12.3k
  • 1
  • 30
  • 68

Python 3, 109 99 104104 101 bytes

This is a simple answer that uses complex numbers, with input as a space-separated string or a newline-separated string. Golfing suggestions welcome!

Edit: -1013 bytes thanks to Labo. +5 bytes for converting to an int.

d=p=0
for r in input().split():d+=1-2*(r[0]<'R'r<'R');p+=1j**d*int(r[1:])
print(int(abs(p.real)+abs(p.imag)))

Ungolfing

def manhattan_rotation(seq, nsew=0, pos = 0):
    for rot in seq.split():
        # change direction
        if rot[0] == "L":
            nsew += -1 
        else:
            nsew += 1
        # move in that direction rot[1:] times
        pos += 1j ** nsew * int(rot[1:])
    return int(abs(pos.real)+abs(pos.imag))

Python 3, 109 99 104 bytes

This is a simple answer that uses complex numbers, with input as a space-separated string or a newline-separated string. Golfing suggestions welcome!

Edit: -10 bytes thanks to Labo. +5 bytes for converting to an int.

d=p=0
for r in input().split():d+=1-2*(r[0]<'R');p+=1j**d*int(r[1:])
print(int(abs(p.real)+abs(p.imag)))

Ungolfing

def manhattan_rotation(seq, nsew=0, pos = 0):
    for rot in seq.split():
        # change direction
        if rot[0] == "L":
            nsew += -1 
        else:
            nsew += 1
        # move in that direction rot[1:] times
        pos += 1j ** nsew * int(rot[1:])
    return int(abs(pos.real)+abs(pos.imag))

Python 3, 109 99 104 101 bytes

This is a simple answer that uses complex numbers, with input as a space-separated string or a newline-separated string. Golfing suggestions welcome!

Edit: -13 bytes thanks to Labo. +5 bytes for converting to an int.

d=p=0
for r in input().split():d+=1-2*(r<'R');p+=1j**d*int(r[1:])
print(int(abs(p.real)+abs(p.imag)))

Ungolfing

def manhattan_rotation(seq, nsew=0, pos = 0):
    for rot in seq.split():
        # change direction
        if rot[0] == "L":
            nsew += -1 
        else:
            nsew += 1
        # move in that direction rot[1:] times
        pos += 1j ** nsew * int(rot[1:])
    return int(abs(pos.real)+abs(pos.imag))
Correcting the answer
Source Link
Sherlock9
  • 12.3k
  • 1
  • 30
  • 68

Python 3, 109 9999 104 bytes

This is a simple answer that uses complex numbers, with input as a space-separated string or a newline-separated string. Golfing suggestions welcome!

Edit: -10 bytes thanks to Labo. +5 bytes for converting to an int.

d=p=0
for r in input().split():d+=1-2*(r[0]<'R');p+=1j**d*int(r[1:])
print(int(abs(p.real)+abs(p.imag)))

Ungolfing

def manhattan_rotation(seq, nsew=0, pos = 0):
    for rot in seq.split():
        # change direction
        if rot[0] == "L":
            nsew += -1 
        else:
            nsew += 1
        # move in that direction rot[1:] times
        pos += 1j ** nsew * int(rot[1:])
    return int(abs(pos.real)+abs(pos.imag))

Python 3, 109 99 bytes

This is a simple answer that uses complex numbers, with input as a space-separated string or a newline-separated string. Golfing suggestions welcome!

Edit: -10 bytes thanks to Labo.

d=p=0
for r in input().split():d+=1-2*(r[0]<'R');p+=1j**d*int(r[1:])
print(abs(p.real)+abs(p.imag))

Ungolfing

def manhattan_rotation(seq, nsew=0, pos = 0):
    for rot in seq.split():
        # change direction
        if rot[0] == "L":
            nsew += -1 
        else:
            nsew += 1
        # move in that direction rot[1:] times
        pos += 1j ** nsew * int(rot[1:])
    return abs(pos.real)+abs(pos.imag)

Python 3, 109 99 104 bytes

This is a simple answer that uses complex numbers, with input as a space-separated string or a newline-separated string. Golfing suggestions welcome!

Edit: -10 bytes thanks to Labo. +5 bytes for converting to an int.

d=p=0
for r in input().split():d+=1-2*(r[0]<'R');p+=1j**d*int(r[1:])
print(int(abs(p.real)+abs(p.imag)))

Ungolfing

def manhattan_rotation(seq, nsew=0, pos = 0):
    for rot in seq.split():
        # change direction
        if rot[0] == "L":
            nsew += -1 
        else:
            nsew += 1
        # move in that direction rot[1:] times
        pos += 1j ** nsew * int(rot[1:])
    return int(abs(pos.real)+abs(pos.imag))
More golfing
Source Link
Sherlock9
  • 12.3k
  • 1
  • 30
  • 68

Python 3, 109109 99 bytes

This is a simple answer that uses complex numbers, with input as a space-separated string or a newline-separated string. Golfing suggestions welcome!

Edit: -10 bytes thanks to Labo.

def m(s,d=0,p=0):d=p=0
 for r in sinput().split():d+=(d+=1-1)**2*(r[0]<"R"r[0]<'R');p+=1j**d*int(r[1:])
 return print(abs(p.real)+abs(p.imag))

Ungolfing

def manhattan_rotation(seq, nsew=0, pos = 0):
    for rot in seq.split():
        # change direction
        if rot[0] == "L":
            nsew += -1 
        else:
            nsew += 1
        # move in that direction rot[1:] times
        pos += 1j ** nsew * int(rot[1:])
    return abs(pos.real)+abs(pos.imag)

Python 3, 109 bytes

This is a simple answer that uses complex numbers, with input as a space-separated string or a newline-separated string. Golfing suggestions welcome!

def m(s,d=0,p=0):
 for r in s.split():d+=(-1)**(r[0]<"R");p+=1j**d*int(r[1:])
 return abs(p.real)+abs(p.imag)

Ungolfing

def manhattan_rotation(seq, nsew=0, pos = 0):
    for rot in seq.split():
        # change direction
        if rot[0] == "L":
            nsew += -1 
        else:
            nsew += 1
        # move in that direction rot[1:] times
        pos += 1j ** nsew * int(rot[1:])
    return abs(pos.real)+abs(pos.imag)

Python 3, 109 99 bytes

This is a simple answer that uses complex numbers, with input as a space-separated string or a newline-separated string. Golfing suggestions welcome!

Edit: -10 bytes thanks to Labo.

d=p=0
for r in input().split():d+=1-2*(r[0]<'R');p+=1j**d*int(r[1:])
print(abs(p.real)+abs(p.imag))

Ungolfing

def manhattan_rotation(seq, nsew=0, pos = 0):
    for rot in seq.split():
        # change direction
        if rot[0] == "L":
            nsew += -1 
        else:
            nsew += 1
        # move in that direction rot[1:] times
        pos += 1j ** nsew * int(rot[1:])
    return abs(pos.real)+abs(pos.imag)
Editing the answer
Source Link
Sherlock9
  • 12.3k
  • 1
  • 30
  • 68
Loading
Adding to the explanation
Source Link
Sherlock9
  • 12.3k
  • 1
  • 30
  • 68
Loading
Source Link
Sherlock9
  • 12.3k
  • 1
  • 30
  • 68
Loading