Skip to content

Instantly share code, notes, and snippets.

@mfikes
Created December 2, 2018 17:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mfikes/d2cf0c9de3808564f7b039f10635ede4 to your computer and use it in GitHub Desktop.
Save mfikes/d2cf0c9de3808564f7b039f10635ede4 to your computer and use it in GitHub Desktop.
(ns advent-2018.day-02
(:require
#?(:cljs [planck.core :refer [line-seq read-string]])
[#?(:clj clojure.java.io :cljs planck.io) :as io]
[advent-2018.day-01 :as day-01])
(:import (java.util.concurrent Executors ExecutorService)))
(def input (->> "advent_2018/day_02/input" io/resource io/reader line-seq))
(defn count-with-exact [box-ids n]
(->> box-ids
(map frequencies)
(map vals)
(keep #(some #{n} %))
count))
(defn part-1 []
(* (count-with-exact input 2) (count-with-exact input 3)))
(defn delete [idx s]
(str (subs s 0 idx) (subs s (inc idx))))
;; Single thread
(defn part-2 []
(some (fn [idx]
(->> input
(map (partial delete idx))
day-01/first-duplicate))
(range)))
;; Parallel version
(def pool (Executors/newCachedThreadPool))
(defn part-2' []
(let [p (promise)]
(doseq [idx (range 26)]
(.submit pool
#(when-some [result (->> input
(map (partial delete idx))
day-01/first-duplicate)]
(deliver p result))))
@p))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment