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
2017-12-20T01:32:45.000209Z

I never knew you could use case with a list! That’s useful for sure

2017-12-20T03:12:03.000130Z

adjusted mine to use my new knowledge.. list-condition case and trying out cond->

mikelis 2017-12-20T04:59:53.000117Z

Wow, that’s interesting behavior with get-in and cljs. I wonder if that’s “undefined behavior” territory or a bug

dyankowsky 2017-12-20T05:20:58.000066Z

I'm not sure how I feel about my solution to tonight's problem

dyankowsky 2017-12-20T05:21:37.000048Z

I though about it and came up with two probablistically correct answers, and got the right answer in both cases, but I don't feel like I "solved" the problem

dyankowsky 2017-12-20T05:25:24.000079Z

but hey, I finally got points on the global leaderboard, so I guess I'll take that 🙂

2017-12-20T05:36:15.000034Z

congrats! bit stumped on this one

dpsutton 2017-12-20T05:36:34.000132Z

for day 13 did anyone do a sieve of eratosthenes type solution?

mfikes 2017-12-20T06:19:44.000114Z

@dpsutton I didn’t see one in Clojure, but Kris Jenkins appeared to do so: https://github.com/krisajenkins/AdventOfCode/blob/master/src/Year2017/Day13.purs#L44

dpsutton 2017-12-20T06:21:36.000082Z

ah cool. i've been slacking and catching up. it's quite elegant

dpsutton 2017-12-20T06:21:43.000133Z

and fast. 1 sec versus 10 for me

dpsutton 2017-12-20T06:22:06.000170Z

(comment
  (time (solve2))
  ;; "Elapsed time: 10507.820736 msecs"
  ;; 3870382
  (dotimes [_ 20] (time (sieve-method data/data)))
  ;; "Elapsed time: 1293.195798 msecs"
  ;; 3870382
  )

dpsutton 2017-12-20T06:23:03.000261Z

i've ripped off your style of multi arity (solve1) and the data/data namespaces. thanks for that

2017-12-20T06:32:15.000187Z

man i have no capacity for visualizing this problem 😆

grzm 2017-12-20T06:32:18.000041Z

@mfikes I'm not sure I'll be satisfied with your solution unless you also provide a 3D hologram displaying the collisions.

grzm 2017-12-20T06:33:28.000003Z

Help me, Mike Fikes Kenobi. You're our only hope! 😉

grzm 2017-12-20T06:34:33.000065Z

@minikomi Have you gotten part-1?

2017-12-20T06:34:44.000018Z

nope, maybe i'm overthinking it

2017-12-20T06:34:55.000140Z

trying to weed out the particles moving & accelerating away from the origin..

mfikes 2017-12-20T06:38:46.000003Z

I’m not satisfied with my day 20 solution either @grzm — I had to fall back on what the AoC game allows 😞

grzm 2017-12-20T06:40:34.000090Z

I hope it's clear that's a friendly jab 😉 Your solutions have really inspired me.

mfikes 2017-12-20T06:40:37.000083Z

This problem definitely has a tradeoff of overthinking vs. pragmatism.

2017-12-20T06:59:20.000001Z

wow

2017-12-20T06:59:28.000001Z

wayyyy overthought it hahaha

dyankowsky 2017-12-20T06:59:28.000080Z

get it?

dyankowsky 2017-12-20T06:59:30.000094Z

heh

2017-12-20T07:10:35.000005Z

lol..

2017-12-20T07:10:49.000027Z

well, it's done. but.. what a squiggy way to solve it

2017-12-20T07:11:52.000057Z

I wonder if there's a way to fine-tune cider output .. or have it print to a terminal. Sometimes it gets reaaaaly slow.

mikelis 2017-12-20T07:51:11.000087Z

today marks my biggest facepalm moment so far.. Took me almost 3 hours and writing a quadratic equation solver to realize that the velocities need to be updated before the positions

erwin 2017-12-20T09:22:07.000105Z

I have a solution for today, but I am not sure if it is valid for all possible inputs ...

ihabunek 2017-12-20T10:51:29.000248Z

my solution: https://github.com/ihabunek/aoc2017/blob/master/src/aoc2017/day20.clj

ihabunek 2017-12-20T10:51:34.000382Z

SPOILERS

ihabunek 2017-12-20T10:51:45.000293Z

i have a nice solution for part 1

ihabunek 2017-12-20T10:52:09.000085Z

particles with the greatest accelleration will end up furthest away

ihabunek 2017-12-20T10:52:19.000133Z

if they have the same accelleration, then those with greatest speed

ihabunek 2017-12-20T10:52:31.000088Z

if they have the same speed, then those which are initially furthers away

ihabunek 2017-12-20T10:52:49.000285Z

so if you sort all particles by absolute accelleration, then speed, then position from center

