I have cleaned up my solution significantly and added comments, if anyone is interested. after doing that I'm actually quite satisfied with it, despite the biggish function at the end
Day 21 answers thread - Post your answers here
@vincent.cantin Love your solution for part 1 π
So simple but it works like a charm
https://github.com/euccastro/advent-of-code-2020/blob/master/src/advent/day21.clj
(yeah, I finally woke up early-ish and couldn't resist giving it a try)
I was already looking into core.logic for the first part when my daughter woke up and the solution popped up in my mind while putting her to sleep again π
can't overemphasize the value of stepping away from the computer sometimes
I found my solution after running after the cat which kept interrupting me..
https://github.com/zelark/AoC-2020/blob/master/src/zelark/aoc_2020/day_21.clj
https://github.com/nbardiuk/adventofcode/blob/master/2020/src/day21.clj
@nbardiuk need a util function for pruning already, yeah.
https://github.com/akovantsev/adventofcode/blob/master/src/adventofcode/y2020/day21.cljc
One from a newcomer to both clojure and aoc: https://github.com/next-mad-hatter/adventofcode/blob/master/src/aoc_2020/day_21.clj Not in leaderboard or racing mode, there's definitely lots to learn from the solutions here for me π
Solution to Day 21: https://github.com/benfle/advent-of-code-2020/blob/main/day21.clj
This one was more reasonable than yesterday π
The only interesting bit is the use of the fixed-point method to identify the food containing each allergen.
@zelark I like your solution, and I think that my extension of group-by
could have been useful to you in the parse-input
function.
Nice one @zelark. A lot of work is done in the parsing function :)
I need to improve my parsing/regexp game.
> newcomer to both clojure and aoc @
[clojure.algo.generic.functor :refer [fmap]]
:tatatananana:@misha is fmap
something arcane or comes with some caveats?
iirc I stumbled upon it at the very beginning of my looking at clojure while searching for a predefined way to transform a map's keys -- it seemed to me like something which would be a natural part of the standard library
have not seen it used in 5 years, but it looks useful. I think it is not a part of a stdlib because of transducers, and you very rarely just "map f over things and that's it". More often you then filter/remove/etc. over it, and only then pick a container for a result. @max.deineko
re-pouring the map result into specific container after each step of a transformation is like anti-transducer, and it is useful only for a single step mapping
Late one: https://github.com/genmeblog/advent-of-code/blob/master/src/advent_of_code_2020/day21.clj
@vincent.cantin thank you. Yes, it could. π But I want to keep every solution as and independent one and relying only clojure stdlib and java interop sometimes. Maybe next year Iβll pick a different way.
Maybe next year it will be part of the stdlib (I am kidding, that's impossible)
I feel silly having matched the parens of the "(contains ...)" part after seeing @zelark just split with #"contains" and grab the words π
It's ok to be paranoid .. don't worry.
the ingredients are probably randomly generated, they may also be spelled "contains".
For the same reason, I won't use read-string but edn/read-string
one trick that seems to recurrently work in aoc challenges is to resolve constraints by sorting the entries by length at the beginning and assuming that the earlier ones will always be the first ones to resolve
I guess that would be more robust if you sorted on every iteration (but then it would be no more efficient or pretty than searching for the already resolved items explicitly)
separating the allergens into their own entries from the beginning is a nice touch, @zelark. I wouldn't have guessed that it would simplify things that much later on
i.e., as opposed to keeping allergen sets by line
@euccastro Iβm glad you learned something from my code, I think thatβs one reason why we all here. I always learn something new from otherβs solutions.
Last year I solved puzzles solely and wasnβt in this chat. I regret a bit about it.
yeah I only started solving the puzzles (after having binge watched @plexus's videos) so I would be better primed to understand others' solutions
I'm still mulling on (reduce into [])
π
so that would be like concat into a vector? but wouldn't frequencies
work the same had you concatenated them before by using mapcat
instead in (map (comp val first) foods)
?
similarly, @vincent.cantin, why are you coercing into a vector here: https://github.com/green-coder/advent-of-code-2020/blob/8b7814d5407a783e641b7b160e7672a12c4aa538/src/aoc/day_21.clj#L32 ?
(my own solution needs some cleanup, I'm just making sure I'm not missing something subtle)
> but wouldnβtΒ `frequencies`Β work the same had you concatenated them before by usingΒ `mapcat` Actually, yes. Thank you for pointing out!
yw! another thing I don't fully understand is why do you invert the result map in narrow
. it shouldn't matter since it's a 1:1 mapping. if you kept allergens as keys then you could use plain sort
instead of (sort-by val)
(then you'd need to (map second)
, but that's no worse than (map first)
I guess? but the main point to me is that it's a bit surprising that `narrow' inverts, on top of narrowing
It was originally built for day 16, over there it makes more sense than here.
Just updated, now it got even simpler https://github.com/zelark/AoC-2020/commit/0f3b898fd1fce7b558239939814c941547e37b72#diff-81c48a0532316c42f7ff22c58f13917b8129fae2266d787458f41270948f4234
The code on github is mainly what I wrote while running against the clock. The vec was not needed, but when I typed it I didn't know yet. I favor vectors over sequences because of the constant access time and fast subvec.
@euccastro thank you for the code review ^_^
Late to the party: https://github.com/transducer/adventofcode/blob/master/src/adventofcode/2020/day21.clj
For part 2, I just need to clean up the data a bit to figure out the answer manually π https://github.com/dawranliou/advent-of-code/blob/master/2020/src/dawranliou/advent_of_code_2020/day_21.clj
wow ! you are fast
I didnβt do the part 1 yet
Iβm sure I can clean up the logic more but it is what it isβ¦ Now time to go back to my day 20 lol.
I got my 2 stars ^_^ .Β After I sawΒ @dawran6Β finishing early, I decided to try again for another hour.
https://github.com/green-coder/advent-of-code-2020/blob/master/src/aoc/day_21.clj
Love this: https://github.com/green-coder/advent-of-code-2020/blob/master/src/aoc/day_21.clj#L62-L64
π
https://github.com/Charlynux/advent-of-code-2020/blob/master/day21/day21.clj
4 days to go !
Tried some Common Lisp for today, spent really long time debugging. The problem was that mapcan
was doing some sideffect and modifying my list, something that would have never happend with mapcat
, I still don't quite understand why, but I had to switch (mapcan #'f xs)
to (apply #'concatenate 'list (mapcar #'f xs))