Fork me on GitHub
#adventofcode
<
2021-12-04
>
alekszelark04:12:22

🧵 Day 4 answers thread: post your answers here

alekszelark07:12:30

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

1
nbardiuk07:12:19

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.

jacoelho10:12:12

Solved representing each card as a map, looking at other solutions here, seems overcomplicated https://github.com/jacoelho/advent-of-code-clj/blob/main/src/aoc/2021/day04.clj

tschady11:12:31

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 https://github.com/callum-oakley/advent-of-code/blob/main/src/aoc/2021/04.clj

nice 2
tschady12:12:02

https://github.com/tschady/advent-of-code/blob/main/src/aoc/2021/d04.clj 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

https://github.com/kfirmanty/advent-of-code-2021/blob/main/src/day4.clj - 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

Joe12:12:18

Loved this one! Pretty happy I was able to get my https://github.com/RedPenguin101/aoc2021/blob/main/clojure/src/aoc2021/day04.clj to less than 50 lines. I went down a bit of a wrong path after part 1 due to https://github.com/RedPenguin101/aoc2021/blob/main/day04.md, 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
borkdude12:12:31

Surprisingly my solution is faster in babashka than clojure:

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

2
Felipe14:12:44

is it cheating if you use throw to escape out of a nested loop? part 1: https://github.com/FelipeCortez/advent-of-code/blob/master/2021/04.clj

😅 1
borkdude14:12:19

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

😁 1
drowsy15:12:36

Well, it's not exactly concise nor fast, but I learned a lot about datascript while using it to solve day4: https://github.com/IamDrowsy/aoc-2021/blob/main/src/aoc2021/d04.clj

👏 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 https://coyotesqrl.github.io/advent-of-code/2021/#/src%2Fcoyotesqrl%2F2021%2Fday4.clj

potetm16:12:00

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

potetm16:12:16

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.

potetm16:12:26

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

genmeblog17:12:39

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. https://github.com/genmeblog/advent-of-code/blob/master/src/advent_of_code_2021/day04.clj

👍 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 😄

genmeblog17:12:05

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
Stuart18:12:52

Part 1 https://gist.github.com/stuartstein777/e16e3b8b4150f0ad9e9646183438c189 I store each board as a flat collection, e.g. https://gist.github.com/stuartstein777/e0af631b8b151fa2e401fa24189e4264 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 :(

euccastro18:12:44

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

wow 1
👻 1
Stuart19:12:03

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
Stuart19:12:40

Part 2 https://gist.github.com/stuartstein777/74765018745c8a500dbf4b2ad9405451 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:

Stuart19:12:58

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.

Stuart20:12:08

Probably changes per country though

potetm20:12:23

Okay, it looks much better when you don’t track each board’s state individually and just track the called numbers: https://github.com/potetm/advent-of-code/blob/master/src/advent_2021/day_4.clj

Antonio Bibiano20:12:43

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

sebastian22:12:45

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 🙂

mchampine06:12:01

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

Stuart14:12:32

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.

Stuart14:12:17

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.

Stuart14:12:16

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

tschady19:12:51

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
Sidestep13:12:01

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

💜 1
alekszelark05:12:49

Hello Saturday!

borkdude10:12:46

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

borkdude10:12:07

Now it does

Benjamin18:12:47

day 3 tip request

Benjamin18:12:12

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

genmeblog18:12:41

(apply map vector rows)

☝️ 3
📝 2
genmeblog18:12:52

I keep data column-wise

genmeblog18:12:16

(for the first part)

Benjamin18:12:39

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

genmeblog18:12:51

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?

borkdude19:12:06

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

borkdude19:12:21

but it yields more verbose code

borkdude19:12:32

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

potetm19:12:56

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

borkdude19:12:39

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

potetm19:12:11

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

potetm19:12:16

at some point

potetm19:12:33

I did today just to do matrix/index-seq

potetm19:12:54

So might be worth checking it out.

Antonio Bibiano19:12:44

yeah i'll definitely keep it in mind!

borkdude21:12:44

apparently day 4 is a good day for #babashka performance ;) https://twitter.com/mknoszlig/status/1467240830301843458

nbardiuk21:12:23

Made a quick animation of the bingo game in quil

👍 15
🎉 6
5
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