Fork me on GitHub

Whoah, pretty much all the solutions to Dec6/pt1 saw that you could just add all of the distances from each node to the root. I didn't see that so I added each node's orbiter count multiplied by the depth of the node—about an order of magnitude less efficient, but still works:


(defn count-orbits
  ([orbits] (count-orbits orbits 1 :o-COM))
  ([orbits depth node]
   (let [orbiters (get-orbiters-of node orbits)]  ;; (:o-A :o-X)
     (if (seq orbiters)
       (+ (* depth (count orbiters))
          (->> orbiters
               (map (fn [orbiter] (count-orbits orbits (inc depth) orbiter)))
               (reduce +)))


Part 2 is a doozy and I love it.


I have no idea how I'm gonna visualize this, though, hahahah


Seriously, though, twists like this are what make me love these challenges. And I'm glad he's steadily building on the same machine this year.


I have real trouble understanding how part 2 is supposed to work… 😢


Can anyone explain part2 without spoilers?


• Each amplifier is given their phase as their first input, just like in part 1. • Amplifier A receives input '0'. • Amplifier A runs and emits 1 or more elements to output. • Amplifier A tries to read an input, but has no input to read yet, and so we'll hold off on evaluating it further for now. • Amplifier B consumes A's outputs as its new inputs. • Amplifier B runs and consumes its inputs until it runs out, emitting outputs for C to consume • ... • Etc., until E emits outputs for A to consume as inputs. • ... • Eventually, all the machines will formally halt (opcode 99), and when the last machine does, its output is the output of the array as a whole.

👏 4

(This is just a rephrasing of the information as it exists in part 2)


“so we’ll hold off on evaluating it further for now” THAT!


I think I get it now… Thanks


Now I just have to figure out how to do this 😂


They key is to understand that there are multiple VMs running now, each yielding to the next while keeping state (never need to reset now)

🙂 4

I'm very confused. Aren't the amplifiers only supposed to send the thruster signal to the next amp, each amp in turn increasing this number? Or do all of the amplifiers have their own thruster signal state, which only that specific amp increases, only to be added up when it finally halts? (And then communicating with the other amps via in/out signals that don't hold the actual thruster signal.)


When you hold off evaluating further because you have no more inputs, do you keep the offset and resume from where you suspended in the next iteration?


@UCW9TUDNK Each amplifier consumes inputs and generates outputs; those outputs are fed as inputs to the next amp in the chain, as shown in the pictures.


@U0DB715GU Correct. You can think of it as 'the amp is still running, but it's waiting for the 'in' operation to complete'. Similar to a CPU idling until data arrives from disk.


> And I'm glad he's steadily building on the same machine this year. Yes but... I had a bug apparently in my previous code (of the "why did this work before" variety) so that took me a while to debug.

simple_smile 4

ugh. today's part 2 is going to eat my whole weekend


My part 2 is working for examples but doesnt halt with puzzle input :/


finally finished part 2. solution is horrible though


Note to self: don't try to print infinite lazy seqs

😄 8

(set! *print-length* 20)
=> 20
(println (range))
(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...)
=> nil


Seemed it wasn't halting actually was trying to print a lazy seq


I can't tell you the number of times I've had to kill cider because I did that in AoC.

💯 8

Haskell does that better


I did run a bit into nested data structure confusion this round. Curious to see how others did it.


I always make sure to set*print-length* and*print-level* if I’m printing off large/lazy structures

👍 8

I was wondering if someone would use channels :)


I did something very similar. Just quite messier


I also did something similar, but I didn't use a go-loop... then had to deal with blocking IO. Argh! Should definitely have used a go-loop! Great job


wow, great idea!


I see the comments now. Thanks! 🙂 I didn’t see a way to solve the problem by passing state, but it is clearly possible looking at other solutions. I am also curious about ideas with other constructs that pass data from one process to another.


In this case, you can basically do it by forcing the asynchronous system to be fully synchronous; you impose some global order of execution across all amplifiers, and hop from one to the other as needed. It certainly feels kludgier, though. :)


Interesting @erwinrooijakkers, though it feels core.async is not an exact fit for the problem, as asynchronous operations is not necessary to solve it


Python generators (or ES6 generators in JS) are a better fit - do these have an implementation in Clojure?


Just exhausting all input and continue running tje next amp works also but that is basically a manual yield


Ugh, got part 1 pretty quickly, but for part 2 I had to refactor a large part of the intcode machine. I used core.async too. Will share later.


Hallo Clojurians - for Day 7, I am trying to figure out how can I feed input to the amplifiers. Context, my Intcode interpreter uses (read-line) for opcode 3 which reads from IN (ps: please read this as "star in star", Slack turns it into bold!). I've been exploring binding IN to a Reader that will successively return phase and input on each call to read-line. The default impl. of IN is a LineNumberingPushbackReader that wraps System/in. I want to make my own LineNumberingPushbackReader that closes over phase and input and returns them on succesive calls to read-line. My Clojure is a bit rusty, so I'm not exactly sure about the best way to proceed. Any tips?


Like this?

(with-in-str "line1\nline2"
  [(read-line) (read-line)])
; => ["line1", "line2"]

👏 8

Thanks much 🙂


I knew about with-out-str , never searched for with-in-str!


In the end I am pretty happy with my solution. I always am if I can use the same code for part 1 and 2 (I realize that my code is rather a beginner level… but in the end I get it somehow done 😆). Thanks to @fellshard for the explanation so I could finally understand what was going on! 🙏 <>

🙏 4
🙂 4

I am still really struggling with day5


I keep running up against this instruction: ("101" "-80" "224" "224")


this seems to be saying "read position -80" which doesn't make sense


I've completely rewritten my code a couple times now. my intcode computer passes day2 and and all test cases I've found so far


all of my diagnostic codes come back 0


Doesn't "101" mean read 1st parameter in immediate mode, so -80 should be interpreted as a value?


AFAICT "101" would be padded to "00101"?

💡 4

> read right-to-left from the opcode


@lilactown I struggled with something similar, and then found a line in the instructions "Parameters that an instruction writes to will never be in immediate mode." - this line seems contradictory to me, but that was the problem with my code!

💡 4

yeah I misread the problem, I was reading the modes left-to-right 😵


very frustrating


(set! *print-length* 20)
=> 20
(println (range))
(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...)
=> nil