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
karlis 2018-12-01T05:31:17.014200Z

happy advent of code 2018 everyone! 🎉

karlis 2018-12-01T05:32:02.015100Z

woke up, made coffee, read the puzzle 1 & wasn't sure if I'm still half asleep or is that really a 1-liner.

nate 2018-12-01T05:54:25.015400Z

heh

fellshard 2018-12-01T06:00:37.016100Z

Day one is usually just a warmup. I use the time I'd normally take to get the environment warmed up, etc.

fellshard 2018-12-01T06:01:12.016900Z

I have a standard workspace I use now for these, since there's a lot of common core utils - reading from resources file, parsing integers, defining coarsely-curried functions for interpreters, etc.

orestis 2018-12-01T06:01:51.017900Z

Happy puzzling! I did today’s in Replete typing on an iPad keyboard :)

😄 3
nate 2018-12-01T06:04:16.018200Z

@orestis wow!

orestis 2018-12-01T06:05:10.018500Z

Yeah but it’s tiny :)

orestis 2018-12-01T06:06:27.020700Z

I don’t think I will do another, though with some love replete could easily be much more useful. Being able to save some files locally or even better to iCloud, some helpful keys above the keyboard. I wonder if it’s open source.

orestis 2018-12-01T06:12:02.021400Z

It is! Perhaps I’ll pivot from AoC this year then.

mfikes 2018-12-01T15:15:57.057Z

Yeah, I maintain Replete. Custom keyboard stuff is always challenging to pull off while preserving the polished feel, but I definitely agree there could be some keyboard improvement. (You can always use a 3rd party keyboard with it.) The ability to save and edit locally or in iCloud would be nice. We can't do this, as it is not allowed http://blog.fikesfarm.com/posts/2015-08-11-loading-clojurescript-into-your-iphone.html but perhaps content created in Replete could be saved without violating any App Store rules.

lilactown 2018-12-01T06:28:08.022Z

I feel like there’s a clever use of lazy seqs in the 2nd part that I’m not seeing

mfikes 2018-12-01T15:18:56.057300Z

Here is a way to do it using only lazy seqs, but in the end, I didn't like that approach compared to just a direct reduce to find the dup. https://gist.github.com/mfikes/53c53673f66257878ab82408635f297d

nate 2018-12-01T06:29:33.022600Z

Yeah, or using cycle

orestis 2018-12-01T06:49:57.023900Z

Given my constraints, I just went with a loop recur for part 2. Not much room for experimenting.

2018-12-01T06:50:17.024200Z

my part 2 was def not 1 line

orestis 2018-12-01T06:52:13.025300Z

Yeah, more like 10 in my case. It’s gone now so I can’t count :)

borkdude 2018-12-01T07:48:28.025600Z

Submitted my solution for day 1 to Advent of CLJC: https://github.com/borkdude/advent-of-cljc

borkdude 2018-12-01T07:51:23.026300Z

curious about your solutions. if you want you can make a PR and add it to the repo

borkdude 2018-12-01T07:52:19.026900Z

The fun thing is you can see the timing of every day and user here: https://circleci.com/gh/borkdude/advent-of-cljc/55 when you scroll to the test execution

helios 2018-12-01T08:20:21.028100Z

@borkdude where is the private leaderboard? 🙂

Mario C. 2018-12-01T08:22:27.028500Z

wow part 2 was a doozy!

Mario C. 2018-12-01T08:23:46.029100Z

are we allowed to post solutions here?

borkdude 2018-12-01T08:35:16.030Z

@helios I don’t know if there is a leaderboard. You might want to create one? @mario.cordova.862 You could also submit a PR to the above repo as an alternative of posting your solution here 🙂

borkdude 2018-12-01T08:36:32.031100Z

I wouldn’t post your code here directly, a gist would be more appropriate. Of if you’re going to post it, post it only in a thread, so people don’t see the spoiler.

helios 2018-12-01T08:36:32.031200Z

57048-763fafc9 is the code for the private leaderboard

borkdude 2018-12-01T08:37:24.031800Z

@helios thanks. might also want to add it to the https://github.com/adventofcode-clojurians/adventofcode-clojurians repo

helios 2018-12-01T08:39:00.032300Z

@borkdude done. also added myself

Mario C. 2018-12-01T08:49:51.032600Z

@borkdude Added myself to the readme

borkdude 2018-12-01T08:50:04.033100Z

