adventofcode

2021-12-01T05:25:46.051900Z

Hooray - I actually remembered advent of code on the first day.

πŸ’― 4
R.A. Porter 2021-12-01T05:30:18.055100Z

Day 1 Solutions 🧡

Benjamin 2021-12-03T17:38:57.181100Z

@mchampine that swag

πŸ˜€ 1
kj 2021-12-02T08:47:57.097600Z

(def input ...)

(defn count-increases
  [coll n]
  (->> (map < coll (drop n coll))
       (filter true?)
       count))

;; Part 1
(count-increases input 1)

;; Part 2
(count-increases input 3)

euccastro 2021-12-02T23:38:51.152600Z

solution to day1: https://github.com/euccastro/advent-of-code-2021/blob/main/day1.clj

R.A. Porter 2021-12-01T05:32:40.055500Z

Took me twice as long as it could have because I forgot the pad for partition was a collection. But otherwise...just partitions and filters on comparisons.

Chase 2021-12-01T05:37:28.055700Z

https://github.com/Chase-Lambert/aoc-clojure/blob/main/src/aoc/2021/day_01.clj

πŸ‘ 2
πŸ‘πŸ» 1
Chase 2021-12-01T05:40:20.056Z

That was exciting to be there at the start. I wasted a few minutes doing something dumb and ended up at like 4000th place which is probably the best spot I'll get. lol

Chase 2021-12-01T05:41:06.056200Z

and already seeing some great clojure solutions on the reddit solution thread

kpav 2021-12-01T05:41:37.056400Z

https://github.com/kwpav/advent-of-code-2021/blob/master/src/advent_of_code_2021/day01.clj

πŸ‘ 3
πŸ‘πŸ» 2
R.A. Porter 2021-12-01T05:45:09.056800Z

Guess I should post mine. Elided the boilerplate of reading/parsing input.

πŸ‘ 3
πŸ‘πŸ» 1
mchampine 2021-12-01T06:31:35.060200Z

;; part1
(defn count-incs [nn]
  (count (filter pos? (map - (rest nn) nn))))

(count-incs input) ;; => 1681

