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
markw 2020-12-18T02:08:53.382800Z

I thought day 17 looked reasonably easy at first… Now my back of the envelope guess for part 2 completion is 2.5 hours

markw 2020-12-18T02:10:42.383500Z

guess that leaves me some time to consider my botched approach … or just be lazy and let it finish before day 18 πŸ˜›

markw 2020-12-18T02:44:02.384300Z

only an hour it turns out…. yay?

2020-12-18T04:10:06.385600Z

While I have no clue what your solution looks like, maybe you can bring that down without rewriting your logic at all by using memoize (if you have any functions that will often have the same inputs throughout execution)

markw 2020-12-18T04:20:11.385800Z

yeah i was considering going in that direction, but i just scrapped version one and did a big refactor.. now it’s < 1 second for part 1 and about 10 for part 2, good enough for me

2020-12-18T04:28:52.386300Z

Nice!

2020-12-18T05:21:00.386700Z

Day 18 answers thread - Post your answers here

2020-12-18T08:29:42.394200Z

Here is my solution, based on postwalk as well, but without tricks for part 2 https://github.com/zelark/AoC-2020/blob/master/src/zelark/aoc_2020/day_18.clj

πŸ‘ 3
nbardiuk 2020-12-18T10:32:05.394700Z

I struggled with this one https://github.com/nbardiuk/adventofcode/blob/master/2020/src/day18.clj

πŸ‘ 3
Joe 2020-12-18T11:59:24.399300Z

https://github.com/RedPenguin101/aoc2020/blob/main/day18.clj no post walk, just good old recursive calls to evaluate. Feels very SICP'y

πŸ‘ 5
erwinrooijakkers 2020-12-18T12:46:58.400700Z

Lovely question. Defined two context-free grammars and used instaparse: https://github.com/transducer/adventofcode/blob/master/src/adventofcode/2020/day18.clj

πŸ‘ 3
πŸ‘ 1
😍 1
erwinrooijakkers 2020-12-18T15:30:29.403100Z

(insta/visualize (arithmetic-parser2 "1 + (2 * 3) + (4 * (5 + 6))"))

2
2020-12-18T16:18:49.403900Z

@erwinrooijakkers at the end of your program, you do a map inside your reduce

2020-12-18T16:20:01.404100Z

you may also do something like

(-&gt;&gt; input
  (map (comp-&gt; arithmetic-parser (partial insta/transform parse-tree-&gt;sexp)))
  (reduce +))

πŸ‘ 1
Average-user 2020-12-18T16:53:42.404300Z

(not mine) But I found this solution quite fun https://github.com/elektronaut/advent-of-code/blob/main/day18/day18.clj Don't know if this guy is here or not

πŸ‘ 3
genmeblog 2020-12-18T17:19:28.404900Z

I spent too much time on this today 😞 https://github.com/genmeblog/advent-of-code/blob/master/src/advent_of_code_2020/day18.clj

πŸ‘ 1
nbardiuk 2020-12-18T17:47:35.405700Z

I feel you pain, I also manually processed tokens and spend too much time debugging precedence and parenthesis

genmeblog 2020-12-18T18:13:55.405900Z

Yeah... The most frustrating moment was when all tests were passing but total result was wrong. After a while I've found that (9*((1+1))+1) gave different result than (9*(1+1)+1)....

benoit 2020-12-18T18:43:01.406100Z

Instaparse solution for me https://github.com/benfle/advent-of-code-2020/blob/main/day18.clj

πŸ‘ 5
R.A. Porter 2020-12-19T03:45:52.407Z

I also went the grammar + instaparse route, anticipating that part 2 might be harder or, finally, tomorrow would build upon today. Made for almost no clojure code, but here are my crappy grammars https://github.com/coyotesqrl/advent2020/blob/master/resources/day18-no-precedence.grammar https://github.com/coyotesqrl/advent2020/blob/master/resources/day18-plus-precedence.grammar

πŸŽ‰ 1
2020-12-19T08:02:38.422400Z

Should have gone with instaparse right away, I also got all the test data working but failed on the file. https://github.com/Dexterminator/advent-of-code-2020/blob/main/src/day18/core.clj

πŸ‘ 1
misha 2020-12-19T10:34:20.431100Z

