adventofcode

Happy Advent 2020! Please put answers in the pinned threads or create one if it does not exist yet. | https://github.com/adventofcode-clojurians/adventofcode-clojurians | Join the private leaderboard with code 217019-4a55b8eb
euccastro 2020-12-21T00:46:15.012900Z

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

1πŸ‘
2020-12-21T05:30:10.013700Z

Day 21 answers thread - Post your answers here

peterc 2020-12-21T08:03:11.021200Z

@vincent.cantin Love your solution for part 1 πŸ˜„

peterc 2020-12-21T08:03:25.021400Z

So simple but it works like a charm

1😊
euccastro 2020-12-21T08:16:00.022200Z

(yeah, I finally woke up early-ish and couldn't resist giving it a try)

euccastro 2020-12-21T08:18:20.022400Z

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 πŸ˜›

euccastro 2020-12-21T08:19:14.022700Z

can't overemphasize the value of stepping away from the computer sometimes

4βž•
2020-12-21T08:19:41.022900Z

I found my solution after running after the cat which kept interrupting me..

1🐱
misha 2020-12-21T11:15:42.026100Z

@nbardiuk need a util function for pruning already, yeah.

1πŸ’―
2020-12-21T12:35:34.027400Z

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 πŸ™‚

3πŸ‘
benoit 2020-12-21T12:40:16.027700Z

Solution to Day 21: https://github.com/benfle/advent-of-code-2020/blob/main/day21.clj

2πŸ‘
benoit 2020-12-21T12:40:23.028Z

This one was more reasonable than yesterday πŸ™‚

2βž•
benoit 2020-12-21T12:41:27.028300Z

The only interesting bit is the use of the fixed-point method to identify the food containing each allergen.

2020-12-21T12:43:17.028500Z

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

1πŸ™
benoit 2020-12-21T12:59:41.029400Z

Nice one @zelark. A lot of work is done in the parsing function :)

1πŸ™
benoit 2020-12-21T13:00:05.029600Z

I need to improve my parsing/regexp game.

misha 2020-12-21T13:10:36.030200Z

> newcomer to both clojure and aoc @

[clojure.algo.generic.functor :refer [fmap]]
:tatatananana:

2020-12-21T13:26:37.030800Z

@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

misha 2020-12-21T13:37:51.031Z

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

1πŸ“Ž
misha 2020-12-21T13:40:00.031200Z

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

1πŸ’‘
2020-12-21T14:06:41.031700Z

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

2020-12-21T15:47:20.032500Z

Maybe next year it will be part of the stdlib (I am kidding, that's impossible)

1πŸ˜ƒ
euccastro 2020-12-21T17:04:27.032800Z

I feel silly having matched the parens of the "(contains ...)" part after seeing @zelark just split with #"contains" and grab the words πŸ™‚

1βž•
2020-12-21T17:05:46.033100Z

It's ok to be paranoid .. don't worry.

2020-12-21T17:06:20.033300Z

the ingredients are probably randomly generated, they may also be spelled "contains".

2020-12-21T17:07:00.033500Z

For the same reason, I won't use read-string but edn/read-string

euccastro 2020-12-21T17:23:20.033900Z

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

euccastro 2020-12-21T17:24:30.034100Z

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)

euccastro 2020-12-21T17:28:47.034400Z

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

euccastro 2020-12-21T17:29:22.034600Z

i.e., as opposed to keeping allergen sets by line

2020-12-21T17:31:16.034800Z

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

1βž•
2020-12-21T17:32:46.035Z

Last year I solved puzzles solely and wasn’t in this chat. I regret a bit about it.

euccastro 2020-12-21T17:37:45.035200Z

