Fork me on GitHub

🧵 Day 4 answers thread: post your answers here


@U076FM90B yeah, lazy-seq is a good candidate for today’s puzzle


I've solved the problem with reduce, the lazy-seq was an optimization after refactoring. I like the idea of lazy-seq but not familiar enough to just use it on the spot

R.A. Porter08:12:35

Solved...but I can't bring myself to post in the current state. Maybe I'll clean up tomorrow.


Solved representing each card as a map, looking at other solutions here, seems overcomplicated


first pass, eager version. lazy coming next.

👏 1
Callum Oakley11:12:09

Doesn’t “mark” the boards in any way, just compares against the set of called numbers. Plays every board to completion, min-key for part-1 and max-key for part-2. ~50ms

nice 2
tschady12:12:02 i couldn’t unsee @U076FM90B’s. group-by bingo? is 🔥 here’s my lazy eval version. i like the helpers, but any advice on fixing that last cons line?

👍 5
karol12:12:20 - so here is my first very naive pass using loop but still it executes in less than 100ms so can't complain. will try loopless approach later


Loved this one! Pretty happy I was able to get my to less than 50 lines. I went down a bit of a wrong path after part 1 due to, and I had to write some reduce functions, but happily part 2 forced to me reevaluate what the issue was and get back to a cleaner declarative solution.

👍 1

Surprisingly my solution is faster in babashka than clojure:

$ bb aoc21_04.clj
"Elapsed time: 122.147231 msecs"
"Elapsed time: 451.069084 msecs"
$ clojure -M aoc21_04.clj
"Elapsed time: 180.087077 msecs"
"Elapsed time: 521.740564 msecs"


is it cheating if you use throw to escape out of a nested loop? part 1:

😅 1

@UA2U3KW0L I would say everything is allowed in AOC, even the most insane hacks. Impressive!

😁 1

Well, it's not exactly concise nor fast, but I learned a lot about datascript while using it to solve day4:

👏 1
R.A. Porter15:12:40

I'm still not content, but it's not as terrible as it could be? I have three functions that should be collapsible to one, but I was too tired to see it. Not sure why this one was such a bear for me. Trying a link to a statically-generated Clerk page


IMO the easiest way to get laziness out of this one is to iterate


I think I have an aversion to repeatedly mapping over the same data, no matter the cost 😄 Like in this one, I tracked board state by index instead of mapping over boards with a set of called numbers.


tbh I don’t even know if its actually faster.


My idea is to pack rows/cols into sets, disjoin every input and looking for empty set in each board. Haven't checked other solution yet.

👍 1
Antonio Bibiano17:12:11

my solution is similar to yours I think

bananadance 1
Antonio Bibiano17:12:24

I called the "boards" "cards" because in my native language we call them like that 😄


It is indeed! I avoided filtering twice by using group-by and used disj instead of difference.

Antonio Bibiano17:12:41

ah nice, i love group-by

Antonio Bibiano17:12:19

just figured since my predicate didn't return true/false the output would be a bit awkward touse

Antonio Bibiano17:12:29

but yeah it irked me a bit to "filter" twice 😄

😀 1

Part 1 I store each board as a flat collection, e.g. Is their a nicer way to do my mark-number-called without the nested maps?

Antonio Bibiano18:12:47

