projects
/
adventofcode2019.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
3887e35
)
Add correct day3pt2 solution
author
Jack Kinsey
<j.jameskinsey@gmail.com>
Wed, 4 Dec 2019 04:24:23 +0000
(23:24 -0500)
committer
Jack Kinsey
<j.jameskinsey@gmail.com>
Wed, 4 Dec 2019 04:24:23 +0000
(23:24 -0500)
src/adventofcode2019/day03.clj
patch
|
blob
|
blame
|
history
diff --git
a/src/adventofcode2019/day03.clj
b/src/adventofcode2019/day03.clj
index 17564deae33dd0e3aaad463c89607bdca9c5f731..d21968f9ad1aa30163386302315e1291cb3f43b3 100644
(file)
--- a/
src/adventofcode2019/day03.clj
+++ b/
src/adventofcode2019/day03.clj
@@
-19,32
+19,36
@@
(defn ints-between [x y]
(cond
(defn ints-between [x y]
(cond
- (> x y) (r
ange y (inc x
))
+ (> x y) (r
everse (range y (inc x)
))
(< x y) (range x (inc y))
:else [x]))
(defn points-diff [pos-a pos-b]
(let [[a-x a-y] pos-a
[b-x b-y] pos-b]
(< x y) (range x (inc y))
:else [x]))
(defn points-diff [pos-a pos-b]
(let [[a-x a-y] pos-a
[b-x b-y] pos-b]
- (
se
t (for [x (ints-between a-x b-x)
- y (ints-between a-y b-y)]
- [x y]))))
+ (
res
t (for [x (ints-between a-x b-x)
+
y (ints-between a-y b-y)]
+
[x y]))))
(defn wire-spec->points [wire]
(let [wire-ops (map spec->op wire)
trace-reduction (fn [{:keys [pos points]} op]
(let [new-pos (op pos)
new-points (points-diff pos new-pos)]
(defn wire-spec->points [wire]
(let [wire-ops (map spec->op wire)
trace-reduction (fn [{:keys [pos points]} op]
(let [new-pos (op pos)
new-points (points-diff pos new-pos)]
- {:pos new-pos :points (
set/union
points new-points)}))
- trace (reduce trace-reduction {:pos [0 0], :points
#{}
} wire-ops)]
+ {:pos new-pos :points (
concat
points new-points)}))
+ trace (reduce trace-reduction {:pos [0 0], :points
[]
} wire-ops)]
(:points trace)))
(defn find-intersections [wires]
(:points trace)))
(defn find-intersections [wires]
- (let [wires-points (map wire-spec->points wires)]
- (set/difference (apply set/intersection wires-points) #{[0 0]})))
+ (let [wires-points (map wire-spec->points wires)
+ intersections (apply set/intersection (map set wires-points))
+ find-steps (fn [i pos] (when (intersections pos) {pos (inc i)}))
+ step-counts (map #(map-indexed find-steps %) wires-points)]
+ (apply (partial merge-with +) (flatten step-counts))))
(defn day03 []
(let [wires (parse-input (input-file))
manhattan-distance (fn [[x y]] (+ (Math/abs x) (Math/abs y)))
intersections (find-intersections wires)]
(defn day03 []
(let [wires (parse-input (input-file))
manhattan-distance (fn [[x y]] (+ (Math/abs x) (Math/abs y)))
intersections (find-intersections wires)]
- (println (apply min (map manhattan-distance intersections)))))
+ (println (apply min (map manhattan-distance (keys intersections))))
+ (println (apply min (vals intersections)))))