This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-12-04
Channels
- # admin-announcements (1)
- # adventofcode (98)
- # announcements (5)
- # asami (3)
- # babashka (24)
- # beginners (51)
- # bitcoin (3)
- # calva (24)
- # clj-kondo (73)
- # cljdoc (5)
- # cljs-dev (2)
- # clojure (15)
- # clojure-czech (3)
- # clojure-dev (27)
- # clojure-europe (7)
- # clojure-gamedev (1)
- # clojure-italy (2)
- # clojure-uk (1)
- # conjure (4)
- # cursive (18)
- # datahike (4)
- # datomic (3)
- # deps-new (7)
- # emacs (1)
- # events (10)
- # fulcro (5)
- # honeysql (4)
- # jackdaw (2)
- # java (13)
- # lsp (85)
- # meander (9)
- # membrane (1)
- # minecraft (1)
- # off-topic (45)
- # re-frame (16)
- # sql (17)
- # tools-deps (10)
- # vscode (9)
- # xtdb (8)
🧵 Day 4 answers thread: post your answers here
could be optimized of course https://github.com/zelark/AoC-2021/blob/main/src/zelark/aoc_2021/day_04.clj
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
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
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
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?
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
My day 4: https://gist.github.com/borkdude/b5cf0e9d2d8ab7c678d88e27a3357b33#file-aoc21_04-clj
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.
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"
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
@UA2U3KW0L I would say everything is allowed in AOC, even the most insane hacks. Impressive!
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
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
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.
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
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
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 :(
today I was in the "many little helpers" mood: https://github.com/euccastro/advent-of-code-2021/blob/main/day4.clj
@U013YN3T4DA see @U1Z392WMQ's solutions for a nice use of specter to avoid the nested maps
For part 2, are people finding a situation where a number is eliminating multiple boards?
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 :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.
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
My solution, similar to @U4LN72X44 's https://github.com/green-coder/advent-of-code/blob/master/src/aoc_2021/day_4.clj
My spec-sploration solution: https://samadams.dev/2021/12/04/advent-of-code-day-4.html
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)))
That's the main part which describe the algorithm, not the whole solution.
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.
I wrote the full implementation at https://github.com/green-coder/advent-of-code/blob/master/src/aoc_2021/day_4_fast.clj
is this similar in principle to @U01HL2S0X71 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.
https://gist.github.com/corasaurus-hex/ee69c4bdee71b55a1ebbcf588d82c527#file-4-1-clj
@U02N27RK69K probably clearest implementation. I bet it performs well too because it's well designed.
Hello Saturday!
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
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
@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
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
yeah i'll definitely keep it in mind!
apparently day 4 is a good day for #babashka performance ;) https://twitter.com/mknoszlig/status/1467240830301843458
It makes sense, as Bingo was a game we were mostly playing a long time ago, during childhood.