adventofcode 2021-12-04

@thomas.turner has joined the channel

🧡 Day 4 answers thread: post your answers here

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

βž• 1

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

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.

Did anyone find it?

The implementation is also very small.

;; 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 ?

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?

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!

is this similar in principle to @c.oakley108 solution?

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.

Ah now I get it!

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

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

first pass, eager version. lazy coming next.

πŸ‘ 1

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

2

https://github.com/tschady/advent-of-code/blob/main/src/aoc/2021/d04.clj i couldn’t unsee @nbardiuk’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

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

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

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

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

@felipecortezfi 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: https://github.com/IamDrowsy/aoc-2021/blob/main/src/aoc2021/d04.clj

πŸ‘ 1

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

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

πŸ‘ 1

my solution is similar to yours I think

1

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.

ah nice, i love group-by

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

but yeah it irked me a bit to "filter" twice πŸ˜„

πŸ˜€ 1

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?

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 :(

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

πŸ‘» 1
1

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

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

πŸ‘ 1

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:

actually I did something similar, because I thought that in bingo when you win you can keep the board and do bingo again πŸ€¦β€β™‚οΈ

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: https://github.com/potetm/advent-of-code/blob/master/src/advent_2021/day_4.clj

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

I need to code faster so I have time to read all these enlightening solutions πŸ™‚

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

@corasaurus-hex 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)

πŸ“ 2
☝️ 3

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.

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

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

@antbbn 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

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

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

I did today just to do matrix/index-seq

So might be worth checking it out.

yeah i'll definitely keep it in mind!

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

Made a quick animation of the bingo game in quil

πŸ‘ 15
🀩 5
πŸŽ‰ 6

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

πŸ‘ 1
πŸ¦‘ 2

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

😱 2