Fork me on GitHub
#adventofcode
<
2020-12-08
>
Vincent Cantin05:12:29

good morning

🌅 3
Vincent Cantin05:12:11

Today, I hoped there was a part 3.

Ben List19:12:56

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

devn06:12:40

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

devn06:12:09

(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

misha07:12:44

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

devn21:12:55

@U051HUZLD will post as a gist, one sec

devn21:12:09

also, you can drag the sidebar and expand it

plexus06:12:09

Today's was a lot of fun!

7
💪 3
👍 1
alekszelark07:12:39

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

Stuart17:12:54

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.

fingertoe08:12:09

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

Stuart12:12:44

Havent looked at day 8 yet, is it int code computer all over again ? 😄

fingertoe14:12:23

Smells like it.. ;-)

Stuart11:12:40

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

Stuart11:12:33

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

nbardiuk11:12:54

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!

Stuart12:12:43

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

Stuart12:12:05

that makes sense now.

nbardiuk12:12:33

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

pez18:12:39

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
alekszelark18:12:24

May the Force be with you. Brute force.

lightsaber 4
luke 1
😬 1
pez23:12:21

Bruted it. I feel dirty now.

kenj06:12:12

bruted it with code I’m not too proud of 🤕

pez11:12:24

Gold fever is bad for your soul. 😃

andrea.crotti21:12:49

I probably wrote too much code

karol21:12:03

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 😄

andrea.crotti21:12:25

I also went for the brute force

andrea.crotti21:12:52

can't really think of another way tbf

karol21:12:58

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

andrea.crotti21:12:47

yeah maybe it's possible

andrea.crotti21:12:17

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)

andrea.crotti21:12:34

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

karol21:12:36

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

genmeblog22:12:14

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.

andrea.crotti22:12:07

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

andrea.crotti22:12:21

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

andrea.crotti22:12:21

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

benoit22:12:23

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

andrea.crotti22:12:57

Well with loom it would be doable

andrea.crotti22:12:08

The graph algorithm is done at least

karol21:12:17

*spoiler alert click on thread only after finishing the task 🙂

karol21:12:24

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.

karol21:12:43

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

plexus07:12:30

I think you can get some reuse by keeping the program state up until the point where you hit the changed instruction, and reusing that. Still, not sure if it would really be worth it.

karol12:12:55

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 😄

karol12:12:48

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

andrea.crotti13:12:18

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

andrea.crotti13:12:35

can't really spend too much time on smart solutions if I want to keep up to date 😄

karol21:12:35

saw the pinned msg, moved my link there

andrea.crotti21:12:52

ah yes my bad I did the same