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
2020-12-23T03:42:49.082800Z

Day 23 answers thread - Post your answers here

rjray 2020-12-23T08:15:28.089300Z

I basically implemented a linked-list of numbers for part 2. Don't ask how many bugs I had to deal with. Tomorrow I'll consolidate parts 1 & 2 into smaller code. https://github.com/rjray/advent-2020-clojure/blob/master/src/advent_of_code/day23.clj

๐Ÿ‘ 5
nbardiuk 2020-12-23T12:02:37.093700Z

I've implemented double linked list/circle using maps, it is very slow but gives the answer in several minutes ๐Ÿ˜‚ https://github.com/nbardiuk/adventofcode/blob/master/2020/src/day23.clj

๐Ÿ‘ 2
euccastro 2020-12-23T12:29:20.094600Z

plain arrays, slow but workable

peterc 2020-12-23T12:56:03.095300Z

I implemented a linked-list using a dictionary, and recursed the list + pointer to the head and tail. part-2 took ~125s.

2020-12-23T12:59:31.096Z

The solution I posted today is running part2 in 64 seconds, probably half on your computers. I am cleaning up the code and will see if I can improve its performance a little more.

2020-12-23T13:01:20.096300Z

I am counting on reducing the memory footprint to reduce as well the CPU cost, since the CPU cache becomes the bottleneck here.

2020-12-23T13:27:06.096500Z

down to 57 secs

2020-12-23T14:07:38.097400Z

@nbardiuk When the CPU cache does not contain the memory that the program needs, it loads the data from the central memory and place it in the cache. This is very slow compared to the CPUโ€™s raw speed, itโ€™s the same as if it went to take a coffee while waiting for the next bus to pass by.

2020-12-23T14:23:09.097700Z

down to 25 seconds

๐Ÿš€ 2
2020-12-23T14:47:32.098Z

By using a transient linked-list and assoc!, itโ€™s down to 16 secs.

2020-12-23T14:48:42.098200Z

I think I should stop now before I destroy my source code ๐Ÿ™‚

3
benoit 2020-12-23T15:34:59.102300Z

In the right thread this time. Solution with linked list in an int array (takes around 15s) https://github.com/benfle/advent-of-code-2020/blob/main/day23.clj

๐Ÿ‘ 1
๐ŸŽ‰ 2
2020-12-23T15:38:36.102800Z

