Блог пользователя maomao90

Автор maomao90, история, 19 месяцев назад, По-английски

Code golf challenge for 1817D - Toy Machine

In case you do not know what code golf is, the objective is to write the shortest code possible that solves the problem.

After losing 41 rating from Codeforces Round 869 (Div. 1) and almost becoming yellow again, I was depressed and decided to try to upsolve this problem. Surprisingly, I was able to discover a very simple pattern that allowed me to come up with a short and cute solution. Why do I always only solve problems after contest and not during contest 😭

Anyways, here is my code in python:

n,k=map(int,input().split())
print(["RDLU"*(n-k-2)+"LDLU"*n+"RDL","LDRU"*(k-1)+"L"][k<n/2])

Here is my original C++ code that is more readable 204002089.

The prize for coming up with an even shorter code is an ego boost. Bonus points if you come up with an even simpler solution that is different from the pattern that I discovered.

  • Проголосовать: нравится
  • +52
  • Проголосовать: не нравится

»
19 месяцев назад, # |
  Проголосовать: нравится +13 Проголосовать: не нравится

I had the same solution — though it was longer because I don't know Python. :)

»
19 месяцев назад, # |
  Проголосовать: нравится +33 Проголосовать: не нравится

Python 3 (PyPy), 89 bytes

n,k=map(int,input().split())
print(["RDLU"*(n-k-2)+"LDLU"*n+"RDL","LDRU"*~-k+"L"][k<n/2])

Try it online!

»
19 месяцев назад, # |
  Проголосовать: нравится +28 Проголосовать: не нравится

I was able to get it down to 82 bytes

n,k=map(int,input().split())
print(["LURD"*(n+~k)+"ULDL"*n+"RDL","RULD"*k][k<n/2])
  • »
    »
    19 месяцев назад, # ^ |
      Проголосовать: нравится +10 Проголосовать: не нравится

    Got it down to 1 character less, so 81 bytes

    n,k=map(int,input().split())
    n+=~k
    print(["LURD"*n+"ULDL"*k+"RDL","RULD"*k][k<n])
    
»
19 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Nice contest