Skip to content

Instantly share code, notes, and snippets.

@mfikes
Created Dec 2, 2018
Embed
What would you like to do?
(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