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
st3fan 2020-12-03T01:40:21.100700Z

In anticipation of another int computer showing up I rewrite my Python solution from last year in Clojure

st3fan 2020-12-03T01:40:44.101200Z

I’m kind of surprised that I am starting to get a feeling for this style of programming

st3fan 2020-12-03T01:41:57.101600Z

Anyone interested in taking a peek? It is Day 2 of 2019

2020-12-03T03:53:26.104100Z

I just saw that there is a leaderboard code for this year in the topic: 217019-4a55b8eb

solf 2020-12-03T04:44:25.105800Z

Any libraries you usually use for AoC, besides combinatorics?

2020-12-03T04:59:55.106100Z

priority-map

👌 2
2020-12-03T05:30:40.106400Z

and Regal (https://github.com/lambdaisland/regal)

2020-12-03T05:46:00.106800Z

also this one: https://github.com/Engelberg/ubergraph

2020-12-03T05:47:00.107300Z

this one might become handy in some situation too: https://github.com/clojure/core.logic

2020-12-03T05:49:28.107600Z

I mapped my forward/backward slurp/barf in a positional way on my keyboard, to forget their names and remember what they do.

fingertoe 2020-12-03T06:00:29.109400Z

It’s always nice when you solve the first problem adaptively enough that the second problem just works.

✅ 1
fingertoe 2020-12-03T06:02:22.111100Z

@dromar56 I usually try to do it without libs.. Most of the time you don’t need them..

👍 1
➕ 1
solf 2020-12-03T06:05:36.111300Z

None of them needs librairies, it's only if you're trying to go for speed

fingertoe 2020-12-03T06:10:40.111700Z

I do tend to create a util namespace as I go and put a lot of code I am likely to re-use there-- Usually as you go, there is a lot of backtracking, and its handy to re-use the stuff you are going to re-use frequently..

pez 2020-12-03T06:21:57.113700Z

Watched it now. Super nice. You are so fluent in both how you talk and how you code, making it a pleasant experience.

mchampine 2020-12-03T06:34:49.114800Z

Agreed. Mine didn’t “just work” but with a small tweak my part2 function also solved part 1 so I could toss out the first version. I’m ok with that. 🙂. 8 lines total! (not that they’re nice to look at..)

➕ 1
plexus 2020-12-03T06:53:24.115600Z

Thank you 😊

nbardiuk 2020-12-03T06:53:26.115800Z

my solution for part 1 was not flexible enough to support variable vertical step, had to refactor first

plexus 2020-12-03T06:55:02.116400Z

Solution for day 3 (spoilers ahead :)) https://github.com/lambdaisland/aoc_2020/blob/main/src/lambdaisland/aoc_2020/puzzle03.clj

plexus 2020-12-03T06:56:08.117500Z

Recording of the live stream is available on http://youtube.com/c/LambdaIsland

👍 1
plexus 2020-12-03T06:57:22.117600Z

look for past live streams or wait until I re-upload a full quality version

mchampine 2020-12-03T07:04:01.117800Z

Same. I actually started with a more general solution that would have covered both parts, then ‘realized’ it was wasted effort for the first one. My simpler first version + tweak for part 2 was still much less work than the fully general solution I abandoned.

2020-12-03T07:27:27.118300Z

> When in doubt, fork your snippets.

👍 1
2020-12-03T07:29:51.118900Z

I found myself wasting a lot of time reading the problem’s text.

2020-12-03T09:01:52.140500Z

haha, mine is very similar

kenj 2020-12-03T07:37:04.121Z

Anyone have any suggestions on how I can make my (reduce) more readable? I feel like if I came back even a week from now and read the reduce there, it would take me awhile. At the same time, reducing while updating multiple variables seems like a fairly common thing (at least something I do in Python a lot) and I’m wondering if there’s a better way.

👍 1
kenj 2020-12-03T08:03:18.129200Z

Thanks for the advice! I rewrote using loop and it’s cleaner IMO, though a bit longer

(defn path [dx dy data]
  (loop [result-vec []
         row-index  0
         data       (take-nth dy data)]
    (if (seq data)
      (recur
        (conj result-vec (nth (cycle (first data)) row-index))
        (+ row-index dx)
        (rest data))
      result-vec)))
Also thanks for the style guide link Erwin, I updated to use a set predicate as well.

kenj 2020-12-03T08:04:24.129400Z

Re drop 1 vs rest, I thought drop 1 conveyed intentionality a bit more… like for whatever reason this problem doesn’t care about the tree that may or may not exist at your starting point, so drop it :man-shrugging:

