Spoiler alert - https://github.com/st3fan/aoc/blob/master/2020/advent-of-code/src/advent_of_code/day2.clj
In anticipation of another int computer showing up I rewrite my Python solution from last year in Clojure
I’m kind of surprised that I am starting to get a feeling for this style of programming
Anyone interested in taking a peek? It is Day 2 of 2019
https://gist.github.com/st3fan/4c5335a92750f84d62e43fa595fbd906
I just saw that there is a leaderboard code for this year in the topic: 217019-4a55b8eb
Any libraries you usually use for AoC, besides combinatorics?
priority-map
and Regal (https://github.com/lambdaisland/regal)
also this one: https://github.com/Engelberg/ubergraph
this one might become handy in some situation too: https://github.com/clojure/core.logic
I mapped my forward/backward slurp/barf in a positional way on my keyboard, to forget their names and remember what they do.
It’s always nice when you solve the first problem adaptively enough that the second problem just works.
@dromar56 I usually try to do it without libs.. Most of the time you don’t need them..
None of them needs librairies, it's only if you're trying to go for speed
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..
Watched it now. Super nice. You are so fluent in both how you talk and how you code, making it a pleasant experience.
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..)
Thank you 😊
my solution for part 1 was not flexible enough to support variable vertical step, had to refactor first
Solution for day 3 (spoilers ahead :)) https://github.com/lambdaisland/aoc_2020/blob/main/src/lambdaisland/aoc_2020/puzzle03.clj
Recording of the live stream is available on http://youtube.com/c/LambdaIsland
look for past live streams or wait until I re-upload a full quality version
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.
> When in doubt, fork your snippets.
I found myself wasting a lot of time reading the problem’s text.
My Day 3 solution: https://gist.github.com/KennyMonster/2cdb391d0f45105b8ab38dab58923063#file-day_3-clj-L11
haha, mine is very similar
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.
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.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:
i see
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)))
So use map with two colls, where first coll has the rows, and second coll is lazy seq of x-indices
ohhhh that’s clever!
very concise while still being understandable
🙂
Mine: https://github.com/transducer/adventofcode/blob/master/src/adventofcode/2020/day3.clj
Nice. Did not know of take-nth
a loop / recur has similar effect as a reduce, with less destructuring
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(comp (partial take-while identity) map)
It does go up to two times over the collection this way
map and then take-while, I hoped for a solution that goes over it once
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))
true 🙂 thanks!
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
;; good
(remove #{1} [0 1 2 3 4 5])
;; bad
(remove #(= % 1) [0 1 2 3 4 5])
Cursive users be like “WHAAAAAAAT????!!!!” (time in the video 21:37~22:37) https://youtu.be/wXVDfzxeZ-o?t=1297
haha impressive 🙂
I use multiple cursors for those
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.
bit more archaic
regex has more flair
if only emacs regexes weren't so terrible
why did you stop using mc
?
not sure it was really a conscious choice. Might have happened when I switched to vim-style bindings
VS Code + Calva Paredit.
Unfortunately I haven’t had the focused time I need to make Paredit multi cursor. I miss it every day.
drop 1
= rest
ed(1) users be like, pfff no sweat
My solution (mostly inline code) https://github.com/green-coder/advent-of-code-2020/blob/master/src/aoc/day_3.clj#L21-L35
no named function --> no problem choosing and learning the names
nice and compact!
https://github.com/nbardiuk/adventofcode/blob/master/2020/src/day03.clj - inetersting how part1 is just a smaller example for part2
interesting! I like how you make a separate sequence of xs and ys and then use multi-arity map!
I had to be careful to keep one of the sequences finite 😁
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
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)[i x _]
<--- you have defined 3 parameters (`_` is also a name for parameter)
yeah, if i remove that parameter I get an error that I'm passing wrong number of parameters.
Ok, I see, threading last adds another third sequence to your map.
sou you have: 1. input 2. iterate... 3. take-nth... as a thrid
just remove input
oh yeah! of course! Thanks
And this should fix the second problem I suppose
yes, it did 😄 Thanks for your help!
sometimes you stare at code and dont see a problem, then someone else spots it in a second!
Yeah, that's common 🙂
(defn trees [lines [dx dy]]
(let [width (->> lines first count)
nextx (fn [x] (mod (+ x dx) width))]
(->> (map get
(take-nth dy lines)
(iterate nextx 0))
(keep #{\#})
(count))))
cool, concise
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
Hi everyone. A little late on the advent of code this year.
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).
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.
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 ¯\(ツ)/¯
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.
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.
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:
That's a great suggestion. I will abide.
https://redpenguin101.github.io/posts/2020_12_03_aoc2020_day3.html
But I won't be watching the chan before I finish my solutions.
Day 3 https://gist.github.com/st3fan/2308d8a849fe47b0b31e2d3108870660
I should put part3 in a loop instead of separate calls
You should put your answer in a thread 😛
posting links is fine, right? 🙂
Yeah, posting links is fine, me thinks.
not sure if you were joking, I guess the 😛 is tongue in cheek
yes it was a medium joke
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.
will someone pin a daily answers thread then?
i can't find the right thread
Day 3 Answers Thread .. Post your answers here
https://github.com/chamaeleon/aoc2020-clj/blob/master/src/aoc2020/day03.clj
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]
(-> line
(nth (mod dx (count line)))))
(defn count-trees [[dx dy] woods]
(->> woods
(take-nth dy)
(map line-landing (take-nth dx (range)))
(filter #{\#})
(count)))
(comment
(def input (util/fetch-input 3))
(time
(as-> input $
(s/split-lines $)
(map #(count-trees % $) [[1 1] [3 1] [5 1] [7 1] [1 2]])
(apply * $)))
;; "Elapsed time: 1.75155 msecs"
;; => 3316272960
)
Day 4 Answers Thread .. Post your answers here
My part 1
(def processed-input
(->> (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))
https://github.com/st3fan/aoc/blob/master/2020/advent-of-code/src/advent_of_code/day4.clj
https://github.com/chamaeleon/aoc2020-clj/blob/master/src/aoc2020/day04.clj
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.
Oh, a day 4 solutions thread. Nice! https://gist.github.com/PEZ/42375289c032e43ee6dd8cd40b3c1b42
many good solutions here. adding mine https://github.com/neeasade/AdventOfCode/blob/66cdb6073b37ce854f3d102575040f786395355c/src/aoc/2020.clj#L64-L116
https://github.com/st3fan/aoc/blob/master/2020/advent-of-code/src/advent_of_code/day3.clj
https://clojurians.slack.com/archives/C0GLTDB2T/p1606983569135300
I started to build a utils with some things like (load-input day tranformer-fn)
to make things simpler
https://gist.github.com/stuartstein777/b9e34125f48d3c7791c2e08e7c41e576
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
I upgraded my deps.edn with more dependencies, just in case. https://github.com/green-coder/advent-of-code-2020/blob/master/deps.edn
https://github.com/lambdaisland/aoc_2020/blob/main/src/lambdaisland/aoc_2020/puzzle03.clj
I noticed that it is possible to read the time at which people got their stars in the JSON file following the [API] link.
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?
https://redpenguin101.github.io/posts/2020_12_03_aoc2020_day3.html
I mean in the Clojurian leaderboard.
Today, the fastest was about 16 minutes (5:16 a.m. UTC)
there's one in the channel topic, but it only mentions 2017 and 2018, although it did just get a commit
ah cool
So what you're saying is you actually prefer people to post their answers directly inline?
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.
@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.
@st3fan yeah, puzzle go live at 5am my time. No way am I getting out of bed that early!
5am I could do! 🙂
here it's 6am, which somehow because of the pandemic is later than when I usually get up 🙂
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.
6am i made today and hopefully tomorrow again 🙂 really nice to see the clock ticking
Agreed as well
You can expect to see spoilers if you come here, same for http://old.reddit.com/r/adventofcode
https://github.com/listba/advent-of-code-2020/blob/master/clojure/src/aoc_2020/days/03.clj
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
Extra work, but everybody is still upset. Not worth it, imo.
does everyone get different inputs?
ah, yes. opening incognito reveals this: Puzzle inputs differ by user. Please log in to get your puzzle input.
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.
"barking", ok
Yup no cheating!
not cheating, just curious 🙂
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…
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.
I’m sorry. Didn’t mean it literally and didn’t mean to be rude.