3 ms·
I love looking at AoC solution megathreads on reddit. So many languages and so many different approaches are hard to find and observe anywhere.
by middayc 2y ago
I love looking at AoC solution megathreads on reddit. So many languages and so many different approaches are hard to find and observe anywhere.
- jodrellblank 2y agoI feel like those threads would make great research opportunities. We often hear people say that code should be for people to read, incidentally for machines to execute, just be amazed at how much the readability varies from answer to answer. Skimming some, the core of Part 1 after people have parsed and sorted: Python: sum([abs(x-y) for x,y in zip(left,right)) TypeScript: list1.reduce((acc, cur, i) => { return acc += Math.abs(cur - list2[i]) }, 0) Common Lisp: (reduce #'+ (mapcar (lambda (l r) (abs (- l r))) sorted-left sorted-right)))) Julia: sum(abs.(list1 .- list2)) Rust: Ok(zip(left, right).map(|(l, r)| l.abs_diff(r)).sum() F#: Seq.map2 (fun x y -> abs (x - y)) xs ys |> Seq.sum APL: +/|-⌿ Haskell: map abs $ zipWith (-) column2 column1 and then all the submissions which don't do anything like this, and have manual loops and indexing and clunky data representations or performance-optimized data representations, etc. etc.