I also had a bunch of nested mapping, the only thing I could come up with is to create an helper function that did the inner map :(


@U013YN3T4DA see @U1Z392WMQ's solutions for a nice use of specter to avoid the nested maps

wow 1
👻 1

For part 2, are people finding a situation where a number is eliminating multiple boards?

Antonio Bibiano19:12:10

yes for me it happens, i have multuple winners in a single draw

👍 1

Part 2 I removed the boards as they become winners. I had an issue where on each number I was only looking for A winning board, so that lost a lot of time. It was fine once I realised what was happening. I found it by printing the boards at each number and by the end all of my boards were totally filled in:laughing:

Antonio Bibiano19:12:04

actually I did something similar, because I thought that in bingo when you win you can keep the board and do bingo again :man-facepalming:


I think you can, I think you can call bingo up to 3 times. Atleast from my memory of bingo, you can get 1 horizontal line, 2 horizontal lines or 3 horizontal lines.


Probably changes per country though


Okay, it looks much better when you don’t track each board’s state individually and just track the called numbers:

Antonio Bibiano20:12:43

i guess that's how it's played in the north pole


this one I'm really happy with (being a beginner in Clojure)

👍 1
Sam Adams00:12:13

I need to code faster so I have time to read all these enlightening solutions 🙂


Not happy with this one, lots of missed optimizations..

Vincent Cantin14:12:30

I think that there was a way to solve this Bingo problem in linear time, O(n+m) where n is the count of numbers and m is the number of grids.

Vincent Cantin14:12:08

Did anyone find it?

Vincent Cantin14:12:04

The implementation is also very small.

Vincent Cantin14:12:11

;; Fast solution
(def number->index
  (into {} (map-indexed (fn [i n] [n i])) numbers))

;; grid is a vector of sets representing the numbers in the rows and the cols.
(defn winning-index [grid]
  (->> grid
       (mapv (fn [line]
               (transduce (map number->index) max line)))
       (apply min)))


I dont understand, is that the entire solution for part 1 ?

Vincent Cantin14:12:55

That's the main part which describe the algorithm, not the whole solution.


oh, is that working out if a grid is won or not?

Vincent Cantin14:12:56

Once you calculate the winning index of each grid (which is fast), you can take the first one which wins or the last one, for part 1 and part 2.


ohhh, i see! Oh that's clever!

Antonio Bibiano16:12:11

is this similar in principle to @U01HL2S0X71 solution?

Vincent Cantin17:12:59

It's different, his solution has to test a grid multiple times. In my version, each grid is visited only once. My algorithm should be faster.

Antonio Bibiano17:12:33

Ah now I get it!

Callum Oakley18:12:52

oooh very nice


perhaps a bit of a bug, in that if the set of called numbers don’t complete a card ever, yours will still call it completed.

👍 1

@U02N27RK69K probably clearest implementation. I bet it performs well too because it's well designed.

💜 1

Hello Saturday!


My day 2 works on the sample input, but not on the real data :(


Now it does


day 3 tip request


what is a data structure let's me easily express vertical and horizontal series? I'm guessing just a vector of vectors? Do you have a tip for flipping it on it's side


(apply map vector rows)

☝️ 3
📝 2

I keep data column-wise


(for the first part)


man I need to start using multi arity map (refering to apply map vector


Yeah, it's really helpful.

Antonio Bibiano19:12:42

Yeah I think that trick is gonna be used in every challenge

Antonio Bibiano19:12:04

I was wondering about it’s performance though, let’s say you have a lot of rows, there was a discussion about apply having bad performance on large lists of argumens. Is there an alternative?


if you know the size of the matrix, you could optimize it using that knowledge and not use apply


but it yields more verbose code


and I don't think it really matters for this AOC puzzle


@U01HY37QQUA I’ve never seen it be a particular problem.


in SCI apply was a problem, getting rid of that vastly improved the speed of function calls (those were always done with apply in the beginning) but this is a very non-typical use case

Antonio Bibiano19:12:57

nice, because I was going through the posted solution and discovered core.matrix but I guess just for transposing or getting the columns it might be overkill


@U01HY37QQUA I almost always end up using core.matrix during AoC


at some point


I did today just to do matrix/index-seq


So might be worth checking it out.

Antonio Bibiano19:12:44

yeah i'll definitely keep it in mind!


apparently day 4 is a good day for #babashka performance ;)


Made a quick animation of the bingo game in quil

👍 15
🎉 6
Vincent Cantin22:12:50

I just realized that we were all playing the "Squid's Game" today.

🦑 2
👏 1
Vincent Cantin22:12:17

It makes sense, as Bingo was a game we were mostly playing a long time ago, during childhood.

😱 2