adventofcode 2021-12-10

Watch a script and run tests while developing with babashka. Should come in handy for Aoc :) https://github.com/babashka/babashka/discussions/1102

1

πŸ‘ 7
2
3
3
6

Very nice! I wonder if <> where valid bracket a clojure ide could solve it

Also could would be to see a regex solution

Hi, I just want to say that as a beginner in clojure, I've learnt so much from this thread and peoples answers. πŸ‘

5
❀️ 6
🌹 1

it has been breaking my brain a bit trying to decipher some of the solutions, but once I understand them I feel like I learned something new

If you like Clojure, and if you like Advent of Code, you might be interested in joining our remote Advent of Code meetup at Clojadelphia, next Thursday at 5pm EST. https://www.meetup.com/Clojadelphia/events/282565898/

looks like day3 part2 was a breaking point for lots of peeps

meaning number of devs gave up after that

i could honestly see that, i was frustrated (mainly with how it was worded/presented) with that one the most

I think a lot of people don't know that if you don't complete a day, you can still do the next

ha, actually I just found out that today

after clicking next day willy nilly

damn this slack is a mess

usability sucks

First time in my life I have written a double barreled recur statement -> https://github.com/samedhi/advent-of-code-2021/blob/main/18.clj (problem 9 second section)

I think I set a new land speed record for slowest possible code on Day 8 -> https://github.com/samedhi/advent-of-code-2021/blob/main/16.clj

🧡Day 10 answers thread: post your answers here

Day 10 Part 1

(def scores {\} 1197
           \) 3
           \] 57
           \> 25137})

(defn parser [line]
  (map identity line))

(defn find-syntax-error [opens [f & r]]
  (if (empty? r)
    nil
    (cond (#{\{ \( \[ \<} f)
          (recur (conj opens f) r)

          (#{\} \) \] \>} f)   ;
          (if (= f (matching (peek opens)))
            (recur (pop opens) r)
            f))))

(->> (f/read-all-lines-and-parse "puzzle-inputs/2021/day10" parser)
     (map (partial find-syntax-error []))
     (remove nil?)
     (map scores)
     (reduce +))

This day is confusing calvas syntax highlighting!

πŸ€ͺ 3

I like your substitutions! Need to introduce some in emacs.

I noticed i'm using remove a lot

switching to transients is pretty easy πŸ‘Œ

This worked well for me:

(defn parse [l]
 (loop [line l]
   (let [new (-> line
                 (str/replace #"\[\]" "")
                 (str/replace #"\{\}" "")
                 (str/replace #"\<\>" "")
                 (str/replace #"\(\)" ""))]
     (if (= (count new) (count line))
       line
       (recur new)))))

πŸ’― 1
πŸ†’ 4

Same stack based approach. I didn’t flag the results, just used the fact that if a coll is returned, it’s the unbalanced stack, if it’s a single char it’s the bad one. https://github.com/tschady/advent-of-code/blob/main/src/aoc/2021/d10.clj

I wonder if he threw the <, > in there just so you couldn’t use the clojure reader somehow

this is my regex approach

(defn fixed-point [f a]
  (let [r (f a)]
    (if (= r a)
      a
      (recur f r))))

(defn validate [line]
  (let [fp (fixed-point #(string/replace % #"\(\)|\[\]|\{\}|\<\>" "") line)
        invalid (re-find #"\)|\]|\}|\>" fp)]
    [invalid (map open->close (string/reverse fp))]))

Well, that was a bit embarrasing how long it took to find the bug in (find-middle-score)πŸ˜– Part 2

(def scores {\} 3
           \) 1
           \] 2
           \> 4})

(def matcher {\{ \}
            \[ \]
            \< \>
            \( \)})

(defn get-remaining [open [c & r]]
  (if (nil? c)
    open
    (cond (#{\{ \[ \< \(} c)
          (recur (conj open c) r)

          (#{\} \] \> \)} c)
          (if (= c (matcher (peek open)))
            (recur (pop open) r)
            nil))))

(defn score [auto-completes]
  (reduce (fn [acc i] 
            (+ (* acc 5) (scores i))) 0 auto-completes))

(defn auto-complete [line]
  (map matcher line))

(defn get-middle-score [scores]
  (let [sorted-scores (sort scores)]
       (nth sorted-scores (dec (Math/ceil (/ (count scores) 2.0))))))

(->> (f/read-all-lines-and-parse "puzzle-inputs/2021/day10" parser)
     (map (partial get-remaining []))
     (remove nil?)
     (map auto-complete)
     (map reverse)
     (map score)
     (get-middle-score))

I found very surprising that this works just fine (nth [1 2 3] (/ 3 2))

pretty cool!

Late to the party today.

https://github.com/kfirmanty/advent-of-code-2021 found day10 to be much easier than few last ones. Will check other people solution but I guess most used list as stack to keep track of opened parens

Tweaked mine a little, both solutions run in about 1ms in babashka

1
🐎 1

today's puzzle seemed relatively mundane, and so does my solution: https://github.com/euccastro/advent-of-code-2021/blob/main/day10.clj

part 1 (inspired by @peterc)

Can somebody compare the performance of it to stack based implementation?