Hacker News new | past | comments | ask | show | jobs | submit login

Python looks excruciatingly slow to me. If you want fast I believe you need to think and write in vector languages like kdb+/q. I am not a kdb+ expert by any means and my code can probably use more q primitives, but here was my solution in ~2 minutes:

  i1:("I  I";" ")0: `:1.txt;
  sum {abs last deltas x }each flip asc each i1 / answer 1
  sum {x * sum x = i1[1]}each i1[0] / answer 2



Everything autocompletes in Python.

    from collections import *
    xys = list(map(int, open(0).read().split()))
    xs = xys[::2]
    ys = xys[1::2]
    xs.sort()
    ys.sort()
    print(sum(abs(x-y) for x,y in zip(xs,ys)))
    
    yc = Counter(ys)
    
    print(sum(((yc[x])*x for x in xs)))


this is the least amount of loc I can think of rn

    data = { i+1 : sorted([ x for x in list(map(int, open('input').read().split()))[i::2]]) for i in range(2) }
    total_distance = sum(list(map(lambda x: abs(x[0]-x[1]), zip(data[1], data[2]))))
    print("part 1:", total_distance)
    similarity_score = sum(list(map(lambda x: (x*data[2].count(x))*data[1].count(x), set(data[1]).intersection(data[2]))))
    print("part 2:", similarity_score)


That’s cool, but horribly ugly! What does production kdb+ look like? Is it similarly terse, maybe Perl-ish, or was this purely for speed?

What does each flip asc do?


i1 is Nx2. asc each i1 sorts each N in ascending order. flip transposes to 2xN.




Consider applying for YC's Summer 2025 batch! Applications are open till May 13

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: