DEV Community

Discussion on: AoC Day 2: Inventory Management System

Collapse
 
ballpointcarrot profile image
Christopher Kruse

I like the threaded use of update here in part 1 - my method used a transient map and returned a persistent copy at the end:

(ns aoc.aoc2)

(defn reduce-twos-threes
  "check the given frequency map n for twos or threes matches, and update
   the memo map to indicate if the string has a match. Used for a reducer."
  [memo n]
  (let [t-memo (transient memo)]
    (if (some (fn [[k v]] (= v 2)) n) 
      (assoc! t-memo :twos (inc (:twos t-memo))))
    (if (some (fn [[k v]] (= v 3)) n) 
      (assoc! t-memo :threes (inc (:threes t-memo))))
    (persistent! t-memo)))

(defn checksum [input]
  (let [sum-maps (map frequencies input)
        twos-threes (reduce reduce-twos-threes {:twos 0 :threes 0} sum-maps)]
    (* (:twos twos-threes) (:threes twos-threes))))
Collapse
 
trueneu profile image
Pavel Gurkov

Nice one. Is definitely faster than mine.