@mario.cordova.862 can you rebase your PR? it has a conflict now

niels 2018-12-01T08:50:15.033200Z

Mario C. 2018-12-01T08:57:51.033900Z

@borkdude done!

orestis 2018-12-01T09:01:18.035400Z

There used to be an adventofcode-clojurians leaderboard, but I don’t have the invitation code. Perhaps clojurians slack log might have it?

2018-12-01T09:34:47.037900Z

Created pull request for adventofcode-clojurians. Also slightly offtopic does anyone else has a internal conflict of writing things in a simple way vs smart? I ask because as a developers I feel that we are constantly judged on quality of our code and how "smart" it is but when reading other people code I like things rather simple.

borkdude 2018-12-01T09:45:38.038500Z

@karol Sometimes there’s a balance between keeping it simple and hacking it for performance

2018-12-01T09:47:58.040700Z

of course, and that is why I like advent of code because usually the first part can be done without optimization and you need to be clever in second part 😉 but I am fan of optimize only when needed and don't introduced layers of abstraction "for future", because I saw few clojure codebases that look like Java EE with parentheses but it is morning here so I might be just complaining 😄

borkdude 2018-12-01T10:04:00.041100Z

I found the old leaderboard code, updated the topic (cc @helios)

borkdude 2018-12-01T10:04:36.041400Z

didn’t know the leaderboards could be re-used over multiple years

helios 2018-12-01T10:11:12.042100Z

👍

helios 2018-12-01T10:12:32.042600Z

i have no idea how AoC works, are points based on time as well?

borkdude 2018-12-01T10:17:55.043Z

the faster you solve the problem, the higher your score in the leaderboard

helios 2018-12-01T10:22:07.043300Z

gotcha

2018-12-01T10:42:46.044200Z

And its nothing to do with when you start solving! Time starts for everyone when the problem appears.

borkdude 2018-12-01T10:48:34.044800Z

@denis.fuenzalida @dandorman thanks for contributing to advent of cljc https://github.com/borkdude/advent-of-cljc

orestis 2018-12-01T10:49:02.045600Z

I just realized what advent-of-cljc is, I’ll be forking that as well.

orestis 2018-12-01T10:49:14.045800Z

Should we add more inputs there?

borkdude 2018-12-01T10:52:14.046200Z

no, the author of advent of code explicitly forbids it

borkdude 2018-12-01T10:52:30.046400Z

See: https://github.com/borkdude/advent-of-cljc/issues/6

borkdude 2018-12-01T10:52:50.047Z

So we’ll have to do with the input of whoever does the first PR

borkdude 2018-12-01T10:58:32.047400Z

updated the README to reflect this

orestis 2018-12-01T11:01:27.048100Z

Worth to do the runs at least a few times to ensure warm up for the timings etc?

borkdude 2018-12-01T11:04:20.049200Z

Maybe. I’m not sure how this will work out if multiple users contribute all of their days. If it gets too slow on CI, I might filter the tests only for that user. So running tests multiple times might be something you want to do locally

orestis 2018-12-01T11:07:03.049700Z

I guess not too important at this point, the corpus is the important thing. Great initiative!

orestis 2018-12-01T11:07:12.050Z

I’ve submitted my PR. I’ll use this for this years puzzles.

borkdude 2018-12-01T11:17:21.050700Z

Thanks! Merged.

2018-12-01T12:37:56.051500Z

My repl is timing out on my part 2 solution.. 😅

2018-12-01T13:23:27.052800Z

