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
plexus 2020-12-13T06:05:14.213800Z

Streaming day 13 in a few moments: https://youtu.be/ONclnLu3UqA

πŸ‘ 2
plexus 2020-12-13T08:15:14.218200Z

Recording: https://youtu.be/q-T87Ja2DF8

oxalorg (Mitesh) 2020-12-13T06:24:40.214700Z

Looks like today's 2nd part might need some Modular arithmetic to solve πŸ™ˆ

2020-12-13T06:30:18.215500Z

β€œIn life, there are 92319243542196534129765432175643216314925319 kinds of people … those who can solve the math problems, and the others.”

πŸ˜† 2
nbardiuk 2020-12-13T08:24:05.218700Z

I've given up on brute forcing when it got too warm with laptop on my laps πŸ™‚

fingertoe 2020-12-13T08:51:50.219300Z

Brute force? or remember basic HS math from thirty years ago? Maybe my high schooler is up.. If only I was a CS major instead of MIS.. Maybe it will be debits and credits next?

πŸ˜† 1
2020-12-13T06:30:22.215700Z

Good morning !

2020-12-13T06:30:52.215900Z

Day 13 answers thread - Post your answers here

nbardiuk 2020-12-13T08:18:13.218400Z

this was the hardest for me this year so far https://github.com/nbardiuk/adventofcode/blob/master/2020/src/day13.clj

πŸ‘ 2
2020-12-13T09:18:23.219800Z

I find it fantastic that we found similar, yet different ways to calculate the result. Interesting.

nbardiuk 2020-12-13T09:32:36.221300Z

@vincent.cantin oh, I didn't notice that bus cycles are primes, so I don't need LCM, multiplication is enough, nice catch!

2020-12-13T09:49:40.221700Z

I guess it depends which data you got, mine were all primes

πŸ‘ 1
2020-12-13T10:37:05.222Z

There must be a way to speed up our solutions using https://en.wikipedia.org/wiki/Modular_multiplicative_inverse … the section β€œApplication” is giving the exact math formula to our puzzle.

πŸ‘ 1
joppe 2020-12-13T11:02:43.222500Z

I used the chinese remainders theorem in my solution https://github.com/Yopi/advent-of-code/blob/master/src/adventofcode/2020/day13.clj

πŸ‘ 2
joppe 2020-12-13T11:03:16.222900Z

But maybe it would have been easier to use modular multiplicative inverse straight away πŸ˜„

2020-12-13T13:29:28.224100Z

I implemented the solution from the Wikipedia article on invert-modulo, here is the source with benchmark: https://github.com/green-coder/advent-of-code-2020/blob/master/src/aoc/day_13.clj#L71-L88

πŸ‘ 1
πŸ‘ 2
2020-12-13T13:57:33.226200Z

@brann.jesper Clojure has (clojure.core/long n)

benoit 2020-12-13T14:24:01.228300Z

This one required some research in number theory. https://github.com/benfle/advent-of-code-2020/blob/main/day13.clj

πŸ‘ 1
benoit 2020-12-13T14:26:28.228800Z

@nbardiuk or not πŸ™‚ Trying to understand your solution...

2020-12-13T14:31:42.229Z

@me1740 there is a gcd function in https://github.com/clojure/math.numeric-tower

2020-12-13T14:33:15.229300Z

and there is a .modInverse in the JDK https://clojuredocs.org/clojure.core/bigint#example-59fa5422e4b0a08026c48c8f

πŸŽ‰ 1
benoit 2020-12-13T14:33:47.229600Z

Thanks, looking.

benoit 2020-12-13T14:39:32.229800Z

Ok simplified a bit. Number theory is not my friend.

Joe 2020-12-13T14:56:32.230300Z

Does anyone have a more thorough example of using inverses to solve systems of linear congruences? I can't figure out how you'd generalize the formula on the applications section of https://en.wikipedia.org/wiki/Modular_multiplicative_inverse to anything other than 3 equations...

benoit 2020-12-13T15:00:32.231100Z

@nbardiuk Nice solution, I didn't realize that each partial solution was also cyclic and you could progress towards the solution for all buses like this.

