Another solution for the socks (using frequencies, a bit higher level, avoiding explicit reduce):
(->>
[1 2 1 2 1 3 2]
(frequencies)
(map
(fn [[sock-color cnt]]
;determine if we have an odd or even number of socks for each color
(let [cnt' (if (odd? cnt) (dec cnt) cnt)]
;number of pairs of socks for a color
(/ cnt' 2))))
;total of all pairs
(apply +))
I like this solution. Occasionally, I fall in the trap of always looking for the “higher level” solution where a more “procedural” solution would be OK.
Second one, less imperative (using a library for dedupe-by):
(count
(sequence
(comp
;dedupe by below sea level
(medley.core/dedupe-by #(neg? %))
;at sea level
(filter zero?))
(reductions + [+1 -1 -1 -1 +1 -1 +1 +1])))
@ps ^^