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-11T02:00:27.000126Z

Nice weekend everyone? 😄

2017-12-11T02:24:12.000095Z

got day9.. should have used a map and not vars for the loop/recur

grzm 2017-12-11T02:27:26.000098Z

too bad you can't change it 🙂 (git records all of our sins)

dpsutton 2017-12-11T02:39:02.000123Z

force push to the rescue

2017-12-11T02:39:33.000038Z

lol. i like having the history of my refinements for AoC

👍 2
mfikes 2017-12-11T02:44:11.000014Z

@minikomi Nice solution! Your throw may make things more robust, but if the input is valid, I think you should be able to take the three cases \, \newline, and \! and eliminate them, if you take the result-expr for \, and use it instead of the (throw ...).

2017-12-11T02:45:04.000018Z

yeah, i just had it there to refine my solution.. catch (ha!) the times it went awry.

mfikes 2017-12-11T02:45:37.000011Z

Instead of trying to say it in words, this variation on your solution works for me: https://gist.github.com/mfikes/57c1b666c30bb5fd98b235df87b5c189

2017-12-11T02:46:32.000141Z

oh, is cancel guaranteed to only be in garbage? that's good then!

mfikes 2017-12-11T02:47:03.000041Z

Right, I think you can interpret the problem definition that way. And it seems to be borne out by the input data.

2017-12-11T02:47:09.000140Z

cool

2017-12-11T02:52:11.000203Z

there 🙂 credited you in the commit heh

2017-12-11T04:32:32.000084Z

man this wording for day 10 part 2 is confusing.

2017-12-11T04:33:36.000026Z

If the suffix is added, there's 8 3, 4, 1, 5, 17, 31, 73, 47, 23 as the steps.. but the skip sequence carried over is 4 ?

2017-12-11T04:33:56.000126Z

rather than 9, since 9 steps have occured..?

2017-12-11T04:49:21.000061Z

oh, ok i got it.

2017-12-11T05:30:31.000199Z

reductions 💌

👌 2
fellshard 2017-12-11T05:37:57.000090Z

I've gotta search for the number of times part 2 has been reduce -> reductions

dyankowsky 2017-12-11T05:40:15.000088Z

I've used Clojure for all three years, and I feel like some of the earlier problems are easier to solve in a procedural style, but I feel like functional languages really start to shine after a few days.

dyankowsky 2017-12-11T05:41:04.000215Z

Though the hash cracking one from... last year?... was a bit tough... I had to learn more about Java interop to make it fast

fellshard 2017-12-11T05:42:24.000029Z

I'd love to go through and solve these with different paradigms, hone a sense for what gets easier and what gets harder

fellshard 2017-12-11T05:44:27.000054Z

Also had Proto Repl finally set up on my laptop for this round 😄

mikelis 2017-12-11T06:28:17.000142Z

@fellshard same here, but right now trying to decide between “pretty” with reduce/`reductions` vs 30x faster with apply

2017-12-11T06:38:24.000123Z