nbardiuk 2020-12-13T15:09:10.231600Z

Yes, the partial solution repeats with least common multiple of bus cycles. As Vincent noticed all busses have prime cycle so lcm becomes just multiplication

πŸ‘ 1
Joe 2020-12-13T15:51:44.232100Z

@me1740 thanks, that was so much easier to understand

πŸ‘ 1
Joe 2020-12-13T16:37:36.232900Z

Pretty easy after you've spent 6 hours reading about modular arithmetic 😡

(defn- linear-congruence-solve [pairs]
  (let [remainders (map first pairs)
        modulos (map second pairs)
        prod (apply * modulos)
        pp (map #(/ prod %) modulos)
        inv (map #(.modInverse (biginteger %1) (biginteger %2)) pp modulos)]
    (mod (apply + (map * remainders pp inv)) prod)))

πŸ‘ 2
πŸš€ 1
1
Joe 2020-12-13T17:22:13.233400Z

A https://redpenguin101.github.io/posts/2020_12_13_mod_mult.html I took on the math in case they will be helpful for anyone else.

πŸ‘ 2
❀️ 2
genmeblog 2020-12-14T00:36:29.251400Z

So much energy went to figure out such short solution (0.5ms execution time) for part 2: https://github.com/genmeblog/advent-of-code/blob/master/src/advent_of_code_2020/day13.clj#L34-L44

oxalorg (Mitesh) 2020-12-13T06:33:30.216200Z

I thought maybe today they want us to brute force it and I waited for 20 minutes for the brute force to complete before giving up πŸ™ˆ

2020-12-13T06:37:33.216500Z

I tried brute forcing, but then I read the text again and I stopped torturing my 2011 mac book air.

πŸ˜† 1
2020-12-13T09:21:11.221100Z

I think that those numbers large enough to fear a math overflow are a good hint that we need to be prepared to use https://clojuredocs.org/clojure.core/bigint and its friends.

pez 2020-12-13T13:27:16.224Z

How long will a really naΓ―ve solution take for step 2? Asking for a friend.

3
pez 2020-12-14T12:58:25.262800Z

Haha, I took it quit literally and it guided me to my gold star, which isn’t always easy to find on overcast days.

πŸ™‚ 1
2020-12-13T13:45:32.225300Z

Tell your friend that his chances will be better if he tries the β€œleap of faith” approach:

(-> (for [x (repeatedly (long (* (rand) Long/MAX_VALUE)))
          :when (valid? x)]
      x)
    first)

pez 2020-12-13T13:53:33.225800Z

My friends computer is getting really hot.

1
plexus 2020-12-13T13:53:43.226Z

So my input had 9 bus ids, and up to 6 or 7 my initial approach would still run within a few seconds, but after that it really blew up

2020-12-13T14:02:36.226400Z

@pez It was not meant to be solved this way. https://moddr.net/uploads/2008/10/computer-bbq.jpg

pez 2020-12-13T14:05:12.226700Z

Yeah, I must find a better approach. That’s what friends are for, right?

2020-12-13T14:06:35.227Z

[spoilers alert] I can give your friend a hint: find a way to reduce the constraints by combining them.

pez 2020-12-13T14:06:47.227300Z

❀️

2020-12-13T14:07:29.227500Z

[spoilers alert] work on a pair of them, see what you can do to make them one

pez 2020-12-13T14:08:15.227700Z

Will do. Thanks!

erwinrooijakkers 2020-12-13T14:50:02.230100Z

If your computation for every step takes a millisecond and you have a trillion operations it will take more than 31 years to return

thom 2020-12-13T18:49:47.237500Z

Only just found this channel and running a bit behind, but I've been putting up marginalia versions of my solutions at: https://lemonwatcher.com/advent.html

πŸ‘ 3
😍 1
thom 2020-12-13T18:49:55.237900Z

apologies for any and all crimes against Clojure

2020-12-13T18:58:26.240400Z

Bit stuck on part 2 today, not sure what I'm missing: Given the busses in positions

19 is pos 0
41 is pos 9
37 is pos 13
367 is pos 19
13 is pos 32
17 is pos 36
29 is pos 48
373 is pos 50
23 is pos 73
I have calculated the answer to the problem I think I've to solve as :
561773853149685
Because
(mod 561773853149685 19)
=> 0

(mod 561773853149685 41)
=> 32 (41 - 9 = 32)

(mod 561773853149685 37)
=> 24 (37 - 13 = 24)

(mod 561773853149685 367)
=> 348 (367 - 19 = 348) as 367 is in the 19th pos.

(mod 561773853149685 13)
=> 7 (13-32 mod 13 = 7) as 13 is in the 32nd pos

(mod 561773853149685 17)
=> 15 (17 - 36 mod 17) as 17 is in the 36th pos.

(mod 561773853149685 29)
=> 10 (29 - 48 mod 29)

(mod 561773853149685 373)
=> 273 (373 - 50)

(mod 561773853149685 23)
=> 19 (23 - 73 mod 23)
What am I misunderstanding?

2020-12-13T19:17:34.241Z

I think my entire idea on how I can solve this doesnt work at all. damn

pez 2020-12-13T20:52:37.241400Z

I managed to trial and error my way to a solution with @vincent.cantin’s hints. It runs in slightly less than 31 years. Now I am quite wasted. Not looking forward to tomorrow, even.

1
1
πŸ™ 1
πŸŽ‰ 1
ryan echternacht 2020-12-13T21:25:58.242500Z

it recommends starting at 15 places (100000000000000). Is this a good starting place?

ryan echternacht 2020-12-13T21:26:21.243100Z

and is there some smart math form of this (using LCM?). I couldn't figure one out given the offsets

2020-12-31T18:18:44.245700Z

Agreed with the above. There are times when the β€œmath puzzle” piece is interesting, but at least for me, personally, I’m trying to build up my Clojure skills through this exercise. I managed to Google my way to the Chinese remainder theorem (and this thread), and happy to see others are in the same boat. This is one case where I was happy to copy/paste some code. πŸ˜† https://github.com/jeff303/advent-of-code-2020/blob/master/src/advent_of_code/day13.clj#L80

erwinrooijakkers 2020-12-13T21:30:43.243700Z

It’s literally the Chinese remainder theorem

erwinrooijakkers 2020-12-13T21:31:58.243900Z

Using LCM I don’t know

ryan echternacht 2020-12-13T21:33:01.244100Z

oh, @vincent.cantin are you saying. find the "step size" that the first 2 numbers must be apart to line up. then combine that with the 3rd number to find a bigger step count, then ...

Joe 2020-12-13T21:40:50.245500Z

@ryan072 check out the day 13 answers thread - plenty of advice in there.

2020-12-13T21:50:52.247200Z

Well, all my buses are primes. So the least common multiplier between any 2 buses is just bus 1 * bus 2

pez 2020-12-13T22:03:54.247400Z

I think that’s what he said. πŸ˜ƒ My reductions look like so:

({:bus 29, :offset 0}
 {:bus 1073, :offset 754}
 {:bus 438857, :offset 379523}
 {:bus 7460569, :offset 5645807}
 {:bus 96987397, :offset 50409221}
 {:bus 1842760543, :offset 923295794}
 {:bus 42383492489, :offset 32250225025}
 {:bus 14961372848617, :offset 4312982966414}
 {:bus 613416286793297, :offset 408270049879073})

2020-12-13T22:16:35.248Z

finally! Got a solution that works for part 2. I had 12 failed attempts at this one!

🀘 6
ryan echternacht 2020-12-13T23:12:54.248600Z

Thanks. I got it figured out

ryan echternacht 2020-12-13T23:16:40.248700Z

Yeah, I finally got there too

🀘 2
rfisher 2020-12-13T23:26:07.248900Z

I enjoy these when they're programming puzzles. It's not so much fun when I have to try (and fail) to independently work out some mathematical theorem I've never heard of.

rfisher 2020-12-13T23:26:22.249100Z

That's not a moan, other than at myself for never learning mathematics πŸ™‚