daily pastebin goal
59%
SHARE
TWEET

Untitled

a guest Dec 15th, 2018 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns aoc2018-clj.day13
  2.   (:require [clojure.string :as str]
  3.             [clojure.java.io :as io]
  4.             [clojure.math.combinatorics :as combs]
  5.             [clojure.set :as set]))
  6.  
  7. (def track (str/split-lines (-> "day13.txt" io/resource slurp)))
  8. (def race-track (mapv vec track))
  9.  
  10. (defn intersection-rule [{:keys [loc dir turn-count] :as car}]
  11.   (assoc car :dir
  12.    (case (mod turn-count 3)
  13.      0  ({\> \^ \< \v \v \> \^ \<} dir) ;; turn left
  14.      1  dir                             ;; straight
  15.      2 ({\> \v \< \^ \v \< \^ \>} dir)) ;; turn right
  16.    :turn-count (inc turn-count)))
  17.  
  18. (defn change-dir
  19.   [{:keys [loc dir turn-count] :as car} road-segment]
  20.   (case road-segment
  21.    \/ (update car :dir {\^ \> \v \< \> \^ \< \v})
  22.    \\ (update car :dir {\^ \< \v \> \> \v \< \^})
  23.    \+ (intersection-rule car)
  24.    car))
  25.  
  26. (defn drive [{:keys [loc dir turn-count] :as car}]
  27.  (let [from->to {\> [0 1] \< [0 -1] \^ [-1 0] \v [1 0]}
  28.        destination (mapv + (from->to dir) loc)
  29.        road-segment (get-in race-track destination)]
  30.    (-> (change-dir car road-segment)
  31.        (assoc :loc destination))))
  32.  
  33. (defn make-cars [initial-track]
  34.   (for [i (range (count initial-track))
  35.         j (range (count initial-track))
  36.         :let [dir (get-in initial-track [i j])]
  37.         :when (contains? #{\< \> \^ \v} dir)]
  38.     {:loc [i j] :dir dir :turn-count 0}))
  39.  
  40. (defn advance-cars [cars]
  41.  (let [cars (sort-by :loc cars)]
  42.   (reduce
  43.    (fn [[full? new-cars] car]
  44.     (let [next-car (drive car)
  45.           next-loc (:loc next-car)]
  46.       (if (full? next-loc)
  47.         (reduced [#{} [(assoc next-car :collision true)]])
  48.         [(conj (set/difference full? #{(:loc car)}) next-loc)
  49.          (conj new-cars next-car)])))
  50.    [(set (map :loc cars)) []]
  51.    (sort-by :loc cars))))
  52.  
  53. (defn collision? [cars]
  54.   (some #(when (contains? % :collision) %) cars))
  55.  
  56. (defn part1 []
  57.   (loop [[full? cars] [#{} (make-cars race-track)]]
  58.    ;(show-track (place-cars (clear-track race-track) cars))
  59.     (if-let [collision (collision? cars)]
  60.       collision
  61.       (recur (advance-cars cars)))))
  62.  
  63. ;;pt 2
  64. (defn advance-cars2 [cars]
  65.   (loop [[c & cs] (sort-by :loc cars)
  66.          full? (set (map :locs cars))
  67.          new-cars []]
  68.     (if (empty? c)
  69.       new-cars
  70.       (let [new-car (drive c)
  71.             new-loc (:loc new-car)]
  72.         (if (full? new-loc)
  73.           (recur (remove #(= (:loc %) new-loc) cs) ;remove preserves sorted orer
  74.                  (disj full? (:loc c) new-loc)
  75.                  (remove #(= (:loc %) new-loc) new-cars))
  76.           (recur cs
  77.                  (conj (disj full? (:loc c)) new-loc)
  78.                  (conj new-cars new-car)))))))
  79. (defn part2 []
  80.   (some #(when (= 1 (count %)) %)
  81.         (iterate advance-cars2 (make-cars race-track))))
  82. (part1)
  83. (part2)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top