(apply str (map #(format "%02x" %) xored)) Was pretty happy wit that hexidecimal-string-conversion 😄

2017-12-11T06:38:40.000208Z

format is such a beast

orestis 2017-12-11T09:33:26.000353Z

Hi all! @fellshard Indeed, “coda” is a musical term; It literally means “tail” in Italian. It refers to the bars that come after a repeating part. I don’t know how it popped into my mind, but I like it better than “tail” which can be confusing when destructuring linked lists.

orestis 2017-12-11T09:34:37.000005Z

Today I didn’t have much time to arrive to the solution from first principles; I had to lookup hex grids and how they work.

orestis 2017-12-11T09:36:19.000105Z

I’ve included a link to a nice article in my solution code.

borkdude 2017-12-11T10:01:29.000021Z

Day 11 is a bit difficult for me

borkdude 2017-12-11T10:01:49.000213Z

I’ll peek at @orestis's link

borkdude 2017-12-11T10:53:03.000118Z

Anyone who wants a SPOILER or hint, peek in this thread

karlis 2017-12-11T12:06:19.000294Z

my solution for today: https://github.com/skazhy/advent/blob/master/src/advent/2017/day11.clj with a handy ascii art on how the coordinates are handled.

mikelis 2017-12-11T12:11:55.000432Z

In my opinion, today’s challenge lent itself well to one of the most elegant & concise solutions this year so far https://github.com/axelarge/advent-of-code/blob/master/src/advent_of_code/2017/day11.clj

👏 5
orestis 2017-12-11T12:38:57.000435Z

@mikelis.vindavs That is indeed very nice.

1
borkdude 2017-12-11T13:05:20.000283Z

Did anyone else get emotional while reading > “It’s my child process,” she says, “he’s gotten lost in an infinite grid!”

2017-12-11T13:07:30.000044Z

https://repl.it/repls/BoldAzureCuscus here’s mine

2017-12-11T13:09:41.000451Z

cube coordinates everywhere 😄

borkdude 2017-12-11T13:09:45.000477Z

@mfikes isomorphic once again

borkdude 2017-12-11T13:10:11.000162Z

I have to admit without the hint about cube coordinates I would still be thinking right now

mfikes 2017-12-11T13:10:52.000353Z

Me too. I did some quick research and chose that coordinate system because it makes calculating distance trivial.

borkdude 2017-12-11T13:11:45.000043Z

The photo in the spoiler thread above did it for me

2017-12-11T13:12:08.000330Z

I’m wrote a game on hex grid once so it was a no-brainer

erwin 2017-12-11T13:24:06.000169Z

I solved it without cube coordinates, by using the simplified versions of de diagonals for north and south. So NE + NW = (1,1) + (1,-1) = (2,0). Calculation is simple after that, similar to using a Z-axis.

borkdude 2017-12-11T13:25:08.000213Z

@erwin link?

mikelis 2017-12-11T13:27:36.000306Z

nice approach with the input resolving directly to coordinate tuples 👍:skin-tone-2:

👍 2
borkdude 2017-12-11T13:29:40.000067Z

@erwin nice!

borkdude 2017-12-11T13:30:08.000132Z

it seems so simple once you see it

erwin 2017-12-11T13:32:49.000189Z

today was fast to compensate for yesterday, which was very slow for me 😁

2017-12-11T13:35:48.000131Z

input/input

2017-12-11T13:35:49.000262Z

?

2017-12-11T13:36:34.000029Z

woo, i forgot that reductions exists

erwin 2017-12-11T13:37:49.000343Z

oow, still a defect? wrote the solution in a repl / nightcode at work and pasted it in github in one file. Let me fix that.

mfikes 2017-12-11T13:39:01.000293Z

There is an interesting perf anomaly with @erwin's solve-2 where it runs in 124 ms for me in Clojure vs. 15 ms in self-hosted ClojureScript. Hrm.

borkdude 2017-12-11T13:40:11.000515Z

@mfikes sanity check: do they yield the same outcome?

mfikes 2017-12-11T13:42:29.000516Z

Yes, 1493

mfikes 2017-12-11T13:49:39.000247Z

This was the page I hit for the research I did. It has incredible visualizations of the coordinate systems. https://www.redblobgames.com/grids/hexagons/

borkdude 2017-12-11T13:50:34.000582Z

I hit that one too and then I tried the q layouts, but had problems calculating the distance

mfikes 2017-12-11T13:51:34.000231Z

I scrolled down to the Distances section and saw that Cube coordinates has a nice formula.

borkdude 2017-12-11T13:57:32.000590Z

yeah, that’s the same system as on the photograph, but I was nearly awake then 😉

bhauman 2017-12-11T14:00:56.000612Z

ok just finished, I spent some time on paper trying to devine the properties of the system first

bhauman 2017-12-11T14:01:06.000529Z

now to see what others did

borkdude 2017-12-11T14:02:53.000411Z

+1 for the Christmas ascii art @bhauman

bhauman 2017-12-11T14:03:39.000302Z

@borkdude thats from the first advent of code

bhauman 2017-12-11T14:05:26.000372Z

oh I see others did this as well, congrats @mikelis.vindavs!!

bhauman 2017-12-11T14:06:58.000326Z

and @erwin

bhauman 2017-12-11T14:07:59.000638Z

oh wait yours is different but I think logically equivalent

mfikes 2017-12-11T14:08:52.000261Z

Interesting. @bhauman's also exhibits that odd perf anomaly where part 2 is much faster in Planck.

mfikes 2017-12-11T14:09:21.000077Z

90 ms vs. 30 ms

bhauman 2017-12-11T14:09:40.000113Z

well its the same work, so maybe its warmed up

mfikes 2017-12-11T14:12:38.000097Z

Planck starts off at 90 ms but then drops down to 30 ms. My guess (based on some stuff Timothy Baldridge was saying): JavaScriptCore uses a tracing JIT, so after it sees execution, it can compile things to machine code that relies purely on primitives. There is evidently something tracing JITs can do that the Java VM can't do with its current implementation, but I don't fully appreciate what is going on.

mfikes 2017-12-11T14:14:05.000234Z

Perhaps the day 11 problem happens to be highly amenable to tracing analysis, involving a tight loop of math at its core.

borkdude 2017-12-11T14:14:29.000310Z

This sounds like an Advent of Code problem in itself

bhauman 2017-12-11T14:14:31.000058Z

javascript engines are freaking crazy

borkdude 2017-12-11T14:16:09.000342Z

Day 12: > Part 1: some random array walking > Part 2: perhaps the day 12 problem happens to be highly amenable to tracing analysis, involving a tight loop of math at its core. > Determine the time spent after running the algorithm on the output of part 1.

😉 2
mfikes 2017-12-11T14:17:32.000049Z

FWIW, my solution also exhibits the odd perf anomaly: part-2 is 124 ms in Clojure and 19 ms in Planck / JavaScriptCore.

erwin 2017-12-11T14:21:17.000593Z

using the tips and tricks from day5 i see some boxed math and reflection optimization possibilities, so I would say that it is indeed the tracing jit

mfikes 2017-12-11T14:24:33.000318Z

FWIW, nooga's is also exhibiting the same phenomenon

borkdude 2017-12-11T14:24:50.000545Z

@mfikes what about your own?

erwin 2017-12-11T14:25:08.000259Z

https://github.com/ekroon/adventofcode2017/commit/95d397e70fb1485a16a0a9ae30a61a0c80117320 adding ^int ^int ^int ... and it is 10ms and 20 ms

👍 2
borkdude 2017-12-11T14:25:18.000706Z

@mfikes oh sorry 🙂

mfikes 2017-12-11T14:33:34.000499Z

Yeah, for me the call to Math/max was using reflection. 😱 My solution dropped from 124 ms to 8 ms with https://github.com/mfikes/advent-of-code/commit/8b71e9a87f7e9dee970fc6f5176edf30bb2569db#diff-71207370594c4dd13d58690ffbff0646

Alonoaky 2017-12-11T14:37:55.000814Z

try using (defn abs [n] (max n (- n))) instead

mfikes 2017-12-11T14:37:57.000327Z

So, in short, no tracing JIT magic, just plain old reflection being damned slow.

mfikes 2017-12-11T14:38:07.000077Z

OK

Alonoaky 2017-12-11T14:38:09.000127Z

would probably have same effect

mfikes 2017-12-11T14:38:51.000725Z

@alonoaky Yep

borkdude 2017-12-11T14:44:57.000179Z

wasn’t this kind of type hint also valid? (defn ^long distance […] …). When I call distance from the REPL it works fine

borkdude 2017-12-11T14:45:16.000757Z

But when I call it from another form, I get: java.lang.IllegalArgumentException: Unable to resolve classname: clojure.core$long@462e36ad

borkdude 2017-12-11T14:47:09.000505Z

Short snippet of what I mean:

(defn ^long d [] 1)
(Math/abs (d))

mfikes 2017-12-11T14:49:56.000661Z

@borkdude I think that's where they go in ClojureScript, but in Clojure

(defn d ^long [] 1)

mfikes 2017-12-11T14:51:24.000776Z

I'm not so sure, TBH.

borkdude 2017-12-11T14:52:01.000548Z

cool, 12 ms for both parts now

borkdude 2017-12-11T14:53:45.000142Z

why can’t I use ^longs here (instead of annotating each element): https://github.com/borkdude/aoc2017/blob/master/src/day11.clj#L28

borkdude 2017-12-11T14:53:54.000176Z

here I could: https://github.com/borkdude/aoc2017/blob/master/src/day11.clj#L19

mfikes 2017-12-11T14:55:54.000052Z

Hmm. ^longs means a primitive array of ^long right?

borkdude 2017-12-11T14:57:22.000603Z

> What about when you have a sequence of values, all of a uniform type? Clojure provides a number of special hints for these cases, namely ^ints, ^floats, ^longs, and ^doubles. https://github.com/clojure-cookbook/clojure-cookbook/blob/master/08_deployment-and-distribution/8-05_type-hinting.asciidoc

borkdude 2017-12-11T14:57:54.000424Z

works for apply +

borkdude 2017-12-11T14:58:26.000362Z

maybe it’s not supported in a destructuring form

borkdude 2017-12-11T14:58:56.000599Z

I’ll ask in #clojure

borkdude 2017-12-11T15:00:44.000361Z

This works though:

(defn f [[x y z :as ^longs args]]
    (apply + args))

borkdude 2017-12-11T15:01:23.000296Z

so I think as long as you’re using the entire sequence, the type hint will work, but not for individually destructured elements

borkdude 2017-12-11T15:19:39.000353Z

Turns out + doesn’t need the type hint, so it’s bogus anyway

borkdude 2017-12-11T15:23:40.000503Z

This is a bit funny. This give a type hint warning:

(defn f [[x y z]]
  (+ x y z))
but this doesn’t
(defn f [[x y z]]
  (apply + [x y z]))

borkdude 2017-12-11T15:24:28.000472Z

I guess because a vector cannot contain primitives anyway

mfikes 2017-12-11T15:25:54.000593Z

Hmm @borkdude I don't get a reflection warning on your first form (in Clojure 1.9.0)

mfikes 2017-12-11T15:26:22.000680Z

Or, what do you mean by "type hint warning"?

borkdude 2017-12-11T15:26:58.000050Z

@mfikes (set! *unchecked-math* :warn-on-boxed)

mfikes 2017-12-11T15:46:06.000468Z

Ahh, that's cool. By doing (set! *unchecked-math* :warn-on-boxed) you are essentially saying you want the (+ ...) form to use unchecked math (presumably replacing clojure.core/+ with clojure.core/unchecked-add in some way at compile time), but it is warning that it can't actually employ unchecked math because the values are boxed as Object instances? (Which can be fixed by type hinting them.) But in the second form, + is not being compiled—it is being passed in as a higher-order function to apply, so the result is that you get checked arithmetic regardless.

borkdude 2017-12-11T15:48:02.000855Z

aaah 💡

mfikes 2017-12-11T15:54:11.000071Z

I'd like to know why you can't make the second form use unchecked arithmetic by changing it to:

(defn f [[x y z]]
  (reduce unchecked-add [x y z]))

borkdude 2017-12-11T15:55:34.000593Z

vectors can’t contain primitives, but not sure if I understand the problem

mfikes 2017-12-11T15:58:12.000509Z

OK, so

(unchecked-add Long/MAX_VALUE Long/MAX_VALUE)
but no
(apply unchecked-add [Long/MAX_VALUE Long/MAX_VALUE])

mfikes 2017-12-11T15:59:38.000175Z

Also no

(let [ua unchecked-add] (ua Long/MAX_VALUE Long/MAX_VALUE))
Compiler chicanery.

borkdude 2017-12-11T15:59:54.000031Z

same reason as before, inlining doesn’t work with higher order?

mfikes 2017-12-11T16:01:58.000892Z

I think this is why Zach wrote https://github.com/ztellman/primitive-math

mfikes 2017-12-11T16:03:56.000860Z

I like Zach's intro here https://groups.google.com/forum/#!msg/clojure/d89gjX_5Om8/KgBcIORMGYYJ

borkdude 2017-12-11T16:05:22.000300Z

the lib is now “archived”?

2017-12-11T16:11:37.000221Z

I finished an alternate solution for day11 that doesn't use a coordinate system https://github.com/thegeez/clj-advent-of-code-2017/blob/master/src/advent/day11.clj#L166

2017-12-11T16:19:07.000375Z

cool, so many different approaches

borkdude 2017-12-11T17:20:30.000102Z

@thegeez Care to give us the tl;dr explanation?

2017-12-11T17:28:11.000180Z

replacement-moves turns two moves into either zero, one or two moves. For instance [:n :s] is the same as doing nothing, [:n :se] is the same as take a single :se step one by one the steps from moves are added to the shortest path, where the added step might undo a previous step, combine with another step into a single step or the step is added

borkdude 2017-12-11T17:31:58.000271Z

very creative!

2017-12-11T17:33:06.000800Z

The first version of that took way too long for part two, but now it works