erwinrooijakkers 2020-12-03T08:04:48.129600Z

i see

erwinrooijakkers 2020-12-03T08:05:18.129900Z

Another way is like this:

(defn path [dx dy data]
  (map (fn [row row-index] (nth (cycle row) row-index))
       (take-nth dy data)
       (iterate (partial + dx) 0)))

erwinrooijakkers 2020-12-03T08:06:13.130100Z

So use map with two colls, where first coll has the rows, and second coll is lazy seq of x-indices

kenj 2020-12-03T08:11:21.130900Z

ohhhh that’s clever!

kenj 2020-12-03T08:12:31.131500Z

very concise while still being understandable

erwinrooijakkers 2020-12-03T08:16:42.133Z

🙂

erwinrooijakkers 2020-12-03T07:44:07.121500Z

Nice. Did not know of take-nth

2020-12-03T07:45:00.122Z

a loop / recur has similar effect as a reduce, with less destructuring

erwinrooijakkers 2020-12-03T07:48:20.125200Z

I was looking for a take-while that does not return a lazy sequence of the original value up to (pred item), but a lazy sequence of the results of (pred item) . So not

(take-while pos? [2 3 -1]) ;; => (2 3)
but:
(*** pos? [2 3 -1]) ;; => (true true)
does this exist? I implemented a keep-while by modifing take-while to do just that, but maybe something exists already

plexus 2020-12-03T07:50:38.125800Z

(comp (partial take-while identity) map)

👍 1
erwinrooijakkers 2020-12-03T09:22:46.140700Z

It does go up to two times over the collection this way

erwinrooijakkers 2020-12-03T09:23:29.140900Z

map and then take-while, I hoped for a solution that goes over it once

plexus 2020-12-03T10:17:16.146400Z

they're lazy sequences though, sure you'll realize at least a chunk but it won't necessarily map over the whole collection. If you really want to do it single pass then I guess transducers are the way to go (comp (map your-fn) (take-while identity))

erwinrooijakkers 2020-12-03T10:49:13.147300Z

true 🙂 thanks!

erwinrooijakkers 2020-12-03T07:52:55.126Z

I’ll take a look, one first suggestion is using #{\#} instead of #(= \# %) . See clojure-style-guide: https://github.com/bbatsov/clojure-style-guide#set-as-predicate

erwinrooijakkers 2020-12-03T07:53:13.126300Z

