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
mfikes 2017-12-17T00:13:27.000057Z

@val_waeselynck I did some quick research before solving it and read about the coordinate systems at https://www.redblobgames.com/grids/hexagons/ and found one that seemed to have a simple distance formula. I’m pretty sure that without having done this research it would have taken a lot of thinking to sort through this one. I went with cube coordinates, which seemed to be pretty popular. The solution for me ended up being fairly clean: https://github.com/mfikes/advent-of-code/blob/master/src/advent_2017/day_11.cljc

2017-12-17T17:38:22.000042Z

I have a solution to day11 without using coordinates https://github.com/thegeez/clj-advent-of-code-2017/blob/master/src/advent/day11.clj (in the alternative for part 2 and 1)

fellshard 2017-12-17T01:58:36.000030Z

Do you have any links to where I can read up on this? I understand how the cyclic nature would lead you to the right answer, but I'm not sure I see how we can assume the dance itself is idempotent.

fellshard 2017-12-17T02:07:20.000002Z

Ohh, wait. Because the possible permutations operations are limited, there are far fewer than 1e13 operations that can be performed?

val_waeselynck 2017-12-17T02:24:37.000069Z

@fellshard re: your question, see this thread 🙂

val_waeselynck 2017-12-17T02:26:50.000011Z

@mfikes Cool blog! I did not have the option of doing that kind of research as I was on a plane, so I ended up working out some math that are a similar but slightly inferior approach to cube coordinates. I did have to sleep over the problem to come up with this, so definitely not a viable approach if I wanted to be competitive on AoC

borkdude 2017-12-17T08:37:27.000021Z

Hmm, after the state

[[0 2 3 1] 3] ;; s1
I expect it to be
[[0 4 2 3 1] 4] ;; s2
but the example gives 0 2 (4) 3 1

borkdude 2017-12-17T08:39:34.000039Z

3 is the current position of s1. going 3 steps means going to position 0 whereafter 4 will be inserted.

val_waeselynck 2017-12-17T08:43:21.000062Z

My solutions for day 17: https://github.com/vvvvalvalval/advent-of-code-2017/blob/master/src/aoc2017/day17.clj

borkdude 2017-12-17T08:43:27.000007Z

o wait, I don’t have to go back and forth probably

borkdude 2017-12-17T08:55:23.000008Z

Got it.

borkdude 2017-12-17T09:40:11.000047Z

Solved.

2017-12-17T10:40:30.000030Z

these puzzles…

2017-12-17T11:17:12.000011Z

I have a solution that works with the example

2017-12-17T11:17:36.000007Z

but doesn’t with 50M

2017-12-17T11:27:48.000038Z

hah, I used wrong input 😂

orestis 2017-12-17T12:21:47.000049Z

My solution for part 1 is slow, no way it can scale to 50M…

orestis 2017-12-17T12:53:06.000014Z

Bah. Still too slow for my tastes.

fellshard 2017-12-17T13:48:15.000051Z

It's another one that's asking you to dig into the math of it

fellshard 2017-12-17T13:48:42.000018Z

Or, at least, to find a simplifying pattern

orestis 2017-12-17T13:54:05.000031Z

I hang my head in shame; I had a limited time to solve this, explored some time looking for patterns, then in the end copied @val_waeselynck algorithm.

orestis 2017-12-17T13:54:43.000057Z

I was looking for some mathematical properties, cycles and so on; but I found too many 🙂

orestis 2017-12-17T13:55:24.000006Z

Here’s my exploration, warts and all if anyone is interested: https://github.com/orestis/adventofcode/blob/master/clojure/aoc/src/aoc/2017_day17.clj

orestis 2017-12-17T13:58:45.000046Z

In my case, I found that 0 is always at the end; therefore the value I care about is always going to be first.

orestis 2017-12-17T13:59:19.000069Z

I’m not sure if this is what the requirements say; i.e. do they care for the first element after the value 0 or the element at the 0 position? Is it the same for all the inputs?

orestis 2017-12-17T14:03:48.000012Z

BTW, some eval’ed comments there are stale; I was tweaking things in the editor a lot.

grzm 2017-12-17T15:43:07.000048Z

They're looking for value (get buf 1)

bhauman 2017-12-17T16:01:47.000044Z

well my second part should finish in 6 minutes

bhauman 2017-12-17T16:02:19.000040Z

I'm finished with mine but again I'll have to wait for 6 minutes to pass to be sure

bhauman 2017-12-17T16:04:17.000069Z

looked for patterns but just couldn't swing it, so I optimized the heck out of the data structure and the transitions

bhauman 2017-12-17T16:11:34.000024Z

well the answer was correct at least

