Fork me on GitHub
#adventofcode
<
2021-12-10
>
samedhi04:12:57

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

alekszelark04:12:24

🧵Day 10 answers thread: post your answers here

Stuart09:12:42

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

Stuart09:12:24

This day is confusing calvas syntax highlighting!

🤪 3
genmeblog09:12:47

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

Antonio Bibiano10:12:27

I noticed i'm using remove a lot

cyppan11:12:14

switching to transients is pretty easy 👌

peterc13:12:53

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

🆒 4
💯 1
tschady13:12:20

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

tschady14:12:33

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

Antonio Bibiano14:12:18

this is my regex approach

Antonio Bibiano14:12:21

(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))]))

Stuart17:12:34

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

Antonio Bibiano18:12:18

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

mchampine19:12:54

Late to the party today.

karol19:12:09

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

borkdude22:12:31

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

🐎 1
babashka 1
euccastro23:12:03

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

Sidestep20:12:50

part 1 (inspired by @U1NLKFVC4)

Sidestep20:12:35

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

borkdude08:12:04

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

babashka 1
nbardiuk10:12:24

👍 7
cparen 3
oparen 3
parens 6
clj-kondo 2
Antonio Bibiano11:12:01

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

Antonio Bibiano11:12:31

Also could would be to see a regex solution

zxspectrr12:12:07

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

❤️ 6
🌹 1
clj 5
zxspectrr12:12:05

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

Nick McAvoy16:12:24

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/

Sidestep17:12:02

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

Sidestep17:12:46

meaning number of devs gave up after that

Darin Douglass17:12:32

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

Stuart17:12:11

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

Sidestep17:12:56

ha, actually I just found out that today

Sidestep17:12:10

after clicking next day willy nilly

Sidestep17:12:24

damn this slack is a mess

Sidestep17:12:32

usability sucks

samedhi23:12:53

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)