;; good
(remove #{1} [0 1 2 3 4 5])

;; bad
(remove #(= % 1) [0 1 2 3 4 5])

2020-12-03T07:56:18.127700Z

Cursive users be like “WHAAAAAAAT????!!!!” (time in the video 21:37~22:37) https://youtu.be/wXVDfzxeZ-o?t=1297

erwinrooijakkers 2020-12-03T08:09:13.130300Z

haha impressive 🙂

erwinrooijakkers 2020-12-03T08:09:22.130500Z

I use multiple cursors for those

plexus 2020-12-03T08:10:56.130700Z

yeah I went through an mc phase but don't really use them any more. In the past I would have used emacs macros for these.

erwinrooijakkers 2020-12-03T08:12:09.131100Z

🦜 1
erwinrooijakkers 2020-12-03T08:13:19.131800Z

bit more archaic

erwinrooijakkers 2020-12-03T08:13:37.132Z

regex has more flair

plexus 2020-12-03T08:16:58.133200Z

if only emacs regexes weren't so terrible

erwinrooijakkers 2020-12-03T08:17:05.133400Z

why did you stop using mc?

plexus 2020-12-03T08:20:27.135600Z

not sure it was really a conscious choice. Might have happened when I switched to vim-style bindings

pez 2020-12-09T20:52:33.462100Z

VS Code + Calva Paredit.

pez 2020-12-09T20:58:54.462500Z

Unfortunately I haven’t had the focused time I need to make Paredit multi cursor. I miss it every day.

erwinrooijakkers 2020-12-03T07:56:34.127800Z

drop 1 = rest

plexus 2020-12-03T07:58:59.128Z

ed(1) users be like, pfff no sweat

😄 3
1
2020-12-03T07:59:31.128400Z

My solution (mostly inline code) https://github.com/green-coder/advent-of-code-2020/blob/master/src/aoc/day_3.clj#L21-L35

👍 1
2020-12-03T08:00:33.128600Z

no named function --> no problem choosing and learning the names

plexus 2020-12-03T08:01:16.128800Z

nice and compact!

nbardiuk 2020-12-03T08:19:29.135300Z

https://github.com/nbardiuk/adventofcode/blob/master/2020/src/day03.clj - inetersting how part1 is just a smaller example for part2

👏 1
plexus 2020-12-03T08:23:19.135900Z

interesting! I like how you make a separate sequence of xs and ys and then use multi-arity map!

nbardiuk 2020-12-03T08:27:33.136100Z

I had to be careful to keep one of the sequences finite 😁

2020-12-03T08:50:03.140200Z

Here is my solution, firstly I used cycle , but it’s a bit slow, so I replaced it with mod . https://github.com/zelark/AoC-2020/blob/master/src/zelark/aoc_2020/day_03.clj

2020-12-03T09:54:12.142900Z

I tried to solve day 3, I got both parts solved with a rubbish solution here

(let [input (->> (slurp "input.txt")
                 (str/split-lines))]
  (loop [x 0
         y 0
         trees 0]
    (if (>= y (count input))
      trees
      (let [cur (nth (nth input y) x)]
        (if (= cur \#)
          (recur (mod (+ 1 x) 31) (+ 2 y) (inc trees))
          (recur (mod (+ 1 x) 31) (+ 2 y) trees))))))
But then I tried to rewrite it using iterate, but I'm having two issues.
(defn is-tree [i x _]
  (= \# (nth i (mod x 31))))

(let [input (->> (slurp "input.txt")
                 (str/split-lines))
      x-step 7
      y-step 1]
  (->> (take-nth y-step input)
       (map is-tree input (iterate #(+ x-step %) 0))
       (filter true?)
       (count)))
1. Why is is-tree wanting 3 parameters here? I thought it would want 2 inputs, line from input and value from my iterate 2. it breaks when y is 2. I should get 30 on my input but I get 32 It works for (1,1), (3,1), (5,1), (7,1) but not (1, 2)

genmeblog 2020-12-03T09:56:59.143100Z

[i x _] <--- you have defined 3 parameters (`_` is also a name for parameter)

2020-12-03T09:57:22.143300Z

yeah, if i remove that parameter I get an error that I'm passing wrong number of parameters.

genmeblog 2020-12-03T09:58:02.143500Z

Ok, I see, threading last adds another third sequence to your map.

genmeblog 2020-12-03T09:58:29.144Z

sou you have: 1. input 2. iterate... 3. take-nth... as a thrid

genmeblog 2020-12-03T09:58:44.144200Z

just remove input

2020-12-03T09:58:45.144400Z

oh yeah! of course! Thanks

genmeblog 2020-12-03T10:01:00.144600Z

And this should fix the second problem I suppose

2020-12-03T10:02:01.144800Z

yes, it did 😄 Thanks for your help!

2020-12-03T10:02:28.145Z

sometimes you stare at code and dont see a problem, then someone else spots it in a second!

genmeblog 2020-12-03T10:03:18.145200Z

Yeah, that's common 🙂

misha 2020-12-03T11:21:58.148600Z

(defn trees [lines [dx dy]]
  (let [width (-&gt;&gt; lines first count)
        nextx (fn [x] (mod (+ x dx) width))]
    (-&gt;&gt; (map get
           (take-nth dy lines)
           (iterate nextx 0))
      (keep #{\#})
      (count))))

👍 3
genmeblog 2020-12-03T11:27:02.149Z

cool, concise

genmeblog 2020-12-03T11:27:24.149500Z

I used map-indexed in my solution: https://github.com/genmeblog/advent-of-code-2020/blob/master/src/advent_of_code_2020/day03.clj#L11

benoit 2020-12-03T12:15:49.151900Z

Hi everyone. A little late on the advent of code this year.

benoit 2020-12-03T12:16:11.152800Z

My solution for day3. https://github.com/benfle/advent-of-code-2020/blob/main/day3.clj I went for readable code (as opposed to concise).

pez 2020-12-03T12:16:20.153200Z

Not sure what you peeps have agreed upon before I joined, but I would like if we could post our solutions in threads. So, a post saying things like “here’s my solution for day X”, but then the code/description is in a reply. But if that’s not something others would want too, I’m fine with not opening this channel until I have my own solution.

👍 3
misha 2020-12-03T12:36:23.159200Z

Each day at least one solution will be posted out in the open by somebody who did not get the memo/does not care/does not think it matters/forgot/mis-clicked/etc. It is hard to coordinate 300+ people without moderation and ban-hammer (like in reddit thread). So I just don't read the channel until I stuck or have a solution. And when I do, and want to see how to improve it – browsing all the threads often becomes tiresome. On the other hand, in a week or so solutions will be too large to be spoilers at a glance, so meh ¯\(ツ)

👍 1
plexus 2020-12-03T13:17:55.162200Z

I do think it's good etiquette to not just post solutions directly as code in the chat. If you just consistently remind people in a friendly way then most people will have gotten the message within a day or two. That way people can also edit their post to hide the spoiler. A hint in the channel topic would also be good, though I'm aware that far from everyone actually reads that.

plexus 2020-12-03T13:19:36.163600Z

but also yes, if you really don't want to be spoilt then it's probably best to avoid this place until you've done the puzzle. Sometimes it's just a matter of discovering the right type of data structure or something like that, where it's not so much seeing the code but just seeing people talk about what they used to solve it.

misha 2020-12-03T13:27:04.165400Z

I agree with you on "good etiquette", but still forecast "at least one open solution per day", now with additional ignored "guys, please post in threads" messages on top of that :opieop:

2020-12-03T14:19:19.165700Z

That's a great suggestion. I will abide.

2020-12-03T14:26:21.169700Z

But I won't be watching the chan before I finish my solutions.

st3fan 2020-12-03T15:52:27.171300Z

I should put part3 in a loop instead of separate calls

erwinrooijakkers 2020-12-03T15:52:34.171500Z

You should put your answer in a thread 😛

plexus 2020-12-03T15:55:01.172100Z

posting links is fine, right? 🙂

pez 2020-12-03T15:55:15.172600Z

Yeah, posting links is fine, me thinks.

plexus 2020-12-03T15:55:21.173Z

not sure if you were joking, I guess the 😛 is tongue in cheek

erwinrooijakkers 2020-12-03T15:59:43.174Z

yes it was a medium joke

erwinrooijakkers 2020-12-03T16:00:01.174400Z

that way of posting is fine indeed there’s no code visible. although i think if you don’t want spoilers and hints don’t go here. i use this channel to check for nice solutions.

st3fan 2020-12-03T16:01:09.175700Z

will someone pin a daily answers thread then?

st3fan 2020-12-03T16:01:18.176Z

i can't find the right thread

st3fan 2020-12-03T16:02:36.176900Z

Day 3 Answers Thread .. Post your answers here

👍 2
pez 2020-12-04T23:06:46.264600Z

With some pointers from @misha (gaining me x200 speed!):

(ns pez.aoc2020.day-3
  (:require [clojure.string :as s]
            [pez.aoc2020.util :as util]))

(defn line-landing [dx line]
  (-&gt; line
      (nth (mod dx (count line)))))

(defn count-trees [[dx dy] woods]
  (-&gt;&gt; woods
       (take-nth dy)
       (map line-landing (take-nth dx (range)))
       (filter #{\#})
       (count)))

(comment
  (def input (util/fetch-input 3))
  (time
   (as-&gt; input $
     (s/split-lines $)
     (map #(count-trees % $) [[1 1] [3 1] [5 1] [7 1] [1 2]])
     (apply * $)))
  ;; "Elapsed time: 1.75155 msecs"
  ;; =&gt; 3316272960
  )

st3fan 2020-12-03T16:02:52.177200Z

Day 4 Answers Thread .. Post your answers here

mchampine 2020-12-04T14:23:10.244400Z

My part 1

(def processed-input
  (-&gt;&gt; (str/split-lines (slurp "resources/day04.data"))
       (partition-by empty?)
       (take-nth 2)
       (map #(apply str %))))

(defn allkeys? [l]
  (every? #(str/includes? l %)
          ["ecl:" "pid:" "byr:" "eyr:" "hcl:" "hgt:" "iyr:"]))

(count (filter allkeys? processed-input))

rjray 2020-12-04T21:57:22.260700Z

This is my cleaned-up day 4 code: https://github.com/rjray/advent-2020-clojure/blob/master/src/advent_of_code/day04bis.clj. I don't know why I struggled with this one, or why the first pass at it was so cluttered.

pez 2020-12-04T22:09:06.262600Z

Oh, a day 4 solutions thread. Nice! https://gist.github.com/PEZ/42375289c032e43ee6dd8cd40b3c1b42

st3fan 2020-12-03T16:06:18.179Z

I started to build a utils with some things like (load-input day tranformer-fn) to make things simpler

2020-12-03T16:16:41.179800Z

The leaderboard is crazy, it takes me longer to read the puzzle description and parse what i've actually to do than it takes some people to solve part 1

☝️ 1
2020-12-03T16:19:18.180600Z

I upgraded my deps.edn with more dependencies, just in case. https://github.com/green-coder/advent-of-code-2020/blob/master/deps.edn

2020-12-03T16:24:39.182700Z

I noticed that it is possible to read the time at which people got their stars in the JSON file following the [API] link.

nate 2020-12-03T16:25:35.183700Z

in the past there has been a github repo with a list of links to individual repos with advent solutions, has anyone started that yet this year?

2020-12-03T16:28:32.184500Z

I mean in the Clojurian leaderboard.

2020-12-03T16:28:49.184800Z

Today, the fastest was about 16 minutes (5:16 a.m. UTC)

plexus 2020-12-03T16:37:03.185500Z

there's one in the channel topic, but it only mentions 2017 and 2018, although it did just get a commit

nate 2020-12-03T16:40:08.185700Z

ah cool

plexus 2020-12-03T16:58:38.188100Z

So what you're saying is you actually prefer people to post their answers directly inline?

misha 2020-12-03T17:03:27.191400Z

inline is fine with me. And it is way better than any moderation fuzz around it (which is 70% of today's messages). There are plenty of places where moderation is organic, and, more importantly, expected. I don't think it fits here, specifically because gains are so tiny, but cost is not.

st3fan 2020-12-03T17:45:24.193100Z

@qmstuart i've given up on the leader board .. also because I'm in EST I would have to stay up till midnight and then start hacking on a solution. So instead I am fine collecting stars and just writing nice solutions. I'm more than happy if I am actually able to solve these with my current Clojure skills. And learn a few new tricks from others.

➕ 4
2020-12-03T17:46:08.193600Z

@st3fan yeah, puzzle go live at 5am my time. No way am I getting out of bed that early!

st3fan 2020-12-03T17:46:15.193800Z

5am I could do! 🙂

plexus 2020-12-03T18:01:05.194500Z

here it's 6am, which somehow because of the pandemic is later than when I usually get up 🙂

😮 2
pez 2020-12-03T18:13:21.194600Z

Agree we do not need to use moderators for this. We can encourage each other. It’s not the end of the world if I see spoilers, just would be nice if most solutions were a click away.

erwinrooijakkers 2020-12-03T18:47:28.195200Z

6am i made today and hopefully tomorrow again 🙂 really nice to see the clock ticking

erwinrooijakkers 2020-12-03T18:49:18.195700Z

Agreed as well

erwinrooijakkers 2020-12-03T18:49:56.195900Z

You can expect to see spoilers if you come here, same for http://old.reddit.com/r/adventofcode

erwinrooijakkers 2020-12-03T18:53:02.196200Z

misha 2020-12-03T19:12:23.197100Z

it is "would be nice if most solutions were not a click away" for me though :d: I'd settle on "click to expand away" :) but generally, the key to disappointment is expectations: If 1) you expect this channel to be moderated, 2) someone new or not-caring posts spoiler code 3) moderator is busy solving, and is late to moderate, but 4) comes and deletes spoiler: • you are upset you saw a spoiler • moderator is upset there were spoilers to moderate • poster is upset their message is deleted

misha 2020-12-03T19:13:52.197300Z

Extra work, but everybody is still upset. Not worth it, imo.

2020-12-03T19:47:11.197600Z

does everyone get different inputs?

2020-12-03T19:47:30.197900Z

ah, yes. opening incognito reveals this: Puzzle inputs differ by user. Please log in to get your puzzle input.

pez 2020-12-03T20:48:30.198200Z

Not sure what tree you are barking up agains, @misha. No-one expects to have a spoilerfree environment. I am just asking if people could consider putting their spoilers in a reply. Now and then someone new will not notice this practice. That’s how it is.

misha 2020-12-03T20:55:10.198400Z

"barking", ok

st3fan 2020-12-03T21:33:48.198700Z

Yup no cheating!

2020-12-03T22:29:16.199Z

not cheating, just curious 🙂

2020-12-03T22:51:20.200Z

I checked the FAQ but couldn’t figure it out… is there a day limit to finish the last problem? Not planning to actually tackle the last one on Christmas day…

rjray 2020-12-03T23:16:58.200900Z

No day limit on any of the problems (people often go back to previous years as practice). But you won't get a positional ranking for any solution that takes more than 24 hours.

pez 2020-12-03T23:37:48.201100Z

I’m sorry. Didn’t mean it literally and didn’t mean to be rude.