Skip to content
This repository has been archived by the owner on Oct 1, 2019. It is now read-only.

bgrabow soln d09 & d10 #94

Merged
merged 72 commits into from
Dec 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
61d0585
Add ignore rules for Cursive/IntelliJ project files
Dec 4, 2018
b41be74
Add d04/bgrabow namespace
Dec 4, 2018
78af561
Copy code from personal project
Dec 4, 2018
ca14cec
Add 2018 day 4 solution
Dec 5, 2018
7eed08a
Fix whitespace in .gitignore
Dec 5, 2018
730b4d1
Fix js errors. Remove extranous repl test code.
Dec 5, 2018
36e6290
Merge remote-tracking branch 'upstream/master'
Dec 5, 2018
1e9f5b0
Revert changes to .gitignore
Dec 5, 2018
5fddf37
Merge remote-tracking branch 'upstream/master'
Dec 5, 2018
c674449
Add bgrabow solution file
Dec 5, 2018
247a4c2
Create iterative and recursive solutions
Dec 5, 2018
c762047
Created faster recursive solution for part 1. Solved part 2.
Dec 5, 2018
76d3db0
Clean up obsolete code
Dec 5, 2018
3474b71
Converted algo to vector-based stacks. Worse performance!
Dec 5, 2018
f134cc5
Revert to seq-based algorithm. Clean up obsolete code.
Dec 5, 2018
ac91643
Use integers instead of characters. Faster!
Dec 5, 2018
823e446
Clean formatting
Dec 5, 2018
97c7436
Merge branch 'master' into master
bgrabow Dec 5, 2018
231bffb
Fix merge conflict. Remove obsolete code.
Dec 5, 2018
5dbcad0
Merge remote-tracking branch 'origin/master'
Dec 5, 2018
6e06f00
Remove extraneous EOF character.
Dec 5, 2018
e893311
Add solution for 2018 day 1
Dec 5, 2018
8cda204
Add solution for 2018 day 2. Use == to speed up solution for day 5.
Dec 5, 2018
8f82ad5
Add day 3 solution
Dec 5, 2018
ee6825d
Refactor algo to use reduce
Dec 5, 2018
e8333ff
Merge branch 'master' into master
bgrabow Dec 5, 2018
73e434e
Merge remote-tracking branch 'upstream/master'
Dec 6, 2018
582c30d
Solve d06 part 1
Dec 6, 2018
c5033e7
Redesign scanning algo
Dec 7, 2018
514318b
Fix scanning algo. Finally the right answer!
Dec 7, 2018
90242c4
Merge remote-tracking branch 'upstream/master'
Dec 7, 2018
28c45e6
Solve part 1
Dec 7, 2018
c39c781
Solve part 2
Dec 7, 2018
1f74759
Merge remote-tracking branch 'upstream/master'
Dec 7, 2018
458debc
Fix cljs PersistentQueue usage
Dec 7, 2018
9393db1
Cleaned up warnings. Replaced computation of alphabet with literal re…
Dec 7, 2018
f889ab9
Merge remote-tracking branch 'upstream/master'
Dec 7, 2018
7b7a677
Revert changes to upstream files
Dec 7, 2018
4030026
Brute force solution for part 2
Dec 7, 2018
f0a8157
Remove println in part 2 solution
Dec 7, 2018
1c14f7a
Vastly improve part 2 soln. Needs clean up of dev work.
Dec 7, 2018
dab23d4
Cleaned up rich comments.
Dec 7, 2018
f641fff
Merge remote-tracking branch 'upstream/master'
Dec 8, 2018
2398125
Fix outdated name
Dec 8, 2018
42d8ea9
Refactor part 2 soln to graph traversal instead of searching linearly
Dec 8, 2018
ba728ed
Plan the refactor for part 1
Dec 8, 2018
dddf904
Refactor part 1 to use graph traversal to find open nodes
Dec 8, 2018
7eeb38c
Merge remote-tracking branch 'upstream/master'
Dec 8, 2018
07651d6
Add file for day 8.
Dec 8, 2018
e81d9a1
Solve day 8
Dec 8, 2018
5d5b38a
Merge remote-tracking branch 'upstream/master'
Dec 8, 2018
95cdc97
Fix part 1
Dec 8, 2018
6b885ea
Solve both parts at the same time!
Dec 9, 2018
87ba4d7
Clean up obsolete code
Dec 9, 2018
6d9198e
Merge remote-tracking branch 'upstream/master'
Dec 9, 2018
5fd37d6
Add soln file for d09
Dec 9, 2018
3b4562a
Slow solution for part 1, plus some planning for a faster solution
Dec 9, 2018
fbb68ee
Use ArrayDeque to massively speed up soln 1 & 2.
Dec 10, 2018
1ad9acc
Clean up obsolete code
Dec 10, 2018
9864421
Merge remote-tracking branch 'upstream/master'
Dec 10, 2018
412755a
Solve d10 part 1 & 2
Dec 10, 2018
57044d0
Merge remote-tracking branch 'upstream/master'
Dec 10, 2018
dab18b6
Use time estimate heuristic to fast-forward before real search.
Dec 10, 2018
5647595
Merge remote-tracking branch 'upstream/master'
Dec 10, 2018
ca69c04
Merge remote-tracking branch 'upstream/master'
Dec 11, 2018
f6689e0
Added some letters to the constellation map and cleaned up code for
Dec 11, 2018
ffe4003
Parse borkdude input in preparation for merge upstream
Dec 11, 2018
344c06f
Merge remote-tracking branch 'upstream/master'
Dec 11, 2018
f1b4ce7
Fix cljs compile errors. Skip cljs tests.
Dec 11, 2018
04c6d28
Merge remote-tracking branch 'upstream/master'
Dec 11, 2018
af29afa
Remove my input. Remove obsolete code.
Dec 11, 2018
5276da4
Convert part-1 answer to hash value to obfuscate solution
Dec 11, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 0 additions & 32 deletions src/aoc/y2018/d06/bgrabow.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand All @@ -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))
Expand Down
125 changes: 125 additions & 0 deletions src/aoc/y2018/d09/bgrabow.cljc
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)))))
110 changes: 110 additions & 0 deletions src/aoc/y2018/d10/bgrabow.cljc
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)))))