ihabunek 2017-12-20T10:53:02.000372Z

and take the first one, that's the solution

ihabunek 2017-12-20T11:18:12.000007Z

heh, just saw that @erwin does the same

mfikes 2017-12-20T12:50:27.000348Z

Very nice. I was worried that this kind of solution might be susceptible to them setting up a slow moving constant-velocity particle that is far away but would cross near the origin. But I suppose, by the problem definition, even though that one might be close temporarily at some point far out in the future, over the long run, it doesn’t meet the definition. Analogy: That extra-solar body that just passed by, but was, for a short while closer than most of our planets.

mfikes 2017-12-20T12:54:14.000283Z

I think your approach is fairly rigorous though.

mfikes 2017-12-20T12:55:38.000192Z

Perhaps there is a similar argument to be made for collisions if you somehow compare the inter-particle accelerations, velocities, and positions to know you are done detecting collisions. Quadratic cost.

bhauman 2017-12-20T13:57:43.000413Z

what format is the answer supposed to be in?

bhauman 2017-12-20T13:58:12.000100Z

for part 1

mikelis 2017-12-20T14:03:13.000397Z

index of the particle

bhauman 2017-12-20T14:03:31.000260Z

oh gosh did they make that clear at all?

mikelis 2017-12-20T14:04:51.000118Z

usually the expected answers are monospaced with a background

bhauman 2017-12-20T14:04:52.000171Z

i thought that was a temp label

bhauman 2017-12-20T14:05:01.000096Z

gotcha

mikelis 2017-12-20T14:05:29.000449Z

Picking 0 as the sample answer was perhaps not the best choice :simple_smile:

mfikes 2017-12-20T14:27:17.000584Z

IMHO, they could have improved that aspect of the problem definition. (I say "they" assuming that Eric Wastl has play testers. Who knows?)

mfikes 2017-12-20T14:47:06.000511Z

By the way, if my solutions tend to look inexplicably transducer heavy (without justification), it is because I'm working on keeping memory usage down when solving the AoC problems in ClojureScript. Background: ClojureScript doesn't have locals clearing. See http://blog.fikesfarm.com/posts/2016-01-15-clojurescript-head-holding.html With transducers you can sometimes work around this limitation. I'm working on https://dev.clojure.org/jira/browse/CLJS-2445, and for many of these problems (which involve reducing over the results of iterate) they become solvable in ClojureScript with a few MB whereas previously you can easily consume several GB.

mfikes 2017-12-20T14:54:13.000564Z

A great example is this line, which is solved in an updated version of Planck in 155 MB (running CLJS-2445 changes), while the unchanged Planck can't effectively solve it unless it is tediously converted to a loop / recur: https://github.com/mfikes/advent-of-code/blob/a440f0ea8326f7ed2eea71270fbc7d4e6d0df1c1/src/advent_2017/day_17.cljc#L24

dyankowsky 2017-12-20T14:58:42.000315Z

My reading of the problem was, in the limit, which particle ends up closest to the origin

mfikes 2017-12-20T15:00:22.000417Z

Actually, the unrevised Planck can run that code, but it takes 14 GB and about 2 minutes, as opposed to 155 MB and 7 seconds. A loop / recur is still faster for that arithmetic-centric problem, running in about 1 s in Planck, but I'd like to be able to work at the higher level of abstraction if it leads to reasonable results.

dyankowsky 2017-12-20T15:00:23.000590Z

I also cheated even more; I looked at my dataset and saw that there were no ties for smallest acceleration

dyankowsky 2017-12-20T15:03:47.000355Z

for the collisions, I think you could do something like iterate until enough acceleration has accumulated into velocity that the initial velocity is negligible, and until enough velocity has accumulated into position that the initial position is negligible

dyankowsky 2017-12-20T15:04:01.000487Z

that is, in the limit, the acceleration is all that matters - it will completely dominate the other two

dyankowsky 2017-12-20T15:06:03.000403Z

I think there has to be some way that you could look at each particle in isolation and determine whether this has happened (within some tolerance) and, when that has happened, you know that any collisions that would happen would have already happened

mfikes 2017-12-20T15:06:57.000244Z

Wow, is physics / calculus sufficient for this problem?

mfikes 2017-12-20T15:07:58.000053Z

I suppose it is a good enough approximation, given you have to approximate anyways.

mfikes 2017-12-20T15:09:37.000086Z

And, using the distance formula leads to a very fast solution. Nice @bhauman

dyankowsky 2017-12-20T15:11:18.000393Z

maybe a better way to put it is that, while your particles all start out at various positions around the origin, going in different arcs, eventually those arcs become nearly straight lines as the accumulated acceleration dominates the velocity. And as time ticks on and everything gets further from the origin, you can increasingly approximate your system as "a bunch of lines all shooting out from the origin". Sure, they didn't all start at the origin, but once they get far enough away, it's sufficient to assume that they all started in the same place.