yeah I only started solving the puzzles (after having binge watched @plexus's videos) so I would be better primed to understand others' solutions

euccastro 2020-12-21T17:37:57.035400Z

I'm still mulling on (reduce into []) πŸ˜„

euccastro 2020-12-21T17:42:39.035600Z

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

euccastro 2020-12-21T17:43:36.035800Z

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 ?

euccastro 2020-12-21T17:45:21.036200Z

(my own solution needs some cleanup, I'm just making sure I'm not missing something subtle)

2020-12-21T17:49:36.036400Z

> but wouldn’tΒ `frequencies`Β work the same had you concatenated them before by usingΒ `mapcat` Actually, yes. Thank you for pointing out!

euccastro 2020-12-21T17:54:46.036600Z

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

2020-12-21T18:03:19.037Z

It was originally built for day 16, over there it makes more sense than here.

2020-12-21T18:09:20.037400Z

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.

1πŸ‘
2020-12-21T18:09:24.037600Z

@euccastro thank you for the code review ^_^

1πŸ‘
erwinrooijakkers 2020-12-21T21:16:59.043300Z

Late to the party: https://github.com/transducer/adventofcode/blob/master/src/adventofcode/2020/day21.clj

2πŸ‘
2020-12-21T06:15:43.015500Z

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

1πŸ‘5πŸŽ‰
2020-12-21T06:16:18.015800Z

wow ! you are fast

2020-12-21T06:16:34.016100Z

I didn’t do the part 1 yet

2020-12-21T06:17:53.016300Z

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.

2020-12-21T06:57:22.018500Z

I got my 2 stars ^_^ .Β  After I sawΒ @dawran6Β finishing early, I decided to try again for another hour.

2πŸ‘
2020-12-21T07:09:42.019800Z

πŸ˜„

peterc 2020-12-21T08:03:11.021200Z

@vincent.cantin Love your solution for part 1 πŸ˜„

peterc 2020-12-21T08:03:25.021400Z

So simple but it works like a charm

1😊
euccastro 2020-12-21T08:16:00.022200Z

(yeah, I finally woke up early-ish and couldn't resist giving it a try)

euccastro 2020-12-21T08:18:20.022400Z

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 πŸ˜›

euccastro 2020-12-21T08:19:14.022700Z

can't overemphasize the value of stepping away from the computer sometimes

4βž•
2020-12-21T08:19:41.022900Z

I found my solution after running after the cat which kept interrupting me..

1🐱
Alexandre Grison 2020-12-21T10:28:53.025300Z

Did one also, except I solved this one with Kotlin πŸ˜„

1😁
misha 2020-12-21T11:15:42.026100Z

@nbardiuk need a util function for pruning already, yeah.

1πŸ’―
2020-12-21T12:35:34.027400Z

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 πŸ™‚

3πŸ‘
benoit 2020-12-21T12:40:16.027700Z

Solution to Day 21: https://github.com/benfle/advent-of-code-2020/blob/main/day21.clj

2πŸ‘
benoit 2020-12-21T12:40:23.028Z

This one was more reasonable than yesterday πŸ™‚

2βž•
benoit 2020-12-21T12:41:27.028300Z

The only interesting bit is the use of the fixed-point method to identify the food containing each allergen.

2020-12-21T12:43:17.028500Z

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

1πŸ™
benoit 2020-12-21T12:59:41.029400Z

Nice one @zelark. A lot of work is done in the parsing function :)

1πŸ™
benoit 2020-12-21T13:00:05.029600Z

I need to improve my parsing/regexp game.

misha 2020-12-21T13:10:36.030200Z

> newcomer to both clojure and aoc @

[clojure.algo.generic.functor :refer [fmap]]
:tatatananana:

2020-12-21T13:26:37.030800Z

@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

misha 2020-12-21T13:37:51.031Z

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

1πŸ“Ž
misha 2020-12-21T13:40:00.031200Z

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

1πŸ’‘
2020-12-21T14:06:41.031700Z

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

2020-12-21T15:47:20.032500Z

Maybe next year it will be part of the stdlib (I am kidding, that's impossible)

1πŸ˜ƒ
euccastro 2020-12-21T17:04:27.032800Z

I feel silly having matched the parens of the "(contains ...)" part after seeing @zelark just split with #"contains" and grab the words πŸ™‚

1βž•
2020-12-21T17:05:46.033100Z

It's ok to be paranoid .. don't worry.

2020-12-21T17:06:20.033300Z

the ingredients are probably randomly generated, they may also be spelled "contains".

2020-12-21T17:07:00.033500Z

For the same reason, I won't use read-string but edn/read-string

euccastro 2020-12-21T17:23:20.033900Z

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

euccastro 2020-12-21T17:24:30.034100Z

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)

euccastro 2020-12-21T17:28:47.034400Z

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

euccastro 2020-12-21T17:29:22.034600Z

i.e., as opposed to keeping allergen sets by line

2020-12-21T17:31:16.034800Z

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

1βž•
2020-12-21T17:32:46.035Z

Last year I solved puzzles solely and wasn’t in this chat. I regret a bit about it.

euccastro 2020-12-21T17:37:45.035200Z

yeah I only started solving the puzzles (after having binge watched @plexus's videos) so I would be better primed to understand others' solutions

euccastro 2020-12-21T17:37:57.035400Z

I'm still mulling on (reduce into []) πŸ˜„

euccastro 2020-12-21T17:42:39.035600Z

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

euccastro 2020-12-21T17:43:36.035800Z

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 ?

euccastro 2020-12-21T17:45:21.036200Z

(my own solution needs some cleanup, I'm just making sure I'm not missing something subtle)

2020-12-21T17:49:36.036400Z

> but wouldn’tΒ `frequencies`Β work the same had you concatenated them before by usingΒ `mapcat` Actually, yes. Thank you for pointing out!

euccastro 2020-12-21T17:54:46.036600Z

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

2020-12-21T18:03:19.037Z

It was originally built for day 16, over there it makes more sense than here.

2020-12-21T18:09:20.037400Z

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.

1πŸ‘
2020-12-21T18:09:24.037600Z

@euccastro thank you for the code review ^_^

1πŸ‘
2020-12-21T18:11:35.038400Z

4 days to go !

3πŸŽ„3🌲11⭐5
Average-user 2020-12-21T18:46:26.042Z

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

erwinrooijakkers 2020-12-21T21:16:59.043300Z

Late to the party: https://github.com/transducer/adventofcode/blob/master/src/adventofcode/2020/day21.clj

2πŸ‘
erwinrooijakkers 2020-12-21T21:23:54.043700Z

One ugly but practical way to solve it is by counting the heads visible in your input, get the images (without borders) to count total number of # and do:

(def char-count-sea-monster
  15)

(def sea-monster-count-guess
  23)

(- total-hashes (* char-count-sea-monster sea-monster-count-guess))
;; => 1576