Would anyone have some time to help me? My process keeps running for part 2 with the puzzle input and just never ends, if I use one of the examples as the input though it does seem to work... (I'll post my code as a thread to this message so spoiler alert!)

2018-12-01T13:24:16.052900Z

(def input (mapv read-string (str/split (slurp "./input") #"\n")))

(defn step [{:keys [frequency-changes current-index frequencies]}]
  (let [
        new-index (mod (inc current-index) (count frequency-changes))
        last-frequency (or (last frequencies) 0)]
    {
     :current-index new-index
     :frequencies (conj frequencies (+ last-frequency (get frequency-changes current-index 0)))
     :frequency-changes frequency-changes
     }))

(defn last-item-double [l]
  (and (> (count l) 1) (> (.indexOf (butlast l) (last l)) -1)))


(defn part2 [frequency-changes] (last (:frequencies (step (last (take-while
 (fn [state] (not (last-item-double (:frequencies state))))
 (iterate step {
        :frequencies []
        :frequency-changes frequency-changes
        :current-index 0
        })))))))

(part2 input)

benoit 2018-12-01T13:44:57.053700Z

My solution for day 1. https://github.com/benfle/advent-of-code-2018/blob/master/day-1.clj

ihabunek 2018-12-01T14:39:51.054700Z

i'm actually doing it in clojure and elixir in parallel, but pretty sure i won't have the time or willpower to do it all the way through in both languages

ihabunek 2018-12-01T14:41:07.055100Z

@me1740 using state, le gasp 😄

benoit 2018-12-01T15:04:10.056200Z

Simplified to use cycle. I forgot about this function. Thanks. Yes state is debatable here but since it improves readability and reusability I opted for it 🙂

orestis 2018-12-01T15:09:44.056300Z

You are using a very inefficient algorithm to check for an already seen item. Using .indexOf etc etc means every time this run means it has to “walk” all the previous steps again and again.

orestis 2018-12-01T15:10:06.056500Z

Consider keeping the previously seen frequencies in a set instead.

orestis 2018-12-01T15:10:55.056700Z

As an aside, also consider formatting your code a little bit better as it can be really hard to follow 😄

2018-12-01T15:22:44.057700Z

I think this problem is going to be a lot of fun on the stream.

2018-12-01T15:23:03.058300Z

I can think of a bunch of different ways to solve it offhand — none of them will take particularly long.

ihabunek 2018-12-01T15:24:24.058700Z

Who is streaming?

2018-12-01T15:24:58.059Z

I am: https://www.twitch.tv/timpote

2018-12-01T15:25:21.059600Z

M-F @ 12:00 CST (UTC-6)

ihabunek 2018-12-01T15:27:30.059800Z

👍 nice

Mario C. 2018-12-01T15:54:22.061800Z

How would day 2 be solved if we had memory constraints?

borkdude 2018-12-01T15:55:41.062Z

day 2?

2018-12-01T16:08:01.062200Z

we have a cheater! 😛

Mario C. 2018-12-01T16:09:34.062500Z

Sorry Day 1 Part 2

benoit 2018-12-01T16:11:07.063800Z

Are people ok with discussing solutions here? What is the assumption about spoilers?

nate 2018-12-01T16:12:33.064400Z

Maybe put the spoiler code in a thread?

mfikes 2018-12-01T16:13:18.065100Z

I tend to not even read this channel until I'm done with my solution. Otherwise you might see solution strategies being discussed.

☝️ 3
mfikes 2018-12-01T16:14:26.066100Z

For example, you can't help from seeing cycle being mentioned. 🙂

benoit 2018-12-01T16:15:15.067100Z

🙂 yeah I realized that after the fact. Sorry.

mfikes 2018-12-01T16:15:50.068300Z

To be honest, I enjoy seeing solution strategies being discussed. (Otherwise, where would they be, all in side threads?)

nate 2018-12-01T16:15:52.068500Z

Good idea to stay away.

mfikes 2018-12-01T16:16:42.070100Z

Also, the ability to look at other people's solutions and other ideas is the main value I get out of this. Much more than solving the problems myself. 🙂

➕ 2
benoit 2018-12-01T16:17:11.071400Z

So to answer Mario. I think if you stream in the changes and stream out the frequency in a sorted file you could solve it while just keeping one number in memory. But I didn't bother doing this 🙂

mfikes 2018-12-01T16:17:15.071600Z

There have been some incredibly clever solutions in the past. 🙂

Mario C. 2018-12-01T16:17:16.071800Z

I tend to solve it first then come on here but I am guessing at later days when it becomes more difficult Ill be coming here for pointers :P

mfikes 2018-12-01T16:18:14.072400Z

Yeah, there's a problem where you are stuck and want a little help, without seeing the solution. :thinking_face:

nate 2018-12-01T16:21:59.073500Z

Heh. Separate channel? #adventofcode-help ?

2018-12-01T16:22:12.073700Z

:moar_channels:

mfikes 2018-12-01T16:25:31.075300Z

One thing I see now is that, even though my solution works on my data, I have a defect in it 🙂

mfikes 2018-12-01T16:28:51.076500Z

My solution gives the wrong answer for this input: [5 10 20 -35]. I suspect this will be a common mistake.

benoit 2018-12-01T16:29:58.077800Z

My first solution was not handling the case when there is no duplicate either 🙂

borkdude 2018-12-01T16:30:06.078100Z

Thanks for submitting your solution to https://github.com/borkdude/advent-of-cljc @mfikes!

👍 1
mfikes 2018-12-01T16:30:08.078400Z

Wastl even gives a simpler example of [+1 -1] in the page

bhauman 2018-12-01T16:36:58.079100Z

@mfikes good catch

mfikes 2018-12-01T16:37:19.079500Z

I only learned about this when I saw another solution catering to that case.

bhauman 2018-12-01T16:37:40.080Z

TRY ALL THE EXAMPLES

mfikes 2018-12-01T16:37:48.080600Z

Indeed

bhauman 2018-12-01T16:37:51.080800Z

that is the lesson I learn every year

bhauman 2018-12-01T16:38:00.081Z

cause I forget

taylor 2018-12-01T16:51:13.083300Z

and here’s mine, enjoying seeing the different solutions! https://github.com/taylorwood/advent-of-code/blob/master/src/advent_of_code/2018/1.clj

ClashTheBunny 2018-12-01T16:51:23.083600Z

I do wish somebody would make a data oriented source of the examples. I just want to pull in a mapping of input to output.

Mario C. 2018-12-01T16:56:09.085100Z

@taylor wow! Learned something new with reductions, reduced :thumbsup:

taylor 2018-12-01T16:56:18.085200Z

feeling pretty cheeky about (drop-while #(apply not= (swap-vals! seen conj %))) :man-juggling:

taylor 2018-12-01T16:59:52.086300Z

nice! it seems a lot of people reached the same conclusion for part 2, though I shamelessly went with atoms for my first attempt

ClashTheBunny 2018-12-01T17:00:46.086400Z

I also have the same issue with my solution. I wish there was a reduce-until function or something like that.

taylor 2018-12-01T17:01:27.087Z

like reduced?

mfikes 2018-12-01T17:02:43.088200Z

I really wanted to find a way to reuse distinct. Here is one way, but I'm wondering if there is some other clever way to reused the underlying capability in the core lib. https://gist.github.com/mfikes/53c53673f66257878ab82408635f297d

👏 1
taylor 2018-12-01T17:04:49.088900Z

I thought about making a variation of distinct transducer like while-distinct or something

ClashTheBunny 2018-12-01T17:06:08.089100Z

Damn, time to refactor!!!

1
borkdude 2018-12-01T17:11:07.090400Z

I’m not sure why people are using reductions?

benoit 2018-12-01T17:11:34.091Z

To get a sequence of the frequencies after applying the changes.

lilactown 2018-12-01T17:11:43.091500Z

yeah I ended up using loop-recur and cycles: https://github.com/Lokeh/advent-2018/blob/master/day1/src/advent/day1.clj#L54

2018-12-01T17:11:45.091600Z

To generate a list of accumulations, instead of manually managing in reduce

ClashTheBunny 2018-12-01T17:12:01.092Z

Thanks!!!

borkdude 2018-12-01T17:12:09.092400Z

clever

2018-12-01T17:12:12.092600Z

yeah

2018-12-01T17:12:21.093Z

less efficient, but breaks part of the computation out

_rj_r_ 2018-12-01T17:13:20.093500Z

is it less efficient since it is lazy? (I'm asking because I genuinely don't know)

taylor 2018-12-01T17:13:42.093600Z

(defn while-distinct []
  (fn [rf]
    (let [seen (volatile! #{})]
      (fn
        ([] (rf))
        ([result] (rf result))
        ([result input]
         (if (contains? @seen input)
           (reduced (rf result input))
           (do (vswap! seen conj input)
               (rf result input))))))))

(->> (cycle values)
     (reductions +)
     (eduction (while-distinct))
     (last))
though that name isn’t great because it includes the first non-distinct value :man-shrugging:

2018-12-01T17:14:32.094200Z

it generates another data structure (the laziness only helps with efficiency).

2018-12-01T17:14:59.094700Z

It’s honestly not something you should think about all the time. But I like running through various levels of efficiency in my head.

2018-12-01T17:16:34.097600Z

so, using cycle+reductions+reduce -> (seq of cycle) + (seq of reductions) + #{set of reduce}

_rj_r_ 2018-12-01T17:16:55.098100Z

my thought was that pretty much either way a new data structure needs to be generated at least from the solutions I've seen thus far, so I wasn't sure if using reductions was actually less efficient than managing the computations and what has been seen in a reduce.

2018-12-01T17:16:56.098200Z

reduce is only (seq of cycle) + #{set of reduce}

_rj_r_ 2018-12-01T17:17:10.098500Z

hmm.. I think I understand that

2018-12-01T17:17:39.099200Z

but it’s evaluated lazily, so you only do the computational work that you must

_rj_r_ 2018-12-01T17:17:46.099400Z

right.. gotcha

_rj_r_ 2018-12-01T17:17:48.099600Z

thanks!

2018-12-01T17:17:51.099800Z

yep!

nate 2018-12-01T18:04:07.100200Z

oh man, the reduced function!! TIL

pesterhazy 2018-12-01T18:30:57.100700Z

yo!

pesterhazy 2018-12-01T18:31:56.101300Z

Here's my solution: https://github.com/pesterhazy/advent2018/blob/master/src/advent/puzzle01.clj

pesterhazy 2018-12-01T18:32:35.102300Z

I'm curious how much room for difference there is in a simple task like this

mfikes 2018-12-01T18:35:12.103400Z

It seems like most solutions involve a reduce or a direct loop / recur to essentially walk the sequence looking for the first dup.

pesterhazy 2018-12-01T18:36:57.104200Z

@mfikes I saw your tweet and couldn't resist 🙂

ClashTheBunny 2018-12-01T18:37:08.104800Z

I thought a bit about how to just calculate it analytically, but didn't see anything easy. Did anybody come up with a way?

mfikes 2018-12-01T18:37:11.104900Z

Excellent. 🙂

pesterhazy 2018-12-01T18:37:44.105500Z

Of course I spent 90% of the time setting up the clj tool with nREPL and the Corfield Comma

pesterhazy 2018-12-01T18:39:40.106Z

@mfikes your idea of using two reductions on top of each other is clever

pesterhazy 2018-12-01T18:40:14.106500Z

other than that mine is identical almost to a character

dpsutton 2018-12-01T18:41:21.107500Z

reduce the reductions you mean?

pesterhazy 2018-12-01T18:41:27.107800Z

yeah

pesterhazy 2018-12-01T18:41:45.108200Z

is that a common FP pattern? I wasn't aware

dpsutton 2018-12-01T18:42:13.109400Z

i did something similar. the data to investigate is the reduction. seems not necessarily FP but just this problem to me

mfikes 2018-12-01T18:42:43.110300Z

Paulus, does your solution handle the sequence [+1 -1]? (This is a common defect.)

pesterhazy 2018-12-01T18:42:50.110500Z

reduce is a great abstraction (obviously!) because you can swap in reductions and get a nice debugging output for free. Plus if the sequence doesn't terminate, you can lazily take n

✔️ 2
pesterhazy 2018-12-01T18:43:41.111200Z

@mfikes yep

mfikes 2018-12-01T18:43:48.111400Z

Cool

dpsutton 2018-12-01T18:44:03.111700Z

(first-duplicate (drop 1 (reductions + 0 (cycle values)))) yeah something similar

rmprescott 2018-12-02T18:35:26.266100Z

Why the drop 1? This is probably related to why I disagreed with [+1 -1] => 0 rather than 1. But I still don't see why.

dpsutton 2018-12-02T18:38:32.266300Z

yes it gets rid of the 0 sum for the sequence of length 0. i originally didn't have it

pesterhazy 2018-12-01T18:44:37.111800Z

nice

mfikes 2018-12-01T18:45:39.113400Z

Maybe some could be used to mechanize the linear scan?

2018-12-01T18:45:53.113700Z

I’ll ping back here after the stream with a link to github if you just want to review results.

mfikes 2018-12-01T18:46:00.114100Z

Your predicate would be stateful 😞

pesterhazy 2018-12-01T18:46:56.115Z

My solution uses a "composite accumulator", in this case a vector [acc-freq acc-seen]. I find myself reaching for composite accumulators often for less-than-simple reduces but I wonder if it's a good pattern

2018-12-01T18:47:32.115900Z

that’s^ normal — although there’s often other ways of going about it

dpsutton 2018-12-01T18:48:05.116600Z

i do that all the time. i only get annoyed because it usually requires a step to pluck apart the composite accumulator at the end

pesterhazy 2018-12-01T18:48:26.117Z

The (->> xs (reductions step-1 init-1) (reduce step-2 init-2)) pattern avoids the composite acc

pesterhazy 2018-12-01T18:50:12.118Z

@dpsutton right, although when you know that you're going to end with reduced, you won't need it

pesterhazy 2018-12-01T18:53:27.119300Z

it would be interesting to compare performance as well. reductions must incur some performance cost

dpsutton 2018-12-01T18:53:43.119900Z

I always feel a little bad using reduced. The short circuit makes me feel like I'm abusing reduce

1
mfikes 2018-12-01T18:54:09.120400Z

Yeah, it always makes you feel like you failed, in some way. 🙂

pesterhazy 2018-12-01T18:54:20.120800Z

😆

mfikes 2018-12-01T18:54:46.121400Z

It is like an inclusion an an otherwise perfect diamond.

borkdude 2018-12-01T18:54:49.121500Z

@pesterhazy I have the same pattern

pesterhazy 2018-12-01T18:55:47.122Z

@borkdude ours are practically identical

dpsutton 2018-12-01T18:56:56.123200Z

I've seen reducing functions defined outside of the reduce call where they include reduced and I have no idea what reduced does if it is not in a reducing context

pesterhazy 2018-12-01T18:58:48.123900Z

it just returns a special Java class, clojure.lang.Reduced

pesterhazy 2018-12-01T19:02:07.124500Z

@me1740's stateful predicate is interesting: https://github.com/benfle/advent-of-code-2018/blob/master/day-1.clj#L16 - I'm not sure how I feel about that

mfikes 2018-12-01T19:04:38.125100Z

Right, keep's docstring is at odds with that approach

mfikes 2018-12-01T19:05:41.126100Z

Perhaps, close to that idea, but safer, would be to take the source of distinct and produce an opposite function named duplicates, and then take the first from that

2018-12-01T19:06:21.126400Z

that’s what I did^ for one of mine

2018-12-01T19:06:28.126600Z

can use as a transducer

mfikes 2018-12-01T19:06:41.127Z

Yeah, and a nice little reusable fn falls out of the effort 🙂

2018-12-01T19:06:46.127200Z

yep 🙂

2018-12-01T19:07:03.127600Z

I was hoping to do a big unveil on stream then post back here w/ the code!

mfikes 2018-12-01T19:07:51.128700Z

Hah. It is still a very nice solution. Especially if the transducer version is fast.

2018-12-01T19:08:02.128900Z

yeah I might benchmark them all for fun as well

2018-12-01T19:08:08.129100Z

good point that

pesterhazy 2018-12-01T19:11:33.130700Z

I guess a stateful sequence processing function (like distinct) is safer than a stateful predicate

2018-12-01T19:11:57.130900Z

not safer

2018-12-01T19:12:11.131300Z

but stateful transducers are more idiomatic

pesterhazy 2018-12-01T19:12:37.132Z

"safer" in the sense that using a stateful predicate for filter or keep is violating its contract

2018-12-01T19:12:42.132400Z

right

2018-12-01T19:13:06.133500Z

but you could generate a lazy-seq of dups

pesterhazy 2018-12-01T19:13:08.133700Z

whereas the contract of sequences has no such provision

2018-12-01T19:13:09.133800Z

that would be fine

2018-12-01T19:13:16.134Z

right

pesterhazy 2018-12-01T19:14:19.135Z

so the idea would be a while-distinct function?

2018-12-01T19:14:46.135700Z

just (duplicates coll) the same as (distinct coll)

pesterhazy 2018-12-01T19:15:37.136Z

does the name duplicates make sense though?

benoit 2018-12-01T19:15:57.136500Z

@mfikes good catch. not sure why keep needs to be free of side effect though.

pesterhazy 2018-12-01T19:16:14.137100Z

we're looking for the opposite of duplicates, r ight?

2018-12-01T19:16:20.137300Z

I think so? “I want all of the duplicates in this seq.” As opposed to, “I want the duplicates removed from this seq.”

2018-12-01T19:16:35.137600Z

oh for the problem

2018-12-01T19:16:41.137800Z

yeah it depends on how you’re going about it!

pesterhazy 2018-12-01T19:17:20.138700Z

gotcha, you'd use (->> xs duplicates first)

2018-12-01T19:17:26.138900Z

yep

pesterhazy 2018-12-01T19:18:29.139700Z

actually while-distinct would work only if it was inclusive of the dupe (which makes little sense)

taylor 2018-12-01T19:21:29.139800Z

yeah I had the same thought earlier, duplicates makes more sense https://clojurians.slack.com/archives/C0GLTDB2T/p1543684422093600?thread_ts=1543683763.088200&cid=C0GLTDB2T

mfikes 2018-12-01T19:21:59.140500Z

@me1740 Trying to come up with an example:

cljs.user=> (def x (eduction (keep (duplicate?)) [1 2 3 2 4 2 3]))
#'cljs.user/x
cljs.user=> (into [] x)
[2 2 3]
cljs.user=> (into [] x)
[1 2 3 2 4 2 3]

benoit 2018-12-01T19:24:45.141400Z

Oh ok I see, thanks. Also explain why you should initialize your state inside a transducer function.

benoit 2018-12-01T19:29:19.143800Z

So I had to rewrite that as a transducer with the state inside it:

(defn duplicates
  []
  (fn [xf]
    (let [seen (volatile! #{})]
      (fn
        ([] (xf))
        ([result] (xf result))
        ([result input]
         (if (contains? @seen input)
           (xf result input)
           (do
             (vswap! seen conj input)
             result)))))))

taylor 2018-12-01T19:29:38.144Z

haha I’m playing w/Quil to visualize the solution as it’s calculated, but then I just figured out it’ll take ~40 minutes to finish @ 60fps

pesterhazy 2018-12-01T20:07:40.146200Z

It’s cool to see to which degree thought processes converge

👍 1
2018-12-01T20:09:08.146500Z

thanks for the feedback! I got it to work way faster with a different approach, using loop/recur and a set as you suggested

2018-12-01T20:09:21.146700Z

though my initial solution did end after 1h40 or so 🙈

2018-12-01T20:09:40.146900Z

regarding code formatting, is there a general coding guideline for clojure and a linter like there is for python?

bhauman 2018-12-01T20:29:58.149Z

having both reductions and duplicates transducers would be interesting, I wonder if I would remember to use the duplicates one

2018-12-01T20:31:49.149200Z

cgrand has it in xforms

bhauman 2018-12-01T20:31:57.149700Z

oh ho ho

2018-12-01T20:32:02.149800Z

reductions that is

2018-12-01T20:32:09.150100Z

I don’t think duplicates is there

orestis 2018-12-01T21:13:33.153700Z

There is the clojure style guidelines by Bbatsov, and a few linters. There’s a big discussion right now going on in ClojureVerse about this so you can find more info there (sorry, on mobile so can’t paste links easily)

2018-12-01T21:32:09.153900Z

I'll have a look at them, thanks for your help!

borkdude 2018-12-01T21:40:51.154600Z

xforms is also available in advent-of-cljc, just sayin’ 🙂

helios 2018-12-01T22:07:04.155300Z

@borkdude I just updated the invite code for the leaderboard, I deleted the ones I created this afternoon

borkdude 2018-12-01T22:14:31.156800Z

who owns this advent-of-clojurians repo, I think @thegeez? can you add more people with PR merge rights? It seems I’m the only one doing that right now

adammiller 2018-12-01T22:49:35.157800Z

Probably not the ideal use, but I used NextJournal to do the day 1 challenges. Link if anyone wants to remix it with other styles: https://nextjournal.com/akmiller/advent-of-code-2018--day-1

lilactown 2018-12-01T23:20:31.158300Z

I decided to do the first day in both CLJ and Rust. https://github.com/Lokeh/advent-2018/tree/master/day1

lilactown 2018-12-01T23:21:08.159300Z

we’ll see how far I get before succumbing to my lack of Rust knowledge (and frustrations with the type system) and just do the rest in CLJ :P

lilactown 2018-12-01T23:31:48.160400Z

I think I might update my CLJ solution with a duplicates xf - wanted to in the moment but it was very late for me last night

mfikes 2018-12-01T23:39:49.161100Z

I like how you name the predicate seen?, even though it is implemented using a set. Nice.

mfikes 2018-12-01T23:40:55.161900Z

It is a predicate that doesn't return a Boolean value, but maybe that's OK.

lilactown 2018-12-01T23:43:15.163Z

Yeah I thought it felt nice. I often do that when using sets as predicates