adventofcode

Happy Advent 2020! Please put answers in the pinned threads or create one if it does not exist yet. | https://github.com/adventofcode-clojurians/adventofcode-clojurians | Join the private leaderboard with code 217019-4a55b8eb
jrwdunham 2019-12-07T04:06:30.377900Z

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:

jrwdunham 2019-12-07T04:06:32.378300Z

(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 +)))
       0))))

fellshard 2019-12-07T06:12:49.378700Z

Part 2 is a doozy and I love it.

fellshard 2019-12-07T06:13:49.379Z

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

fellshard 2019-12-07T06:26:12.379500Z

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.

2019-12-07T06:41:15.380Z

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

2019-12-07T06:44:55.380500Z

Can anyone explain part2 without spoilers?

✅ 2
pesterhazy 2019-12-07T10:24:39.382200Z

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)

🙂 1
uosl 2019-12-07T13:12:08.385500Z

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.)

yenda 2019-12-07T13:40:15.387800Z

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?

fellshard 2019-12-07T14:02:42.390500Z

@regen 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.

fellshard 2019-12-07T14:03:51.390700Z

@yenda 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.

fellshard 2019-12-07T06:54:32.380600Z

• 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.

👏 1
fellshard 2019-12-07T06:54:50.380800Z

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

2019-12-07T06:55:59.381Z

OOOOOOHHHHH!

2019-12-07T06:56:14.381200Z

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

2019-12-07T06:56:34.381400Z

I think I get it now… Thanks

2019-12-07T06:56:49.381600Z

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

pesterhazy 2019-12-07T10:25:49.383300Z

> 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.

1
uosl 2019-12-07T12:23:26.383600Z

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

erwinrooijakkers 2019-12-07T12:49:06.383800Z

+1

yenda 2019-12-07T13:25:35.387Z

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

mpcjanssen 2019-12-07T13:40:19.388100Z

finally finished part 2. solution is horrible though

mpcjanssen 2019-12-07T13:43:20.388800Z

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

😄 2
mpcjanssen 2019-12-07T13:44:07.390Z

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

fellshard 2019-12-07T14:04:26.391400Z

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

💯 2
mpcjanssen 2019-12-07T14:15:25.392Z

Haskell does that better

mpcjanssen 2019-12-07T14:16:14.393300Z

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

enforser 2019-12-07T14:32:38.394100Z

I always make sure to set https://clojuredocs.org/clojure.core/*print-length* and https://clojuredocs.org/clojure.core/*print-level* if I’m printing off large/lazy structures

👍 2
mpcjanssen 2019-12-07T15:08:39.394600Z

And TIL

erwinrooijakkers 2019-12-07T15:27:14.396900Z

Solution using core.async: https://github.com/transducer/adventofcode/blob/master/src/adventofcode/2019/day7.clj

erwinrooijakkers 2019-12-10T16:09:41.053700Z

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.

fellshard 2019-12-10T17:20:09.056400Z

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. :)

pesterhazy 2019-12-07T16:22:12.398200Z

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

pesterhazy 2019-12-07T16:23:13.399Z

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

mpcjanssen 2019-12-07T16:54:22.400600Z

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

2019-12-07T17:33:58.406200Z

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.

2019-12-07T17:34:26.406500Z

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?

2019-12-07T17:37:13.406700Z

Like this?

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

👏 2
2019-12-07T17:38:23.407Z

Super!

2019-12-07T17:39:16.407200Z

Thanks much 🙂

2019-12-07T17:40:09.407500Z

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

2019-12-07T17:53:00.409900Z

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! 🙏 <https://github.com/MeikeMertsch/AoC_2019/blob/master/src/christmas/day07.clj>

🙏 1
🙂 1
fellshard 2019-12-07T19:02:13.410500Z

I was wondering if someone would use channels :)

lilactown 2019-12-07T22:26:09.411500Z

I am still really struggling with day5

lilactown 2019-12-07T22:26:21.411800Z

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

lilactown 2019-12-07T22:26:50.412200Z

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

lilactown 2019-12-07T22:27:23.412900Z

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

lilactown 2019-12-07T22:43:22.413300Z

all of my diagnostic codes come back 0

uosl 2019-12-07T22:45:40.413900Z

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

lilactown 2019-12-07T22:48:22.414400Z

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

💡 1
lilactown 2019-12-07T22:49:00.414600Z

oh foo

lilactown 2019-12-07T22:49:20.414900Z

> read right-to-left from the opcode

2019-12-07T22:51:12.415800Z

@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!

💡 1
lilactown 2019-12-07T23:07:19.416900Z

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

lilactown 2019-12-07T23:07:25.417100Z

very frustrating