diff --git a/src/aoc/y2018/d06/bgrabow.cljc b/src/aoc/y2018/d06/bgrabow.cljc index e11bb5d..30689bf 100644 --- a/src/aoc/y2018/d06/bgrabow.cljc +++ b/src/aoc/y2018/d06/bgrabow.cljc @@ -58,17 +58,6 @@ new-closest-ps (if (second new-closest-ps) acc (update acc (first new-closest-ps) #(conj % [x y]))))))))) - -(defn on-bounding-box [x-bounds y-bounds p] - (or (some #{(first p)} x-bounds) - (some #{(second p)} y-bounds))) - -(defn bounding-box [[x-min x-max] [y-min y-max]] - (for [x (range x-min (inc x-max)) - y (range y-min (inc y-max)) - :when (on-bounding-box [x-min x-max] [y-min y-max] [x y])] - [x y])) - (defn infinite-region? [x-bounds y-bounds [_ territory]] (let [group-x-vals (into #{} (map first territory)) group-y-vals (into #{} (map second territory))] @@ -89,27 +78,6 @@ (apply max-key (comp count second)) second count))) -; -;(def points (parse input)) -;(def x-bounds (map first (min-max-by first points))) -;(def y-bounds (map second (min-max-by second points))) -;(scan-x (first x-bounds) (first y-bounds) points) -;(def x (first x-bounds)) -;(def y (first y-bounds)) -;(def sorted-ps (sort-by #(manhattan-distance [(first x-bounds) (first y-bounds)] %) points)) -;(def m-dist #(manhattan-distance [(first x-bounds) (first y-bounds)] %)) -;(m-dist (first sorted-ps)) -;(map m-dist sorted-ps) - -;(def bbox (into #{} (bounding-box x-bounds y-bounds))) -;(def closest-point-map (filter (comp identity second) (apply merge (map #(scan-x (first x-bounds) % points))))) -; (range (first y-bounds) (second y-bounds)))))) -;(def points-with-infinite-region (into #{} (map second (filter #(bbox (first %)) closest-point-map)))) -;(def remaining-map (remove #(points-with-infinite-region (first %)) closest-point-map)) -;(def freqs (frequencies (map second remaining-map))) -;(def answer (apply max-key second freqs)) - -#_(def points-with-infinite-region (into #{} (map closest-point-map (bounding-box x-bounds y-bounds)))) (defn normalize [[x y] points] (map (fn [[xp yp]] [(Math/abs (- x xp)) diff --git a/src/aoc/y2018/d09/bgrabow.cljc b/src/aoc/y2018/d09/bgrabow.cljc new file mode 100644 index 0000000..628570b --- /dev/null +++ b/src/aoc/y2018/d09/bgrabow.cljc @@ -0,0 +1,125 @@ +(ns aoc.y2018.d09.bgrabow + (:refer-clojure :exclude [read-string format]) + (:require + [aoc.utils :as u :refer [deftest read-string format]] + [aoc.y2018.d09.data :refer [input answer-1 answer-2]] + [clojure.test :refer [is testing]]) + #?(:clj (:import [java.util ArrayDeque]))) + +(defn parse [input] + (let [[n-players last-marble] (map u/parse-int (re-seq #"\d+" input))] + {:n-players n-players + :last-marble last-marble})) + +(defn head-right-v [n v] + (vec (concat (subvec v n) + (subvec v 0 n)))) + +(defn head-left-v [n v] + (vec (concat (subvec v (- (count v) n)) + (subvec v 0 (- (count v) n))))) + +#?(:clj (do + (defn head-right [n ^ArrayDeque dequeue] + (doall (repeatedly n #(.addLast dequeue (.removeFirst dequeue)))) + dequeue) + + (defn head-left [n ^ArrayDeque dequeue] + (doall (repeatedly n #(.addFirst dequeue (.removeLast dequeue)))) + dequeue))) + +;(defn next-circle-vector [circle next-scoring-marble] +; (->> circle +; (head-right-v 1) +; (#(reduce (fn [circle marble] +; (->> circle +; (head-right 1) +; (conj marble))) +; % +; (range (- next-scoring-marble 22) next-scoring-marble))))) + +(defn next-circle-vector [circle next-scoring-marble] + [(vec (concat (interleave (range (- next-scoring-marble 4) next-scoring-marble) (subvec circle 20 24)) + (subvec circle 24) + (subvec circle 0 1) + (interleave (subvec circle 1 19) (range (- next-scoring-marble 22) (- next-scoring-marble 4))))) + (+ (get circle 19) + next-scoring-marble)]) + +(defn next-circle + #?(:clj ([^ArrayDeque circle next-scoring-marble] + (head-right 1 circle) + (doseq [x (range (- next-scoring-marble 22) next-scoring-marble)] + (head-right 1 circle) + (.addLast circle x)) + (head-left 7 circle) + (let [removed (.removeLast circle)] + [circle (+ removed next-scoring-marble)])) + :cljs ([circle next-scoring-marble] + [(vec (concat (interleave (range (- next-scoring-marble 4) next-scoring-marble) (subvec circle 20 24)) + (subvec circle 24) + (subvec circle 0 1) + (interleave (subvec circle 1 19) (range (- next-scoring-marble 22) (- next-scoring-marble 4))))) + (+ (get circle 19) + next-scoring-marble)]))) + +(defn scores [circle scoring-marbles] + (map second (drop 1 (reductions (fn [[circle _] scoring-marble] + (next-circle circle scoring-marble)) [circle nil] scoring-marbles)))) + +(defn circle-after-46-turns [^Integer allocate-size] + #?(:clj (let [circle (ArrayDeque. allocate-size)] + (doseq [e [42 4 43 18 44 19 45 2 24 20 + 25 10 26 21 27 5 28 22 29 11 + 30 1 31 12 32 6 33 13 34 3 35 + 14 36 7 37 15 38 0 39 16 40 8 41]] + (.addLast circle e)) + circle) + :cljs [42 4 43 18 44 19 45 2 24 20 + 25 10 26 21 27 5 28 22 29 11 + 30 1 31 12 32 6 33 13 34 3 35 + 14 36 7 37 15 38 0 39 16 40 8 41])) + +(defn circle-vec-after-46-turns [] + [42 4 43 18 44 19 45 2 24 20 + 25 10 26 21 27 5 28 22 29 11 + 30 1 31 12 32 6 33 13 34 3 35 + 14 36 7 37 15 38 0 39 16 40 8 41]) + +;(println (seq (first (next-circle-dequeue (circle-after-46-turns 100) 69)))) +;(println (seq (first (next-circle-vector (circle-vec-after-46-turns) 69)))) + +(defn all-scoring-turns [] + (concat '(32 63) (scores (circle-after-46-turns 10000000) + (iterate #(+ 23 %) 69)))) + +(defn scoring-players [n-players] + (drop 1 (iterate #(rem (+ % 23) n-players) 0))) + +(defn solve-1 [] + (let [{:keys [n-players last-marble]} (parse input) + n-scoring-marbles (quot last-marble 23) + player-scores (zipmap (range n-players) (repeat 0))] + (->> (reduce (fn [p-s [player score]] (update p-s player #(+ % score))) + player-scores + (map vector (scoring-players n-players) (take n-scoring-marbles (all-scoring-turns)))) + vals + (apply max)))) + +(defn solve-2 [] + (let [{:keys [n-players last-marble]} (parse input) + n-scoring-marbles (quot (* 100 last-marble) 23) + player-scores (zipmap (range n-players) (repeat 0))] + (->> (reduce (fn [p-s [player score]] (update p-s player #(+ % score))) + player-scores + (map vector (scoring-players n-players) (take n-scoring-marbles (all-scoring-turns)))) + vals + (apply max)))) + +(deftest ^:skip-cljs part-1 + (is (= (str answer-1) + (str (solve-1))))) + +(deftest ^:skip-cljs part-2 + (is (= (str answer-2) + (str (solve-2))))) diff --git a/src/aoc/y2018/d10/bgrabow.cljc b/src/aoc/y2018/d10/bgrabow.cljc new file mode 100644 index 0000000..3444c5e --- /dev/null +++ b/src/aoc/y2018/d10/bgrabow.cljc @@ -0,0 +1,110 @@ +(ns aoc.y2018.d10.bgrabow + (:refer-clojure :exclude [read-string format]) + (:require + [aoc.utils :as u :refer [deftest read-string format]] + [aoc.y2018.d10.data :refer [input answer-1 answer-2]] + [clojure.test :refer [is testing]] + [clojure.string :as str])) + +(defn parse [input] + (->> input + str/split-lines + (map #(->> % + (re-seq #"-?\d+") + (map u/parse-int))) + (map (fn [[x y dx dy]] + {:x x + :y y + :dx dx + :dy dy})))) + +(defn step-n [n [x y] [dx dy]] + [(+ x (* dx n)) + (+ y (* dy n))]) + +(defn step [[x y] [dx dy]] + [(+ x dx) + (+ y dy)]) + +(defn time-until-nearby [pa pb] + (let [h (- (:y pa) (:y pb)) + ddy (- (:dy pb) (:dy pa))] + (quot h ddy))) + +(defn min-max-by [pred coll] + (when coll + (let [value-predvalue-pairs (map (fn [x] [x (pred x)]) coll)] + (map first (reduce (fn [[min-pair max-pair] new-pair] + [(min-key second min-pair new-pair) + (max-key second max-pair new-pair)]) + [(first value-predvalue-pairs) (first value-predvalue-pairs)] + (rest value-predvalue-pairs)))))) + +(defn y-height [stars] + (let [[y-min y-max] (min-max-by identity (map second stars))] + (- y-max y-min))) + +(defn stars-to-string [stars] + (let [[x-min x-max] (map first (min-max-by first stars)) + [y-min y-max] (map second (min-max-by second stars)) + stars-set (into #{} stars)] + (->> (for [y (range y-min (inc y-max))] + (apply str (map (fn [x] + (if (stars-set [x y]) \* \space)) + (range x-min (inc x-max))))) + (str/join \newline)))) + +(defn left-most-x-neighbor [all-points point] + (let [x-vals (into #{} (map first all-points))] + (loop [x (first point)] + (if (x-vals (dec x)) + (recur (dec x)) + x)))) + +(defn group-stars [stars] + (group-by + (partial left-most-x-neighbor stars) + stars)) + +(defn solve-1 [] + (let [parsed-input (parse input) + [fastest-down fastest-up] (min-max-by :dy parsed-input) + search-start-time (- (time-until-nearby fastest-down fastest-up) + 10) + velocities (map #(map % [:dx :dy]) parsed-input)] + (->> (loop [stars (->> parsed-input + (map #(map % [:x :y])) + (#(map (fn [star v] (step-n search-start-time star v)) % velocities))) + height (y-height stars) + time search-start-time] + (let [new-stars (map step stars velocities) + new-height (y-height new-stars)] + (if (> new-height height) + stars + (recur new-stars new-height (inc time))))) + stars-to-string))) + +(defn solve-2 [] + (let [parsed-input (parse input) + [fastest-down fastest-up] (min-max-by :dy parsed-input) + search-start-time (- (time-until-nearby fastest-down fastest-up) + 10) + velocities (map #(map % [:dx :dy]) parsed-input)] + (->> (loop [stars (->> parsed-input + (map #(map % [:x :y])) + (#(map (fn [star v] (step-n search-start-time star v)) % velocities))) + height (y-height stars) + time search-start-time] + (let [new-stars (map step stars velocities) + new-height (y-height new-stars)] + (if (> new-height height) + time + (recur new-stars new-height (inc time)))))))) + +(deftest part-1 + (is (= 2072437284 + (hash (str (solve-1)))))) + +(deftest part-2 + (is (= (str answer-2) + (str (solve-2))))) \ No newline at end of file