bhauman 2017-12-20T15:11:23.000273Z

its only good enough for the first part

mfikes 2017-12-20T15:11:31.000544Z

Ahh... good point

bhauman 2017-12-20T15:12:07.000396Z

the second part relies on their slot based interpretation of time

mfikes 2017-12-20T15:12:08.000341Z

The second part seems to converge more quickly for me

mfikes 2017-12-20T15:13:08.000659Z

I still think a lot of participants are going to end up feeling unsatisfied by this problem, even though, logically, all you need to do to solve it is type in an answer that it accepts.

mfikes 2017-12-20T15:13:59.000505Z

In other words, logically, anything you can do that leads you to the right guess is perfectly fine.

dyankowsky 2017-12-20T15:14:21.000060Z

that's perhaps one of the more interesting things about AoC - it's not about code per se, but all the problems are problems where machine assistance is extremely useful

mfikes 2017-12-20T15:14:40.000681Z

And, if you go down that path, I like @bhauman’s transformation to continuous math as opposed to discrete

bhauman 2017-12-20T15:15:44.000003Z

I spent some time "remembering" (i.e googling) that math

mfikes 2017-12-20T15:16:43.000257Z

Yeah, @bytecrafter part 2 of day 7 seemed to be initially solved by many participants half via code with the "last mile" by hand, then backfilling with the correct code

mfikes 2017-12-20T15:17:16.000730Z

That one was strange in that it was actually easier to do it by hand than to write the code to do the needed calculations.

dyankowsky 2017-12-20T15:17:32.000438Z

yep, I was one of those on day7

mfikes 2017-12-20T15:18:00.000384Z

If you factor in that part of the game is to beat the clock, these kinds of problems are appropriate.

dyankowsky 2017-12-20T15:18:28.000786Z

that's also one of the things I like about doing these in Clojure - even though it's not my most comfortable language, doing stuff in the REPL is great

mfikes 2017-12-20T15:19:03.000181Z

Speaking of that, I liked the 4Clojure aspect where part of the problem was to find a solution that was sufficiently efficient for it to be solved before the 4Clojure timeout kicked in.

dyankowsky 2017-12-20T15:20:12.000317Z

it kills off your process if it runs too long?

dyankowsky 2017-12-20T15:20:23.000298Z

(or request or thread or whatever)

mfikes 2017-12-20T15:20:32.000339Z

Yes. Oftentimes you will have some code that actually solves it, but it takes, say 45 seconds.

mfikes 2017-12-20T15:21:00.000200Z

I don't recall the timeouts, but it may have required your code to run on their server within, say 30 seconds

dyankowsky 2017-12-20T15:21:29.000221Z

interesting

mfikes 2017-12-20T15:22:12.000242Z

If you get really frustrated, you can "cheat" by writing functions that produce the correct answers instantly, once you know what they are.

😄 1
mfikes 2017-12-20T15:22:31.000343Z

But, then you defeat the learning process 🙂

mikelis 2017-12-20T15:50:13.000021Z

Part2 can be solved in a non-iterative way using quadratic equations too

mikelis 2017-12-20T15:51:58.000305Z

two particles collide if 1/2*a1^2 + v1*t + s1 == 1/2*a2^2 + v2*t + s2

mikelis 2017-12-20T15:52:56.000482Z

^that’s for one dimension, where a is acceleration, v is velocity and s is the inital position solve for t and get the time when it happens

mikelis 2017-12-20T15:53:15.000371Z

do that for all 3 dimensions, and if the ts match, the particles collide at that time

dyankowsky 2017-12-20T15:53:51.000654Z

to match the iterative approach, t must also be an integer

mikelis 2017-12-20T15:53:55.000553Z

that’s still O(n^2) though, where n = number of particles

dyankowsky 2017-12-20T15:54:27.000802Z

the iterative approach allows particles to "pass through" each other as long as they do so in the middle of a timestep

mikelis 2017-12-20T15:55:01.000478Z

yep, and also need to account for the fact that velocity is “updated” before position

mikelis 2017-12-20T15:55:25.000655Z

I guess by doing v1*(t+1) instead of v1*t?

dyankowsky 2017-12-20T15:55:39.000637Z

not sure

mikelis 2017-12-20T15:57:42.000682Z

I have an unfinished and not cleaned up approach to it at https://github.com/axelarge/advent-of-code/blob/master/src/advent_of_code/2017/day20.clj#L63-L89 It doesn’t work because it doesn’t account for the velocity thing

borkdude 2017-12-20T16:00:14.000737Z

hmm, day 20, stuck at 1000 particles after 10^3,4,5 iterations..

borkdude 2017-12-20T16:01:05.000474Z