(defn calc
  ;; outer
  ([priority form]
   (let [f (complement number?)
         x (reduce
             (fn [form ops]
               (if (f form)
                 (calc priority ops form)
                 (reduced form)))
             form
             priority)]
     (if (f x) (first x) x)))

  ;;inner
  ([priority OP form]
   (if (number? form)
     form
     (loop [todo  (rest form)
            done  [(calc priority (first form))]]
       (if (empty? todo)
         done
         (let [[op x &amp; todo] todo
               f (eval op)
               a (peek done)
               b (if (list? x) (calc priority x) x)]
           (if (OP op)
             (recur todo (conj (pop done) (f a b)))
             (recur todo (conj done op b)))))))))


(calc '[#{+} #{*}] '[(4 * (3 * 2 + 2) * (9 * 7 * 5 * 4 * 9) * (7 * 7 + 7 * 4 + 9)) + 6 * 4 + 8 + ((6 * 5) * 4 * (2 * 8 + 4 + 7 * 9 + 3) * 2 + 6) + 3])
(calc '[#{+} #{-}] '(10 + (6 * 5)))


(defn solve [input priority]
  (-&gt;&gt; input
    (str/split-lines)
    (map #(read-string (str "[" % "]")))
    (map #(calc priority %))
    (reduce +)))

(assert (= 15285807527593  (solve i '[#{+ *}])))
(assert (= 461295257566346 (solve i '[#{+} #{*}])))

πŸ‘ 1
2020-12-18T05:50:34.387800Z

https://github.com/green-coder/advent-of-code-2020/blob/master/src/aoc/day_18.clj

πŸ‘ 4
🀘 3
πŸŽ‰ 3
euccastro 2020-12-18T05:52:32.388400Z

nice one @vincent.cantin!

euccastro 2020-12-18T05:52:47.388600Z

mine is admittedly very hacky πŸ™‚

2020-12-18T05:58:08.388800Z

I am awaiting the solution from @zelark, hope to learn something new again.

2020-12-18T05:58:32.389Z

mine is tricky πŸ™‚ .. tricks are not always available.

euccastro 2020-12-18T06:00:27.389300Z

I kind of knew postwalk existed but I had never used it so I stayed in string land all along πŸ™‚

Average-user 2020-12-18T06:04:02.390900Z

That was boring :c

2020-12-18T06:04:41.391Z

Maybe it means that we improved.

Average-user 2020-12-18T06:05:35.391200Z

Don't think so. My solution is still pretty ugly, but that is because the problem is not very motivating. At least for me of course, it is a matter of taste.

2020-12-18T06:09:47.392100Z

@vincent.cantin today is your turn to teach us something πŸ™‚

☝️ 2
2020-12-18T06:10:05.392400Z

Not fair, I will ask a refund. πŸ˜‰

2020-12-18T06:10:24.392600Z

Nice catch to use postwalk, I even didn’t remember about it.

2020-12-18T06:12:21.393Z

Actually, I solved it in python, because it was faster for me. Now I’m going to have breakfast and go to work.

2
2020-12-18T06:17:42.393900Z

Small note about how to test the type of something that looks like a β€œlist”:

;; Only use list? when you are sure what you want to test is a list.
  (list? (map identity (list :a :b))) ; false
  (list? (cons :a (list :b))) ; false
  (list? `(:a :b)) ; false
  
  ;; sequential? is more generic.
  (sequential? (map identity (list :a :b))) ; true
  (sequential? (cons :a (list :b))) ; true
  (sequential? `(:a :b)) ; true
  
  ;; Note: Strings are seqable, but not sequential?
  (sequential? "foo")) ; false

πŸ™ 2
2
2020-12-18T11:59:58.399900Z

TIL clojure.lang.Symbols are also functions! I guess the purpose of it is to look up things inside a map or a set but I’m not sure how to use this effectively. Here is the source code: https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/Symbol.java#L127-L133

;; surprise!
('+ 1 2)
;; =&gt; 2

('+ {'+ 1})
;; =&gt; 1
('+ {'+ +})
;; =&gt; #function[clojure.core/+]
('+ {'* *})
;; =&gt; nil
('+ {'* *} :not-found)
;; =&gt; :not-found

πŸ‘ 3
2020-12-18T12:03:41.400100Z

Had to learn this the hard way because my day 18 part 1 kept evaluating to the very last number in any given equation. :picard-facepalm:

('+ ('+ 1 ('* 2 3)) ('* 4 ('+ 5 6)))
;; =&gt; 6

πŸ˜… 2
2020-12-18T13:45:24.402Z

I did not know that. Thank you for sharing.