;; part2
;; sliding window size 3
(-> (map #(apply + %) (partition 3 1 input))
    count-incs) ;; => 1704

πŸ‘ 5
πŸ‘πŸ» 1
peterc 2021-12-01T06:42:50.061Z

(defn part1 [data]
  (->> (map - (rest data) data)
       (filter pos?)
       count))

(defn part2 [data]
  (->> (map - (rest (drop 2 data)) data)
       (filter pos?)
       count))

πŸ‘ 2
πŸ‘πŸ» 2
nbardiuk 2021-12-01T06:47:50.061900Z

Happy to use clojure alpha with parse-long in core https://github.com/nbardiuk/adventofcode/blob/master/2021/src/day01.clj

πŸ‘ 3
πŸ™Œ 5
Daniel Amber 2021-12-21T08:05:28.248300Z

@pez Only 20 days later πŸ™‚ https://www.youtube.com/watch?v=q_I3r0JGhR8

πŸŽ‰ 1
Filip Strajnar 2021-12-19T20:26:03.223400Z

(defn
  by-indicies-2
  [numbers]
  (->> numbers count dec range (filter #(< (nth numbers %) (nth numbers (inc %)))) count))

Filip Strajnar 2021-12-19T20:26:20.223600Z

this is the 1st part, I'm looking for suggestions how to make it shorter

Tero Matinlassi 2021-12-06T21:40:19.355800Z

(ns day1 
  (:require [clojure.string]))

(comment
  ; part 1
  (->> "input"
       slurp
       clojure.string/split-lines
       (map #(Integer/parseInt %))
       (reduce (fn [[c a] b]
                 (if (> b a)
                   [(inc c) b]
                   [c b]))
               [0 0])
       first
       dec)
  ;=> 1752
)

(defn sliding-window [s]
  (partition 3 (interleave s (rest s) (rest (rest s)))))  

(comment
  ; part 2
  (->> "input"
       slurp
       clojure.string/split-lines
       (map #(Integer/parseInt %))
       sliding-window
       (map #(reduce + %))
       (reduce (fn [[c a] b]
                 (if (> b a)
                   [(inc c) b]
                   [c b]))
               [0 0])
       first
       dec)
;=> 1781
  )

Antonio Bibiano 2021-12-01T08:16:15.064200Z

very similar to the others.. avoided some repetition

Antonio Bibiano 2021-12-01T08:16:27.064400Z

(defn part1 [input]
  (->> input
       (partition 2 1)
       (filter #(apply < %))
       count))
(defn part2 [input]
  (->> input
       (partition 3 1)
       (map #(apply + %))
       part1))

πŸ‘ 3
❀️ 1
Adam Haber 2021-12-01T08:52:45.065100Z

(->> l 
     (#(map < % (rest %)))
     (filter identity)
     count)

Adam Haber 2021-12-01T08:52:57.065300Z

(->> l
     (#(map + % (rest %) (rest (rest %))))
     (#(map < % (rest %)))
     (filter identity)
     count)

πŸ‘ 1
2021-12-01T09:58:32.066500Z

Using my utility function that reads a file, splits it line by line and applies a parser function to each line.

(def puzzle-input "puzzle-inputs/2021/day1")

(defn calculate-increasing [xs]
  (->> (map < xs (rest xs))
       (filter true?)
       (count)))

(defn parser [n]
  (Integer/parseInt n))

;; Part 1
(->> (f/read-all-lines-and-parse puzzle-input parser)
     calculate-increasing)

;; Part 2
(->> (f/read-all-lines-and-parse puzzle-input parser)
     (partition 3 1)
     (map #(reduce + %))
     calculate-increasing)
Easy day 1. Don't know if that's a good sign or not πŸ˜†

πŸ‘ 1
🀨 1
tschady 2021-12-01T10:52:06.067400Z

(def input (file-util/read-ints "2021/d01.txt"))

(defn part-1 [input]
  (count (filter pos? (map - (rest input) input))))

(defn part-2 [input]
  (count (filter pos? (map - (drop 3 input) input))))

πŸ‘ 3
karlis 2021-12-01T11:03:06.067800Z

Starting off easy πŸ™‚

(defn increase-count [i] (count (filter true? (map < i (drop 1 i)))))

(def puzzle1 increase-count)

(defn puzzle2 [input]
  (increase-count (map + input (drop 1 input) (drop 2 input))))

πŸ‘ 3
Felipe 2021-12-01T13:10:05.075200Z

exact same solution as @antbbn, minus the thread macros

(def input (map read-string (clojure.string/split-lines (slurp ""))))


(defn increases [coll] (count (filter #(apply < %) (partition 2 1 coll))))

;; part 1
(increases input)

;; part 2
(increases (map #(apply + %) (partition 3 1 input)))

πŸ‘ 3
2021-12-01T16:06:54.077800Z

heh, some of y’alls solutions are so simple. Im new so I didnt know about partition facepalm

(defn add-not-nil [& col]
  (when (not (some nil? col)) (reduce + col)))

(defn optimized [col]
  (reduce-kv (fn [count index value]
               (let [value2 (nth col (inc index) nil)
                     value3 (nth col (inc (inc index)) nil)
                     value4 (nth col (inc (inc (inc index))) nil)
                     window1 (add-not-nil value value2 value3)
                     window2 (add-not-nil value2 value3 value4)]
                 (if (and  (not (nil? window1)) (not (nil? window2)))
                   (if (< window1 window2)
                     (inc count)
                     count)
                   count))) 0 col))

2021-12-01T16:53:57.078500Z

Tried to go lazy using transducers (using this lib https://github.com/cgrand/xforms for the missing partition transducer from the core)

pithyless 2021-12-01T17:00:43.079400Z

@cyppan xforms has an x/count and x/lines-in - ie. (x/count (comp ,,,) (xio/lines-in resource)) That way, you wouldn't need the with-open, and returning and adding up the 1 's from keep

2021-12-01T17:01:57.079700Z

oh thanks ! πŸ˜„

pithyless 2021-12-01T17:05:06.079900Z

I'd also argue that this is a little misleading: > Tried to go lazy using transducers I think more appropriate would be > Tried to go super-eager using transducers 😜

2021-12-01T17:08:12.080300Z

you’re absolutely right

Sam Adams 2021-12-01T17:27:58.081Z

My HTML solution πŸ˜‰: https://samadams.dev/2021/12/01/advent-of-code-day-1.html

Andrew Byala 2021-12-01T18:07:18.081700Z

My Clojure solution, including my https://github.com/abyala/advent-2021-clojure/blob/main/docs/day01.md about how I got there.

πŸ‘ 1
nooga 2021-12-01T19:21:18.085600Z

(def input [ .. ])
(def solve #(->> (map < %1 (next %1)) (filter true?) count))
(println (solve input))
(println (->> input (partition 3 1) (map #(apply + %1)) solve))

2021-12-01T06:11:24.059100Z

Good morning everyone, and happy new AoC ! I won't compete on the clock this year, I chose to stream while slowly solving the problems in the hope of making more people discover Clojure.

😁 2
πŸ™Œ 7
πŸ™ŒπŸ» 1
roelof 2021-12-05T08:27:00.254Z

coulod be, was just curious @vincent.cantin

2021-12-05T09:33:18.254400Z

Yes, I am French

Aleks 2021-12-01T06:12:52.059500Z

Morning!

roelof 2021-12-01T06:31:47.060400Z

morning , where can I find the stream and can I look to it when you have solved the challenges ?

roelof 2021-12-01T06:31:57.060600Z

@vincent.cantin

nbardiuk 2021-12-01T06:49:34.062200Z

Vincent streams on twitch https://www.twitch.tv/greencoder

2021-12-01T06:57:01.063200Z

I will start the stream in about 15 min

roelof 2021-12-01T07:14:00.063500Z

I wait

roelof 2021-12-04T14:37:28.213100Z

he, no coding from @vincent.cantin?

2021-12-04T18:02:23.218800Z

I did it on Twitch, but late because of the reClojure conference

roelof 2021-12-04T18:04:20.219Z

oke I will look it later

roelof 2021-12-04T18:04:40.219200Z

and hopefully learn from it

roelof 2021-12-04T18:07:26.219600Z

@vincent.cantin are you french and live in Europe ?

2021-12-04T20:11:08.236500Z

I live in Taiwan πŸ‡ΉπŸ‡Ό

2021-12-04T20:12:37.236900Z

My repo for AoC 2021 is at https://github.com/green-coder/advent-of-code/tree/master/src/aoc_2021

roelof 2021-12-04T20:35:31.237300Z

oke, I asked because I think I heard a french acccent , I apolize

roelof 2021-12-04T20:35:36.237500Z

@vincent.cantin

2021-12-04T21:25:34.238100Z

French people in Taiwan do not lose their French accent 😁

roelof 2021-12-01T11:38:15.068400Z

@vincent.cantin thanks for the stream but I wonder for adding the three numbers . why not use reduce for it ?

2021-12-01T11:48:38.068700Z

We could, but I tried to be accessible to a wider audience

2021-12-01T11:49:42.068900Z

I will mention it in tomorrow's stream. Thanks for mentioning it.

roelof 2021-12-01T11:50:14.069100Z

np, just wondering

misha 2021-12-01T06:53:01.062700Z

πŸ‘‹ opieop

πŸ‘‹ 4
nooga 2021-12-01T12:49:53.073500Z

πŸ‘‹ Happy AoC everyone! This year I’m solving using my own Clojure(-like-language) implementation: https://github.com/nooga/let-go Hoping to extend the core lib and weed out some bugs as I go through AoC problems πŸ™‚ Funnily enough, let-go seems to outperform other implementations on simple programs since it has very minimal startup time:

πŸ‘ 1
πŸ‘ 2
2
nooga 2021-12-01T19:01:07.083700Z

spoiler! not bad for a barebones interpreter huh?

πŸ‘ 1
2021-12-01T19:57:49.088100Z

nice!

nooga 2021-12-01T20:00:49.088400Z

and since AoC is about collecting stars, I wouldn’t mind some github stars ⭐ 😁

roelof 2021-12-01T13:11:59.075600Z

wonders if you need the clojure-tools or that advent-of-code can be solved by using leiningen

2021-12-01T13:58:28.076800Z

either will work. I can't think of any reason why you would need clojure cli tools over lein

βž• 2
Andrew Byala 2021-12-01T18:02:20.081400Z

🧡 I had a simple question about something I've seen in a few solutions for Day 1 Part 2 today. Threading to avoid partial spoilers.

Andrew Byala 2021-12-01T18:03:37.081500Z

In my solution, when I created the sum of each three-tuple, I used (map (partial apply +) triples), but I've seen some folks use (map (partial reduce +) triples). Both give the same answer, so how would you choose one over the other?

pithyless 2021-12-01T18:14:45.082Z

> Threading to avoid partial spoilers. Pun intended!

πŸ™„ 1
πŸ˜‚ 9
nooga 2021-12-01T18:18:35.082500Z

@abyala I think they’re roughly the same for small colls

☝️ 1
pithyless 2021-12-01T18:19:20.082700Z

From just an aesthetics perspective, apply X will work for any variadic function, but reduce X will work any monoid. I originally wrote apply + and then converted it to reduce + for exactly that reason: I figured there are more monoids than variadic functions, so maybe that should be the default.

tschady 2021-12-01T18:22:51.083Z

I remember reduce had significantly better benchmarks on larger colls.

Andrew Byala 2021-12-01T19:06:37.084300Z

@pithyless - your first comment was terrible, and you just made a friend today.

❀️ 1
Andrew Byala 2021-12-01T19:09:02.084800Z

Ok, so in instances when both would work, reduce is the preferred operation, even if the performance doesn't matter for smaller collection sizes. So when would you ever choose apply then?

Antonio Bibiano 2021-12-01T19:27:50.086400Z

Looking at the source looks like reduce is optimized for different types of collections while apply is just a lot of calls to cons

Antonio Bibiano 2021-12-01T19:28:36.086600Z

so I would imagine when you know that the collection is going to be quite small

Antonio Bibiano 2021-12-01T19:43:33.086800Z

or if you have to do it a lot of times! according to my not so advanced benchmarking skills

Antonio Bibiano 2021-12-01T19:43:45.087Z

(time
 (loop [n 0]
   (when (< n 1000000)
     (apply + '(1 2 3))
     (recur (inc n)))))

Antonio Bibiano 2021-12-01T19:43:57.087200Z

takes ~200 ms on my machine

Antonio Bibiano 2021-12-01T19:44:05.087400Z

switching to reduce takes 45 ms

Chase 2021-12-01T21:07:54.088900Z

I've always liked apply + because I think it highlights a great benefit of the lisp notation where you can have variadic args to the + function, etc.

Chase 2021-12-01T21:08:27.089100Z

In my head, that's what I want to use because that is how I mentally solve that particular use case but then in the back of my mind I know it is less performant than reduce and I hate that. I don't why this bothers me so much. hahaha

Chase 2021-12-01T21:09:08.089300Z

That's one thing I appreciate in Rust. I think it allows me to write code the way I want to without punishing me on performance.

pithyless 2021-12-01T21:12:19.089500Z

@antbbn it's good to reach for criterium when you need to do some quick micro-benchmarking:

(require '[criterium.core :as criterium])

(criterium/with-progress-reporting
  (criterium/bench
    (reduce + '(1 2 3))
    ;; (apply + '(1 2 3))
    ))

πŸ™Œ 1
pithyless 2021-12-01T21:12:42.089700Z

On my machine, it looks like this:

;; reduce
;;          Execution time mean : 25.832360 ns
;; Execution time std-deviation : 0.751152 ns
;;
;; apply
;;          Execution time mean : 169.971152 ns
;; Execution time std-deviation : 9.294313 ns

pithyless 2021-12-01T21:13:54.089900Z

But really, this is a microbenchmark that doesn't really mean anything unless we're in some performance critical code; and then you probably want to worry more about boxing and unchecked math ;)

βœ”οΈ 2
Chase 2021-12-01T22:09:43.090800Z

Someone created and posted a great resource to explore the solution threads by computer language: https://aocweb.yulrizka.com/

😍 2
πŸ™ 1
Chase 2021-12-01T22:10:43.091Z

APL is such a wild language

Phil Shapiro 2021-12-02T14:20:29.108100Z

Yes, there's an APL IDE, https://www.dyalog.com/. The UI has a palette that contains all (most?) symbols, and each symbol has a two key shortcut.

2021-12-01T23:57:16.091500Z

I take it APL needs you to be really familiar with keyboard shortcuts to input those weird characters into your IDE (do APL people use an IDE?) Or a keyboard with custom keycaps ?

nooga 2021-12-01T23:58:04.091700Z

The ones I’ve seen used only notepad πŸ˜›

2021-12-02T00:01:49.091900Z

I have a lot of text substitutions setup in my IDE (vs code), but I still type in regular clojure, its just displayed more concisely, I have no idea how I would even type in most of those APL characaters πŸ˜„