bhauman 2017-12-17T16:25:44.000019Z

oh position zero has a special property that the rest don't have ...

bhauman 2017-12-17T16:26:12.000007Z

well that would have been easier, nice work @val_waeselynck

mfikes 2017-12-17T16:41:40.000031Z

@bhauman Still, it is remarkable that you were able to come up with an imperative version that could solve it by brute force.

✅ 1
borkdude 2017-12-17T17:05:24.000013Z

Mine runs in 2s 🙂

borkdude 2017-12-17T17:08:47.000008Z

In part 1 I lost time because I thought you should run back and forth in the buffer instead of cyclic. Doh…

borkdude 2017-12-17T17:19:41.000013Z

@bhauman how does yours even work? is this work parallelizable at all? (well yeah, supposedly, but I have to think about it)

bhauman 2017-12-17T17:44:27.000045Z

I used a cyclical datastructure {a -> b, b->c} i.e. a circular linked list which allowed for fast insertion and lookups

bhauman 2017-12-17T17:45:13.000037Z

not really paralizable

dpsutton 2017-12-17T17:46:17.000055Z

can anyone do me a favor? I'm behind, only on day 7. My friend is taking a flight from SF and wants to do days 10 and 11. Can anyone print those pages with both parts and the answers so he can work on them on the flight and email them to me?

dpsutton 2017-12-17T17:46:50.000027Z

ah nevermind. he found them on reddit

dpsutton 2017-12-17T17:46:59.000007Z

sorry for the noise

bhauman 2017-12-17T17:46:59.000028Z

cool!

bhauman 2017-12-17T17:47:02.000056Z

no worries

borkdude 2017-12-17T17:48:06.000054Z

@bhauman r/reduce applies the function in parallel over the sequence right?

bhauman 2017-12-17T17:48:37.000069Z

nope

mfikes 2017-12-17T17:48:56.000053Z

One twist I really liked was Nathan Armstrong's approach of keeping the insertion point at the end of the actual collection representing the circular buffer, thus allowing conj to be used to insert.

bhauman 2017-12-17T17:48:58.000048Z

@borckdude it can't as it needs the input from the previous iter

mfikes 2017-12-17T17:49:41.000068Z

Nathan's approach also means you need not keep track of the current position, if I'm reading his code correctly.

bhauman 2017-12-17T17:50:27.000014Z

@mfikes with a circular buffer you don't need to keep track of the last position

mfikes 2017-12-17T17:50:33.000090Z

Nathan's code, for reference: https://github.com/armstnp/advent-of-code-2017/blob/master/src/advent_of_code_2017/day-17.clj

mfikes 2017-12-17T17:50:48.000064Z

@bhauman Exactly. I didn't take that approach.

bhauman 2017-12-17T17:51:09.000086Z

oh sorry,let me look at nathan's ...

mfikes 2017-12-17T17:51:42.000054Z

The main idea is to rotate the representation so that the current position is always at the end of that representation.

borkdude 2017-12-17T17:51:51.000020Z

@bhauman let me try it differently: what did you use reducers for specifically?

bhauman 2017-12-17T17:51:58.000113Z

speed

bhauman 2017-12-17T17:54:57.000045Z

actually I didn't need to use a reducer

borkdude 2017-12-17T17:55:13.000012Z

can you elaborate? if there’s nothing to parallelize, what’s the speed gain?

bhauman 2017-12-17T17:55:32.000065Z

I thought the reducer would treat the range as an iterable

bhauman 2017-12-17T17:55:38.000134Z

and not a lazy seq

bhauman 2017-12-17T17:57:02.000122Z

which it think is the wrong assumption, transduce would have done that for me

dpsutton 2017-12-17T17:58:48.000018Z

on second thought, could anyone email me the full page for day 13?

dpsutton 2017-12-17T17:59:12.000096Z

was trying to find it online for him but no luck

dpsutton 2017-12-17T18:01:47.000122Z

perfect! thanks!

dpsutton 2017-12-17T18:02:20.000087Z

you'll make a flight of his much better.

bhauman 2017-12-17T18:02:21.000131Z

@borkdude inputs?

mfikes 2017-12-17T18:10:00.000008Z

I'm curious: Is there a technique that behaves like iterate, but produces something that can reduce itself? The reason I ask is that when running the solutions in ClojureScript, the lack of locals clearing can be worked around by carefully using transducers. By way of an example, you can count this way without blowing out memory:

(transduce (comp (map inc) (filter odd?))
  (completing (fn [c _] (inc c)))
  0
  (range 1e8))

bhauman 2017-12-17T18:19:06.000063Z

this is a good question, I'd like to know the answer as well

