forked from borkdude/advent-of-cljc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dbedd03
commit 2825734
Showing
3 changed files
with
235 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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))))) |