adventofcode

Thomas Turner 2021-12-04T03:47:12.200500Z

@thomas.turner has joined the channel

Aleks 2021-12-04T04:57:22.202900Z

🧡 Day 4 answers thread: post your answers here

Aleks 2021-12-04T06:40:59.204Z

could be optimized of course https://github.com/zelark/AoC-2021/blob/main/src/zelark/aoc_2021/day_04.clj

πŸ‘ 4
Aleks 2021-12-04T07:51:30.204900Z

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

βž• 1
nbardiuk 2021-12-04T07:58:19.205100Z

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

babardo 2021-12-05T10:39:44.257200Z

Cathing up πŸ˜… https://github.com/v-garcia/aoc_2021/blob/main/day_four.clj

2021-12-05T14:15:30.264Z

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.

2021-12-05T14:16:08.264200Z

Did anyone find it?

2021-12-05T14:29:04.265400Z

The implementation is also very small.

2021-12-05T14:31:11.265700Z

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

2021-12-05T14:32:32.265900Z

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

2021-12-05T14:32:55.266100Z

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

2021-12-05T14:33:17.266300Z

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

2021-12-05T14:33:56.266500Z

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.

2021-12-05T14:34:16.266700Z

ohhh, i see! Oh that's clever!

2021-12-05T15:13:29.267900Z

I wrote the full implementation at https://github.com/green-coder/advent-of-code/blob/master/src/aoc_2021/day_4_fast.clj

πŸ‘ 1
πŸ‘Œ 1
Antonio Bibiano 2021-12-05T16:36:11.273200Z

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

2021-12-05T17:23:59.280700Z

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 Bibiano 2021-12-05T17:39:33.286700Z

Ah now I get it!

Callum Oakley 2021-12-05T18:23:52.288900Z

oooh very nice

tschady 2021-12-05T19:38:51.294900Z

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
R.A. Porter 2021-12-04T08:32:35.205400Z

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

jacoelho 2021-12-04T10:10:12.205900Z

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

tschady 2021-12-04T11:25:31.208Z

first pass, eager version. lazy coming next.

πŸ‘ 1
Callum Oakley 2021-12-04T11:52:09.208700Z

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
tschady 2021-12-04T12:05:02.209300Z

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
2021-12-04T12:10:20.209700Z

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

Joe 2021-12-04T12:46:18.210900Z

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
borkdude 2021-12-04T12:50:31.211200Z

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
Felipe 2021-12-04T14:54:44.213300Z

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
borkdude 2021-12-04T14:58:19.213600Z

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

😁 1
2021-12-04T15:18:36.213800Z

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. Porter 2021-12-04T15:30:40.214100Z

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

2021-12-04T16:01:00.215300Z

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

2021-12-04T16:11:16.215500Z

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.

2021-12-04T16:13:26.215700Z

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

genmeblog 2021-12-04T17:12:39.216300Z

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 Bibiano 2021-12-04T17:37:11.216700Z

my solution is similar to yours I think

1
Antonio Bibiano 2021-12-04T17:37:31.216900Z

Antonio Bibiano 2021-12-04T17:39:24.217400Z

I called the "boards" "cards" because in my native language we call them like that πŸ˜„

genmeblog 2021-12-04T17:40:05.217600Z

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

Antonio Bibiano 2021-12-04T17:47:41.218Z

ah nice, i love group-by

Antonio Bibiano 2021-12-04T17:49:19.218200Z

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

Antonio Bibiano 2021-12-04T17:49:29.218400Z

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

πŸ˜€ 1
2021-12-04T18:00:52.218600Z

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 Bibiano 2021-12-04T18:28:47.221500Z

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

euccastro 2021-12-04T18:29:55.221700Z

today I was in the "many little helpers" mood: https://github.com/euccastro/advent-of-code-2021/blob/main/day4.clj

euccastro 2021-12-04T18:49:44.224500Z

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

πŸ‘» 1
1
2021-12-04T19:00:03.226Z

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

Antonio Bibiano 2021-12-04T19:12:10.227600Z

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

πŸ‘ 1
2021-12-04T19:55:40.234900Z

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 Bibiano 2021-12-04T19:59:04.235100Z

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

2021-12-04T19:59:58.235300Z

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.

2021-12-04T20:00:08.235500Z

Probably changes per country though

2021-12-04T20:06:23.235700Z

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 Bibiano 2021-12-04T20:06:43.236100Z

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

sebastian 2021-12-04T22:36:45.243700Z

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

πŸ‘ 1
Sam Adams 2021-12-05T00:01:06.246800Z

My spec-sploration solution: https://samadams.dev/2021/12/04/advent-of-code-day-4.html

Sam Adams 2021-12-05T00:02:13.247100Z

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

mchampine 2021-12-05T06:36:01.249400Z

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

Sidestep 2021-12-09T13:36:01.475300Z

@corasaurus-hex probably clearest implementation. I bet it performs well too because it's well designed.

πŸ’œ 1
Aleks 2021-12-04T05:58:49.203600Z

Hello Saturday!

borkdude 2021-12-04T10:29:46.206600Z

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

borkdude 2021-12-04T10:46:07.206800Z

Now it does

roelof 2021-12-04T11:01:27.207600Z

@borkdude congrats

Benjamin 2021-12-04T18:45:47.223100Z

day 3 tip request

Benjamin 2021-12-04T18:47:12.223500Z

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

genmeblog 2021-12-04T18:48:41.223900Z

(apply map vector rows)

πŸ“ 2
☝️ 3
genmeblog 2021-12-04T18:48:52.224100Z

I keep data column-wise

Benjamin 2021-12-04T18:49:11.224300Z

πŸ‘€

genmeblog 2021-12-04T18:50:16.224700Z

(for the first part)

Benjamin 2021-12-04T18:51:39.225Z

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

genmeblog 2021-12-04T18:52:51.225400Z

Yeah, it's really helpful.

Antonio Bibiano 2021-12-04T19:25:42.228500Z

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

Antonio Bibiano 2021-12-04T19:28:04.231300Z

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?

borkdude 2021-12-04T19:43:06.231900Z

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

borkdude 2021-12-04T19:43:21.232200Z

but it yields more verbose code

borkdude 2021-12-04T19:43:32.232400Z

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

2021-12-04T19:44:56.232700Z

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

2021-12-04T19:45:00.232900Z

speedwise

borkdude 2021-12-04T19:45:39.233100Z

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 Bibiano 2021-12-04T19:45:57.233300Z

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

2021-12-04T19:51:11.233600Z

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

2021-12-04T19:51:16.233800Z

at some point

2021-12-04T19:51:33.234200Z

I did today just to do matrix/index-seq

2021-12-04T19:51:54.234500Z

So might be worth checking it out.

Antonio Bibiano 2021-12-04T19:53:44.234700Z

yeah i'll definitely keep it in mind!

borkdude 2021-12-04T21:30:44.238500Z

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

nbardiuk 2021-12-04T21:55:23.240900Z

Made a quick animation of the bingo game in quil

πŸ‘ 15
🀩 5
πŸŽ‰ 6
borkdude 2021-12-04T21:58:40.241300Z

nice!

2021-12-04T22:03:50.242100Z

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

πŸ‘ 1
πŸ¦‘ 2
2021-12-04T22:06:17.242200Z

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

😱 2