borkdude 2017-12-17T18:35:06.000037Z

@dpsutton if you want an input: https://github.com/borkdude/aoc2017/blob/master/resources/day13.txt

dpsutton 2017-12-17T18:35:32.000020Z

ah thanks

2017-12-17T18:45:43.000003Z

@mfikes something like this for reducible iterate:

(deftype Iter [f init]
  clojure.lang.IReduceInit
  (reduce [this xf xf-init]
    (loop [state (xf xf-init init)
           i init]
      (if (reduced? state)
        @state
        (let [i (f i)
              ret (xf state i)]
          (recur ret i))))))

(comment
  (into []
        (take 10)
        (Iter. inc 0))
  )

borkdude 2017-12-17T18:50:17.000009Z

@dpsutton Why am I sending a PDF if this page is public btw? https://adventofcode.com/2017/day/13

dpsutton 2017-12-17T18:51:53.000058Z

because it only shows the first half until you solve it

borkdude 2017-12-17T18:52:08.000124Z

ah ok. and now he also has my outcomes + input, so that should do it.

dpsutton 2017-12-17T18:52:24.000001Z

and he's on a flight so he could only see the first half. yeah. he's gonna get the answer himself he just wanted something to work on

dpsutton 2017-12-17T18:52:31.000042Z

and usually the second half is the more interesting half

borkdude 2017-12-17T18:58:38.000056Z

@mfikes aren’t iterate results already reducible? https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/Iterate.java#L15

borkdude 2017-12-17T18:59:04.000057Z

Not in ClojureScript it seems.

borkdude 2017-12-17T18:59:20.000016Z

Found this link: http://insideclojure.org/2015/01/18/reducible-generators/

mfikes 2017-12-17T19:16:52.000020Z

Oh, cool @borkdude. Perhaps a patch could land in ClojureScript to make it behave like Clojure 🙂

bhauman 2017-12-17T21:28:38.000080Z

so if I wanted range to be reducible?

bhauman 2017-12-17T21:55:58.000003Z

OK I have explored the sources and I've got my head on straight now, range does know how to reduce itself and this behavior is obvious from how fast it is

val_waeselynck 2017-12-17T22:23:25.000094Z

@erwin @orestis @nooga @fellshard Still having fun with Permutations Algebra, I reimplemented day 16 ("Permutation Promenade") without leveraging the cyclic nature of the dance at all, using exponentiation instead: https://github.com/vvvvalvalval/advent-of-code-2017/blob/master/src/aoc2017/day16.clj#L320

🎉 3
val_waeselynck 2017-12-17T22:24:09.000067Z

I also added a more "literate style" guide explaining how it all works: https://github.com/vvvvalvalval/advent-of-code-2017/blob/master/src/aoc2017/day16.clj#L146

val_waeselynck 2017-12-17T22:28:02.000148Z

I do realize I'm totally over-engineering this btw 😄

bhauman 2017-12-17T22:47:48.000028Z

@val_waeselynck am I correct in thinking that this day17 part 2 solution only works in the first position? I don't think it will work other positions as their positions are more easily disturbed?

val_waeselynck 2017-12-17T22:52:02.000063Z

@bhauman that is correct, I'm relying on the fact that in my representation of the cyclic data structure, 0 has a stable position

bhauman 2017-12-17T22:58:04.000133Z

got it

mikelis 2017-12-17T23:38:15.000056Z

@val_waeselynck nice, looks like I came up with something similar https://github.com/axelarge/advent-of-code/blob/master/src/advent_of_code/2017/day16.clj#L63-L78 Although I unfortunately don’t fully grok the math beneath it, I just experimented around until I confirmed my intuition

mikelis 2017-12-18T09:21:11.000127Z

Hmm it’s function composition only in the sense that mapping a vector of indices over a vector of indices is composition. I’m compiling the dance down to 2 actions - spin+exchange and partner. They could each be passed to exponentiate to get the nth result

mikelis 2017-12-18T09:21:23.000012Z

Actually I should probably try that :)

val_waeselynck 2017-12-18T22:55:05.000344Z

Ah right, I guess I misunderstood the code then

mikelis 2017-12-17T23:38:58.000060Z

the optimized log n exponentation part is nice, probably saves even more time if you know which index you need

mikelis 2017-12-17T23:41:25.000065Z

yeah, mine still relies on finding the cycle period, with log n application that wouldn’t be needed.. very nice solution & explanation!

mikelis 2017-12-17T23:44:41.000068Z

for example, I haven’t really gotten my mind around why the order of applying the spin+exchange and partner moves matters (that is, (spins (partners state)) is not the same as (partners (spins state)) except at the cycle period