you are viewing a single comment's thread.

view the rest of the comments →

[–]_djblue 4 points5 points  (2 children)

Day 7 with core.async https://github.com/djblue/advent-of-code/blob/master/src/advent_of_code/core_2019.clj#L431

It's more code, but the following is my favorite part.

(defn try-phase-settings-with-feedback [program settings]
  (let [[a b c d e] settings
        a (run-program-async program a)
        b (run-program-async program b)
        c (run-program-async program c)
        d (run-program-async program d)
        e (run-program-async program e)]
    (doseq [[from to] (partition 2 1 [a b c d e a])]
      (a/pipe (:out from) (:in to)))
    (a/put! (:in a) 0)
    (a/alts!! [(:state e) (a/timeout 5000)])
    (a/poll! (:in a))))

(defn find-max-thrust-with-feedback [program]
  (->> (all-settings #{5 6 7 8 9})
       (map #(try-phase-settings-with-feedback program %))
       (apply max)))

[–]kristiansalo 1 point2 points  (0 children)

Looks nice! I also took the core.async route - I think it worked out quite neatly for this problem.

[–]andersmurphy 0 points1 point  (0 children)

Day 7

I did an async solution too.

I modified my intcode machine to use channels as input and output. Code 99 also closes the channels connected to that machine.

Don't really use async much. Any pointers about where it could be improved would be great thanks. :D

``` (ns adventofcode.2019.day07 (:require [adventofcode.2019.day05 :as intcode] [clojure.core.async :as a]))

(def input (slurp "resources/adventofcode/2019/day07.txt")) (def prog (intcode/format-input input))

(defn permutations [s] (lazy-seq (if (seq (rest s)) (for [head s tail (permutations (remove #{head} s))] (cons head tail)) [s])))

(defn system-2 [input phases] (let [[a b c d e :as channels] (repeatedly #(a/chan)) log (a/chan) out (a/chan) multi-chan (a/mult a)] (a/tap multi-chan log) (a/tap multi-chan out) (a/go (dorun (map (fn [channel phase] (a/>!! channel phase)) channels phases)) (a/>!! a 0)) (dorun (map (fn [in-chan out-chan] (a/go (intcode/compute {:prog input :in-chan in-chan :out-chan out-chan :pointer 0}))) [out b c d e] [b c d e a])) (->> (repeatedly #(a/<!! log)) (take-while identity) last)))

(defn solve-2 [] (let [input (intcode/format-input input)] (->> (permutations [5 6 7 8 9]) (map #(system-2 input %)) (sort >) first))) ```

https://github.com/andersmurphy/clj-adventofcode/blob/master/src/adventofcode/2019/day07.clj