can someone give me a hint after how many iterations you should expect collisions? 1? 10? 1000?

mikelis 2017-12-20T16:02:19.000664Z

100

mikelis 2017-12-20T16:02:35.000790Z

well, the first one start at 10-15 for me

borkdude 2017-12-20T16:02:45.000824Z

ok, then I’m having something wrong then

mikelis 2017-12-20T16:03:19.000230Z

Could it be this? 😅 https://clojurians.slack.com/archives/C0GLTDB2T/p1513756271000087

borkdude 2017-12-20T16:06:05.000369Z

Definitely…. still got part 1 right though… thanks!

borkdude 2017-12-20T16:07:53.000442Z

ah, now they are colliding… phew..

borkdude 2017-12-20T16:08:40.000394Z

aaaaand solved…. 😅

bhauman 2017-12-20T16:40:31.000538Z

@mikelis.vindavs I don't think the equation solution for part 2 will work, I'm pretty sure we'd need a different position equation as

bhauman 2017-12-20T16:41:13.000390Z

oh shoot

mikelis 2017-12-20T16:49:00.000028Z

Now I’m intrigued :simple_smile:

ihabunek 2017-12-20T16:52:43.000411Z

a friend of mine did part 2 quadratic equation solution in elixir, so it's possible https://gist.github.com/sasa1977/a35e87540669f32c28e3bd2b7698ca7e

borkdude 2017-12-20T17:03:28.000143Z

FWIW, my day 20, no fancy math involved: https://github.com/borkdude/aoc2017/blob/master/src/day20.clj

borkdude 2017-12-20T17:05:41.000286Z

I’m looking at @bhauman’s solution and instantly I remember my high school physics 🙂

mfikes 2017-12-20T17:08:57.000645Z

Hah, I happened to have this lying around from a couple of months back when I was trying to explain some stuff to my son

bhauman 2017-12-20T17:22:28.000026Z

HINT

bhauman 2017-12-20T17:23:17.000483Z

the position function will work if you do (Math/ceil (* 0.5 t t))

bhauman 2017-12-20T17:26:14.000588Z

this is for part 2

borkdude 2017-12-20T17:26:26.000347Z

So for part two you could use some algorithm which checks if functions will collide for some interval maybe?

borkdude 2017-12-20T17:28:16.000438Z

Not sure which one 🙂

borkdude 2017-12-20T17:30:34.000627Z

You could solve equations for every pair

borkdude 2017-12-20T17:30:45.000128Z

and if there is no solution, it won’t collide

mfikes 2017-12-20T17:31:31.000815Z

If you get a closed-form solution (like what Bruce said), then take the difference between every pair, set it to 0 and see if beyond a certain t it can no longer be solved?

mfikes 2017-12-20T17:32:27.000766Z

If you can find such a t that works for all pairs...

borkdude 2017-12-20T17:32:30.000040Z

I didn’t read the whole thread. Will do so tonight. Have to make dinner now.

orestis 2017-12-20T17:59:14.000587Z

I did a numerical approach on day 20 as well: https://github.com/orestis/adventofcode/blob/master/clojure/aoc/src/aoc/2017_day20.clj

2017-12-20T18:28:08.000691Z

hey guys... I have had some trouble figuring out what was wrong with my day 5 implementation! can someone tell me if I'm missing something from the following code snippet 😉 P.S : Clojure newbie here ...your eyes may hurt but I have to start with something I guess 🙂

2017-12-20T18:32:24.000185Z

thanks in advance

erwin 2017-12-20T18:39:40.000372Z

@kaffein I think you should end with a (inc steps) on line 14

erwin 2017-12-20T18:40:13.000211Z

the last step is also a step I would say

2017-12-20T18:48:59.000353Z

Thanks @erwin I will try that out ...though the unit test that I've run using the problem example seems to be okay

borkdude 2017-12-20T21:43:50.000236Z

In Postgres: https://github.com/xocolatl/advent-of-code/blob/master/Day20/script.sql

🤘 1
mfikes 2017-12-20T21:45:58.000366Z

It make me feel better that it says "Both of these queries have shameful, arbitrary stopping points"

mfikes 2017-12-20T21:46:28.000269Z

All developers hang their heads in shame for some reason for this problem. 🙂

fellshard 2017-12-20T22:02:05.000099Z

It edges too close into math we're told we should know and don't really want to explore? 😛

fellshard 2017-12-20T22:02:24.000466Z

We'd rather take the approach of 'make the numbers big enough we remove reasonable doubt'

borkdude 2017-12-20T22:08:58.000278Z

When I encountered part 1 for a moment I thought, should I solve this more cleverly using Newton or …. meh, no, in part 2 they are going to ask for something ridiculous anyway, like how many times the sum of the coordinates were prime numbers in the first million runs