@me1740 (doseq [_ (range n)] can also be (dotimes [_ n]

๐Ÿ‘ 2
2020-12-23T15:47:16.103100Z

@me1740 why (aset cups 0 ^int c4) ?

benoit 2020-12-23T15:49:05.103400Z

I keep the value of the current pointer in the first element. And if you move the next 3 elements, the next current pointer will be c4.

๐Ÿ‘ 2
2020-12-23T15:50:08.103700Z

I see, no problem then

Average-user 2020-12-23T16:25:33.106500Z

https://github.com/Average-user/aoc2020/blob/main/src/day23.clj

๐Ÿ‘ 3
๐ŸŽ‰ 2
Average-user 2020-12-23T16:25:53.106700Z

Takes about 1 minute, simulating a linked list with a map

Average-user 2020-12-23T16:26:39.107Z

$ clj -M day23.clj 
[68245739 219634632000]"Elapsed time: 53733.237706 msecs"

Average-user 2020-12-23T16:32:50.107500Z

You are right, thanks. Always forget about that

rjray 2020-12-23T20:36:50.114100Z

Going to re-do my code, using some of the tips from here ๐Ÿ™‚. I completely forgot about transient last night, or my part 2 might have run faster. It currently runs ~32 sec on my machine. I believe I can significantly shrink my code by rewriting part 1 using part 2's structure. (It will also help the line-count when I remove the debugging fn that I left in last night...)

erwinrooijakkers 2020-12-23T21:07:03.114600Z

@lucaspolymeris can you elaborate a bit on โ€œsimulating a linked list with a mapโ€?

Average-user 2020-12-23T21:19:21.114900Z

@erwinrooijakkers The entry of the map at i si the value which i is pointing to in my imaginary linked list.

1
erwinrooijakkers 2020-12-23T21:20:04.115100Z

I see:

(build input)
;; => {7 1, 1 5, 4 3, 6 2, 3 9, 2 4, 9 7, 5 8, 8 6}
input
;; => [6 2 4 3 9 7 1 5 8]

erwinrooijakkers 2020-12-23T21:20:19.115300Z

what are those f and g doing?

erwinrooijakkers 2020-12-23T21:20:29.115500Z

and d

erwinrooijakkers 2020-12-23T21:20:46.115700Z

and r

Average-user 2020-12-23T21:24:20.116Z

to not build the complete map of 1000000 entries, g just returns i+1 if the entry of the map at i is undefined

Average-user 2020-12-23T21:25:36.116300Z

d stands for destination, as in the problem specification. And f I think should be clear by its definition

Average-user 2020-12-23T21:27:03.116500Z

I also ended up changing the map for a vector. Now it takes half the time it did before. But as other people have done, is probably better to use other structures more suited

erwinrooijakkers 2020-12-23T21:34:20.116800Z

ah i see

erwinrooijakkers 2020-12-23T21:36:46.117Z

smart

erwinrooijakkers 2020-12-23T21:37:12.117200Z

thanks for elaboration i learned something ๐Ÿ™‚

๐Ÿ˜€ 1
Average-user 2020-12-23T21:55:47.117500Z

I'm sorry if I name things to vaguely. I really should get better at naming

2020-12-23T22:49:53.118900Z

TIL about rrb-vector, which worked fine for me in part 1 and broke in part 2 for some reason..ย  Well, I'm quite content with ~20s running time using int vector for storage. https://github.com/next-mad-hatter/adventofcode/blob/master/src/aoc_2020/day_23.clj

euccastro 2020-12-24T00:32:57.121200Z

so I got envious of all your sub-half-an-hour solutions using linked lists and I tried a solution myself. it seems very simple and runs in about 10 seconds (I did no profiling to see whether it can be further optimized yet): https://github.com/euccastro/advent-of-code-2020/blob/8d89563a8909dc1865c2108470b4a928a3394dd1/src/advent/day23.clj#L94

๐ŸŽ‰ 1
euccastro 2020-12-24T00:38:30.121500Z

and this little optimization cut it to under 3sec: https://github.com/euccastro/advent-of-code-2020/commit/4b48551440405efa8de1eef8010aba64ad836d6b

euccastro 2020-12-24T00:53:31.123200Z

ha, I see I ended with essentially the same solution as @misha ๐Ÿ™‚

1
genmeblog 2020-12-24T14:52:07.142800Z

I hate it, 2 days and 1s by mutating int-array https://github.com/genmeblog/advent-of-code/blob/master/src/advent_of_code_2020/day23.clj

๐Ÿš€ 1
๐ŸŽ‰ 2
2020-12-24T15:04:53.143200Z

You redefined the term "persistence". Congratz.

1
holymackerels 2020-12-23T05:47:23.084Z

I'm about a week behind, trying to play catch up, but I managed to get my day 15 solution down to ~1.7s, my first solution took like 5 minutes

๐Ÿ‘ 1
holymackerels 2020-12-23T05:48:50.084100Z

ah I found the thread ๐Ÿ‘€

2020-12-23T06:13:23.085800Z

That puzzle should have been named โ€œCrab, unchainedโ€

โ›“๏ธ 1
๐Ÿฆ€ 2
rjray 2020-12-23T08:04:30.087900Z

I'm getting the wrong answer for the test input on part 2. Ugh.

2020-12-23T08:05:42.088400Z

My bug was that I was doing 1 million rounds instead of 10 millions.

2020-12-23T08:06:08.088600Z

I should repeat to myself: RTFM !

rjray 2020-12-23T08:09:47.088900Z

That's OK. I just got a wrong answer because I submitted the answer from the test data instead of the real data.

๐ŸŽ‰ 1
euccastro 2020-12-23T09:24:41.089700Z

OK, with a simplish array-based implementation I got it down to 5 days. I guess I need to reduce copying...

euccastro 2020-12-23T09:25:48.089900Z

and figure out how to hint stuff for reflection and boxed math

euccastro 2020-12-23T09:25:52.090100Z

might not happen today

erwinrooijakkers 2020-12-23T09:50:02.090600Z

Mine runs in 925 days in time for Christmas 2023

erwinrooijakkers 2020-12-23T09:50:42.090800Z

Will look for some help :)

erwinrooijakkers 2020-12-23T09:51:30.091Z

I see that the use of a LinkedList would help, but not sure what to store where. This evening

euccastro 2020-12-23T10:11:37.091200Z

https://github.com/ptaoussanis/tufte helped me narrow the performance bottleneck down to how I'm searching for the index of the destination cup. a simple improvement to that got me down to one day

euccastro 2020-12-23T10:21:25.091700Z

ok, got it down to 45 minutes, which is close to what I was expecting. I'll just let it run at this point...

2020-12-23T10:22:53.091900Z

Iโ€™m done, but the part 2 was running a few minutes (~3). It was hard to implement and my code is dirty and full of side effectsโ€ฆ

2020-12-23T10:24:01.092100Z

I came up with this

(defprotocol ICircleNode
  (get-next [this])
  (get-val [this])
  (insert-after [this node])
  (remove-after [this]))

๐Ÿ‘ 2
euccastro 2020-12-23T10:37:16.092400Z

I suppose you have them indexed by val?

2020-12-23T11:02:23.092600Z

In French, a linked list is called a "chained list"

2020-12-23T11:02:53.092800Z

I failed my pun about the crab, used the wrong word.

euccastro 2020-12-23T11:13:53.093Z

haha, after 45 minutes waiting I realized that I had fed it the demo input! at least I know it works now ๐Ÿ˜›

euccastro 2020-12-23T11:20:52.093200Z

since I learned programming on my own from sources in English I don't know most technical terms in my own language ๐Ÿ™‚

2020-12-23T11:20:56.093400Z

@euccastro yes, I also have a map for that

๐Ÿ‘ 1
2020-12-23T17:46:33.107900Z

Here is my solution, I wrote it today morning, but polished only now. It runs under 2 minutes for the part 2. https://github.com/zelark/AoC-2020/blob/master/src/zelark/aoc_2020/day_23.clj

๐ŸŽ‰ 1
2020-12-23T18:42:24.109800Z

Also built a fast implementation with deftype and the same protocol. It runs under 20 secs. https://github.com/zelark/AoC-2020/commit/4da8a0b8f6086a5cd552e753a5881edac6798e43

2020-12-24T06:35:36.131200Z

Haha, just noticed it was a wrong thread.

๐Ÿ˜… 1
euccastro 2020-12-23T06:52:38.086200Z

my naive solution for part 2 will take 7 seconds to iterate through 10 steps, so I'll give my dog a 7 million second walk

๐Ÿ˜… 1
2020-12-23T06:53:34.086500Z

I did not even try the naive solution.

2020-12-23T06:53:50.086700Z

I go for the fast way, but itโ€™s complicated to implement.

โž• 1
2020-12-23T06:54:02.086900Z

Many off-by-one issues to deal with.

euccastro 2020-12-23T06:54:08.087100Z

well, I had a step function from part 1, so might as well

euccastro 2020-12-23T06:54:25.087300Z

it would take 81 days to finish ๐Ÿ˜„

2020-12-23T07:21:34.087500Z

I finished the impl, but I have a bug somewhere.

2020-12-23T07:59:00.087700Z

DONE ^_^

misha 2020-12-23T15:02:28.099100Z

if someone still hopes to brute force part2, here is a hint:

uneman 2020-12-26T05:23:54.184900Z

Here is an immutable version of it ;) https://github.com/namenu/data.deque

