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
plexus 2020-12-08T05:12:17.378Z

it is lazy, which is why you're getting the stack overflow. consider this

(def x (reduce concat (map #(list %) (range 100000))))

(first x)
the def will work fine, but as soon as you try to realize the first element it throws. At that point it tries to realize the outer concat, which first needs to realize the concat inside of it, and so forth until it bottoms out. Realizing a lazy-seq concumes a stack frame. It's really just a wrapper around an IFn which implements the Seq interface.

plexus 2020-12-08T05:12:39.378200Z

I'll talk about this on the stream today, it's one of those surprising clojure gotchas.

πŸ‘ 1
2020-12-08T05:34:29.378500Z

good morning

πŸŒ… 3
2020-12-08T05:37:11.379700Z

Today, I hoped there was a part 3.

Ben List 2020-12-08T19:27:56.404800Z

If its anything like last year there will be some future problems building off this one

devn 2020-12-08T06:08:40.381100Z

im still banging my head on yesterday’s. trying to do a clara.rules version. I figured out Part I, but Part II is getting me

devn 2020-12-08T21:10:55.407800Z

@misha will post as a gist, one sec

devn 2020-12-08T21:11:09.408300Z

also, you can drag the sidebar and expand it

devn 2020-12-08T06:09:09.381200Z

(defrecord ContainerBag [type contained-bags])

(defrecord IntermediateBag [parent-type type])

(defrecord GoldBag [parent-type])

(defrule produce-intermediate-bags
  [ContainerBag
   (= ?type type)
   (= ?contained-bags contained-bags)
   (seq contained-bags)]
  =>
  (when (seq ?contained-bags)
    (insert-all!
     (mapv (fn [[kind num]]
             (if (= kind "shiny gold")
               (map->GoldBag {:parent-type ?type})
               (map->IntermediateBag {:parent-type ?type
                                      :type kind})))
           ?contained-bags))))

(defrule indirect-gold-bags
  [ContainerBag
   (= ?type type)]
  [IntermediateBag
   (= ?type parent-type)
   (= ?intermediate-type type)]
  [GoldBag
   (= ?intermediate-type parent-type)]
  =>
  (insert! (map->GoldBag {:parent-type ?type})))

(defquery gold-bags []
  [?gold-bags <- (acc/distinct :parent-type) :from [GoldBag]])

(defquery intermediate-bags []
  [?intermediate-bags <- IntermediateBag])

(defn run-rules []
  (-> (mk-session :cache false)
      (insert-all (doall (for [[k vs] (parse input)]
                           (map->ContainerBag {:type k
                                               :contained-bags vs}))))
      (fire-rules)))

;; Part I
(-> (run-rules)
    (query gold-bags)
    first
    :?gold-bags
    count)
;; => 179

plexus 2020-12-08T06:45:09.382800Z

Today's was a lot of fun!

πŸ‘ 1
βž• 7
πŸ’ͺ 3
2020-12-08T17:07:54.400600Z

A few months ago I wrote an toy asm interpreter as one of my first learn clojure projects. Be nice if I can reuse bits of this for aoc.

2020-12-08T07:01:39.384900Z

Hope they will continue this series as it was last year.

misha 2020-12-08T07:30:44.385700Z

unfortunately, this is unreadable in slack thread even on 15" screen due to line wrapping :(

fingertoe 2020-12-08T08:23:09.387800Z

Bombed out on 7…. Caught up on 8.. I am guessing this is one worth building really good tests around since they will probably have us refactor it a bunch??

2020-12-08T11:31:40.393300Z

For day 7, part 2. What does this mean "becomes topologically impractical"?

2020-12-08T11:32:33.394100Z

I solved part1 as a graph, then reverseed the graph and counted from shiny gold to all the unique nodes i can hit. But I dont know what it means that i have to take into account topologically impractical

nbardiuk 2020-12-08T11:41:54.394200Z

the statement means to go as deep as data, even if in practice nobody would be able nest so many bags in each other

be sure to count all of the bags, even if the nesting becomes topologically impractical!

2020-12-08T12:21:43.395600Z

thank you, so it's like a joke rather than soemthing technical I have to consider?

2020-12-08T12:22:05.395800Z

that makes sense now.

nbardiuk 2020-12-08T12:34:33.396Z

Yes, there are usually only several statements relevant and the rest is for Christmas atmosphere and laughs

2020-12-08T12:35:44.396500Z

Havent looked at day 8 yet, is it int code computer all over again ? πŸ˜„

fingertoe 2020-12-08T14:21:23.397500Z

Smells like it.. ;-)

pez 2020-12-08T18:03:39.403500Z

Part 2 of day 8. I hate it a bit. But now dinner and reading Stravaganza with my kids. Hopefully that will cleanse my thought patterns.

πŸ’ͺ 1
pez 2020-12-09T11:32:24.448400Z

Gold fever is bad for your soul. πŸ˜ƒ

2020-12-08T18:09:24.403700Z

May the Force be with you. Brute force.

😬 1
4
1
2020-12-08T21:11:49.409900Z

I probably wrote too much code

2020-12-08T21:12:03.410900Z

Hello everybody and welcome to day8 πŸ˜„ I finished todays task rather fast but I am left wondering if there is more clever solution to second part than just generating all permutations and executing them so this will probably occupy my mind for a little bit. It still completed in less than 70 milliseconds so Β―\(ツ)/Β― but I felt a little like cheating by bruteing it πŸ˜„

2020-12-08T21:12:25.411100Z

I also went for the brute force

2020-12-08T21:12:52.411400Z

can't really think of another way tbf

2020-12-08T21:15:17.412300Z

*spoiler alert click on thread only after finishing the task πŸ™‚

2020-12-09T12:36:55.451Z

that is what Andrea suggested in other thread and I think it would definitely help in "feel good by not brute forcing it 100%" and I like the idea πŸ˜„

2020-12-09T12:37:48.451200Z

but I was thinking more in the vein that because the every jump is not conditional you could use that as an advantage

2020-12-09T13:26:18.452200Z

hehe well the problem is that that there is a new problem every day

2020-12-09T13:26:35.452400Z

can't really spend too much time on smart solutions if I want to keep up to date πŸ˜„

2020-12-08T21:15:24.412400Z

My gut feeling is that there might be some possible way to do simple heuristics over the code and analyze the bits that will surely loop as the loops are not conditional so they are bound to be executed ever time.

2020-12-08T21:16:43.412600Z

but to be honest this will probably take much more code and in the end will execute slower than basic permutation generation ;d

2020-12-08T21:19:58.412800Z

so as I mentioned my gut feeling is that because jumps are not conditional you could write code that would analyze the source and identify the jumps that would 100% end in infinite loop

2020-12-08T21:20:47.413Z

yeah maybe it's possible

2020-12-08T21:21:17.413200Z

another thing is that you probably don't need to re-run the whole simulation every time with the changed instructions set (which is what I did at least)

2020-12-08T21:21:34.413400Z

you would only need to branch out every time there is a nop or a jmp

2020-12-08T21:22:02.413600Z

yeah probably

2020-12-08T21:23:35.414900Z

saw the pinned msg, moved my link there

2020-12-08T21:25:36.415200Z

good idea, you could explore the "code space" like tree

2020-12-08T21:30:32.415700Z

moving my solution here https://github.com/AndreaCrotti/advent2020/blob/master/src/problems.clj#L257

2020-12-08T21:30:52.416200Z

ah yes my bad I did the same

genmeblog 2020-12-08T22:11:14.417800Z

actually you don't need to generate all permutations upfront, you can generate lazy list of permutations and stop when you find first one which terminates properly.

2020-12-08T22:19:07.418Z

well I generated all the possible replacements https://github.com/AndreaCrotti/advent2020/blob/master/src/problems.clj#L294 which I guess would not be that expensive anyway

2020-12-08T22:19:21.418300Z

it's running the simulation that in theory is the expensive thing

2020-12-08T22:20:21.418500Z

but actually in my solution I'm calling terminates? on all the permutations tbf πŸ˜„ so yeah could definitively improve that

benoit 2020-12-08T22:23:23.418900Z

You could represent the program as a directed graph and run an algorithm to find the minimal cycle-breaking set. But that doesn't seem easy after quick googling :)

2020-12-08T22:29:57.419200Z

Well with loom it would be doable

2020-12-08T22:30:08.419400Z

The graph algorithm is done at least

pez 2020-12-08T23:02:21.420500Z

Bruted it. I feel dirty now.