misha 2020-12-26T07:39:03.187500Z

Niiice

misha 2020-12-23T15:02:34.099200Z

(:import [java.util Deque ArrayDeque]))

2020-12-23T15:04:25.100300Z

How fast is it?

misha 2020-12-23T15:10:06.100500Z

no idea yet :d: but faster than vectors and lists ... I think

2020-12-23T16:02:03.104Z

How did you solve it then?

2020-12-23T16:02:38.104200Z

I mean what approach did you choose?

misha 2020-12-23T16:17:33.104500Z

I did not yet, just got to it. The Deque was handy in aoc 2018, so I quickly solved p1 with it, but then it turned out to be misfit for p2

2020-12-23T17:55:07.108600Z

About Day 20: โ€œI might be able to improve this code, but I have no desire to look at it again any time soon.โ€ Found it in oneโ€™s notes.

๐Ÿ˜‚ 4
rjray 2020-12-23T18:27:50.109500Z

I resemble that remark ๐Ÿ™‚.

๐Ÿ’ฏ 1
2020-12-23T19:24:46.110300Z

Day 24 answers thread - Post your answers here

euccastro 2020-12-24T08:35:37.135Z

@nbardiuk now I feel silly for using drop and first to pick an element by index in a lazy sequence. somehow I though nth didn't work on those (?)

๐Ÿ™‚ 1
2020-12-24T09:01:15.135800Z

A bit revisited my solution after @nbardiuk posted his https://github.com/zelark/AoC-2020/commit/61d33f71a38d146f74661ee497f13ead4fb1c553

๐Ÿ‘ 2
2020-12-24T09:02:48.136Z

@euccastro I believe Itโ€™s a matter of taste here. I prefer drop and first

2020-12-24T09:03:11.136200Z

drop and first works well with ->>, while nth needs ->

erwinrooijakkers 2020-12-24T09:47:25.137500Z

I used this one https://www.redblobgames.com/grids/hexagons/#coordinates-doubled and brute forced inefficiently by looping over all the points and their neighbours, and then finding their neighbours again. The answer does print after some minutes: https://github.com/transducer/adventofcode/blob/master/src/adventofcode/2020/day24.clj

๐ŸŽ‰ 4
2020-12-24T09:54:44.138Z

@erwinrooijakkers apparently I used the same coordinates

benoit 2020-12-24T12:30:24.139600Z

Using coordinate system as well https://github.com/benfle/advent-of-code-2020/blob/main/day24.clj

๐ŸŽ‰ 2
benoit 2020-12-24T12:31:28.140Z

A little differently.

rjray 2020-12-24T17:41:03.145400Z

Once again, I learn much from zelark's solution ๐Ÿ™‚.

1
2020-12-24T18:37:58.147300Z

@rjrayย Also look at nbardiukโ€™s solution I borrowed a couple things from it.

rjray 2020-12-24T21:00:56.148400Z

When this is over, I plan to link to several of the other Clojure repos (in my README.md), for future reference.

nbardiuk 2020-12-24T21:03:40.149100Z

We slowly converge to similar code by learning from each other, nothing original in my solution ๐Ÿ˜

Andrew Byala 2020-12-25T03:11:53.149700Z

Thank you to everyone for sharing your solutions - I'm learning a ton! https://github.com/abyala/advent-2020-clojure/blob/master/src/advent_2020_clojure/day24.clj.

Andrew Byala 2020-12-25T03:12:37.150Z

@nbardiuk - I love your step function. I wouldn't have thought of for -> :when to construct the next set of black tiles.

โค๏ธ 1
โž• 1
misha 2020-12-23T19:29:58.110800Z

projected time: 22minutes :harold:

misha 2020-12-23T19:39:44.111200Z

"Elapsed time: 912366.338705 msecs", 15 minutes, with array as a continer.

๐Ÿ‘ 1
misha 2020-12-23T19:51:15.111900Z

"Elapsed time: 28510.814642 msecs", 28 seconds, with transient map lol

misha 2020-12-23T19:59:07.112500Z

"Elapsed time: 13782.338101 msecs", 13 seconds with java.util.HashMap

misha 2020-12-23T20:19:37.113600Z

"Elapsed time: 6754.597334 msecs", 7 seconds, with typehints for int-array. so that was 14 minutes and 47 seconds of java reflexion, and 13 seconds of actual work, lol

5
๐Ÿ˜ฎ 1
Average-user 2020-12-23T22:18:20.118200Z

I'm surprised by the lack of intcode this year. Maybe tomorrow?

rjray 2020-12-23T23:36:05.120500Z

How odd. I tried re-writing my algorithm to use int-array as @misha does in the 7-second version. It's taking several times longer to run than my vector-based original. I must have something subtle that's wrong in it.

rjray 2020-12-23T23:52:07.121100Z

Well, that's an improvement. I went from 32 seconds to 26 minutes.