adventofcode

pez 2022-12-06T09:53:11.148599Z

If someone would like the horrible experience of using JavaScript instead of Clojure... ๐Ÿ˜ƒ At least it supports reloading of JavaScripts code. Any ideas of how to integrate ChatGPT in a cool way would be awesome. Please ๐Ÿงต . I asked ChatGPT about integrating with its API and it seems pretty straight forward, especially since it gave me the code for it.

braai engineer 2022-12-06T10:02:33.123099Z

Sup Clojurians. Here are my 2022 Advent of Code solutions in Clojure: https://github.com/theronic/advent-of-code/tree/main/src/advent_of_code/year2022

2
๐Ÿ‘‹ 1
tschady 2022-12-06T11:42:34.445329Z

PSA for those new to some of the core functions people use in these solutions: Study the https://clojure.org/api/cheatsheet . e.g. Have a sequence, and want to get some things from the front? go to โ€œSeq In, Seq Outโ€ -> โ€œHead Itemsโ€ and checkout the list: take take-while butlast drop-last for.

๐Ÿ‘ 2
โค๏ธ 2
โ˜๏ธ 1
pwojnowski 2022-12-06T12:38:40.359039Z

I totally forgot about distinct? and really miss basic factory function for queue (sth like (def queue [coll]... (clojure.lang.PersistentQueue ...)

genmeblog 2022-12-06T12:44:08.081329Z

me too! distinct? and keep-indexed this time (`re-seq` yesterday)

genmeblog 2022-12-06T12:44:19.962489Z

every day something new ๐Ÿ™‚

๐Ÿ‘ 1
scottbale 2022-12-06T16:21:12.980179Z

Kind of a meta-point: over time I've come up with a bit of https://github.com/scottbale/advent-of-code/blob/main/clojure/resources/template.clj.txt to tackle each day's puzzle, and I like it so much that this year I wrote a little https://github.com/scottbale/advent-of-code/blob/main/clojure/src/user.clj to generate each day's new one. It's not much boilerplate, but it takes care of the little bit of file I/O fiddliness and my experience becomes, I just hop into the REPL and start hacking first using the puzzle's hardcoded sample data, and then more often than not it works on the first try with my personalized input (which I have to copy into the resource file). So for example day 5 looks like this at the start:

(ns aoc-2022.day5
  "docstring"
  (:require
   [ :as io]))

(defn runner
  "runner docstring"
  [input]
  )


(comment

  (runner ["    [D]"    
           "[N] [C]"    
           "[Z] [M] [P]"
           " 1   2   3" 
           ""
           "move 1 from 2 to 1"
           "move 3 from 1 to 3"
           "move 2 from 2 to 1"
           "move 1 from 1 to 2"])

  (with-open [r (io/reader (io/resource "aoc-2022/day5.txt"))]
    (runner (line-seq r)))

  )

๐Ÿ‘€ 1
๐Ÿ™ 1
Ellis 2022-12-06T17:54:11.502149Z

I have a bb script to create a new day from a template with all my clerk boilerplate

1
Ellis 2022-12-06T17:54:52.492139Z

https://github.com/elken/aoc/blob/master/resources/template.clj

๐Ÿ‘€ 2
Ellis 2022-12-06T17:55:07.780499Z

https://github.com/elken/aoc/blob/master/bb.edn

pithyless 2022-12-06T18:41:27.837259Z

Neat! I started building something that parses the html to markdown and pastes that into clerk, but I might just steal your idea of using hickory and using the clerk/html viewer. The downside is that the prose is not visible in the editor, but the upside is that the prose is not visible in the editor. ๐Ÿค”

Ellis 2022-12-06T18:43:48.500379Z

Yeah I did it manually at first but it's tedious to do and wastes space. You can have the notebook in an xwidgets browser frame of Emacs is compiled with it (which is how I work)

pithyless 2022-12-06T18:47:37.253349Z

I'm old-fashioned and still use an external browser for my Portal and Clerk. :) I managed to automate the markdown parsing with this: https://github.com/simon-brooke/html-to-md But it's just a hacky POC at the moment and I'm not aware of what the sharp edges are (OTOH the AOC html is relatively straightforward so shouldn't be too many issues going forward). I will play with both approaches when I find some time in the coming days and report back. Thanks for sharing @elken!

๐Ÿ˜„ 1
tschady 2022-12-07T01:24:18.101599Z

bb has Selmer built-in, might want to check that out. (ns aoc.{{year}}.d{{day}} is convenient.

peterh 2022-12-06T23:03:45.537949Z

Iโ€™ve seen someone using clojure.lang.PersistentQueue in todays puzzle, so I wondered if this data structure is something that you use frequently in Clojure and if there are any caveats to be aware of (other than from the general properties of queues). I already explored how it behaves with conj, peek and pop, which is as expected from a queue (first-in-first-out). I also see that seq works on queues and so they can be mapped, filtered, etc. and you can use (into clojure.lang.PersistentQueue/EMPTY โ€ฆ) to get a queue from a seq or vector. They also seem to work with Babashka.

scottbale 2022-12-06T23:59:50.553939Z

Funny you should mention it: earlier this very day, I was mentioning to someone that, although I've been programming Clojure since 2011, until I very recently read chapter 2 of Clojure Applied I did not even know of the existence of clojure.lang.PersistentQueue. ๐Ÿคท

wevrem 2022-12-07T01:51:10.562469Z

<sample-of-one>I looked through my past AoC solutions and Iโ€™ve used it about once a year on average. In a full stack production app Iโ€™ve been working on, havenโ€™t used it at all.</sample-of-one>

AC 2022-12-07T03:29:56.040699Z

anecdotally (looking through my AoC history), I have used it a couple times for path-finding puzzles but it looks like I switched to clojure.data.priority-map at some point. Iโ€™m not a clojure expert in any way, so I canโ€™t say whether either was the right way to go. (other than they did do the job asked of them)

bhauman 2022-12-06T00:16:58.319759Z

If you all havenโ€™t seen this, itโ€™s really worth looking at. It demonstrates the interaction with chatGPT to solve advent problems. https://note89.github.io/the-advent-of-code-ai-edition/ via @cfleming

๐Ÿ˜ฌ 2
2
2022-12-06T07:14:06.571229Z

Thanks for sharing! The day 4 prompt is revelatory: > Write instructions for the following challenge in such a way that a large language model like yourself can take those instructions and produce a program that creates the right output when run. The program needs to read from a file called input.txt Reminds me of starting to code straight from a business request vs. doing a planning session first and getting requirements straight.

Apple 2022-12-06T05:13:33.907209Z

Day 6 - Solutions

dumrat 2022-12-23T04:28:19.340899Z

(ns aoc2022.day6
  (:require [ :refer [resource]]
            [clojure.string :as str]
            [clojure.set :refer [intersection]]))

(defn data []
  (->> (resource "inputs/day6.txt")
       (slurp)))

(defn solution [s len]
  (let [start-3 (into [] (take len s))
        except-start-3 (into [] (drop len s))]
    (+ len
       (reduce (fn [[l idx] e]
                 (tap> [l idx])
                 (if (apply distinct? (conj l e))
                   (reduced idx)
                   [(conj (subvec l 1) e) (inc idx)]))
               [(into [] start-3) 1]
               except-start-3))))

(def part1 #(solution % 3))
(def part2 #(solution % 13))

J 2022-12-06T08:18:19.144539Z

(defn solve [input nb]
  (reduce (fn [acc item]
            (if (= (count item) nb)
              (reduced (+ acc nb))
              (inc acc)))
          0
          (map set (partition nb 1 input))))

(defn part1 [input]
  (solve input 4))

(defn part2 [input]
  (solve input 14))

๐Ÿ‘ 3
genmeblog 2022-12-06T08:45:09.937769Z

(defn marker-id
  [data marker-length]
  (->> (partition marker-length 1 data)
       (map (comp count set))
       (map-indexed vector)
       (sort-by second >)
       (first)
       (reduce +)))

2022-12-06T08:47:50.224749Z

Todays was a LOT easier than yesterday ๐Ÿ™‚ https://github.com/stuartstein777/clj-advent-of-code/blob/master/src/stuartstein777/2022/day6.clj

babardo 2022-12-06T09:07:02.525639Z

https://github.com/v-garcia/aoc_2022/blob/main/day6.clj

๐Ÿ‘ 1
2022-12-06T09:14:10.668069Z

(defn solve [it len]
  (let [f (fn [ii] ((juxt #(take len %) #(drop 1 %)) ii))]
    (loop [[a b] (f it), pos len]
      (if (= len (count (set a))) pos
          (recur (f b) (inc pos))))))

(defn -main [day]
  (let [input (file->str day)
        result (partial solve input)]
    (zipmap [:part1 :part2] (map result [4 14]))))

Valentin Mouret 2022-12-06T09:49:57.786989Z

Itโ€™s so nice to learn about all these functions from your examples (`distinct?`, take-while, keep-indexedโ€ฆ). In https://github.com/ValentinMouret/advent-2022/blob/main/src/day_06.clj, I used reduce and reduced and I donโ€™t think I have seen it so far.

๐Ÿ‘ 1
tschady 2022-12-06T10:42:35.928109Z

Variations on above. This was the first day I used my bb submit to send in the answers. Next up, submit direct from emacs cider https://github.com/tschady/advent-of-code/blob/main/src/aoc/2022/d06.clj

๐Ÿ‘ 2
๐Ÿ‘๐Ÿป 1
Benjamin 2022-12-06T10:43:40.281189Z

I am happy that today my solution is almost the same as @tws

๐Ÿคช 1
pwojnowski 2022-12-06T11:01:22.619489Z

Maybe I missed something, but why in Clojure there's no queue function to create a queue?

pwojnowski 2022-12-06T11:01:36.369419Z

Mine: > (defn day-06 [input n] > (loop [seen (apply conj (clojure.lang.PersistentQueue/EMPTY) (take n input)) i n] > (if (= (count (set seen)) n) > i > (recur (conj (pop seen) (get input i)) (inc i))))) > > (defn day-06-1 [input] > (day-06 input 4)) > > (defn day-06-2 [input] > (day-06 input 14))

2022-12-06T11:03:52.937699Z

You have PersistentQueue

2022-12-06T11:04:10.943639Z

but I guess you can mimic simple queues with vectors

jaihindhreddy 2022-12-06T11:31:33.443829Z

I couldn't resist not using juxt ๐Ÿ˜‚:

(defn day6 [fs]
  ((juxt #(% 4) #(% 14))
   (fn [k]
     (->> (partition k 1 fs)
       (keep-indexed
         #(when (apply distinct? %2)
            (+ % k)))
       first))))

๐Ÿ˜ 1
lassemaatta 2022-12-06T11:36:07.715289Z

I've been using AoC as an opportunity to finally learn how to use transducers. For example https://github.com/lassemaatta/clj-aoc-2022/blob/master/src/lassemaatta/aoc_2022/day06.clj my solution for day 6.

๐Ÿ‘ 1
schadocalex 2022-12-06T13:17:47.110749Z

(->> (slurp "day6.txt")
     (partition 14 1)
     (map (comp count set))
     (#(.indexOf % 14))
     (+ 14))

๐Ÿ‘ 4
nooga 2022-12-06T13:25:24.755559Z

https://github.com/nooga/aoc2022/blob/master/day6.lg

(ns day6)

(def data (slurp "input/day6.txt"))

(defn solve [n]
  (->> data (partition n 1) (map (comp #{n} count set)) (take-while not) count (+ n)))

(println "1:" (solve 4))
(println "2:" (solve 14))
helped me to fix my comp implementation ๐Ÿ™‚

carnundotcom 2022-12-06T14:34:55.407649Z

https://github.com/CarnunMP/Advent-of-Code/blob/master/src/y2022/d6.clj, eh? the essence of it:

(defn find-marker [n input]
  (->> (partition n 1 input)
       (keep-indexed #(when (apply distinct? %2) %1))
       first
       (+ n)))

2022-12-06T14:35:48.412849Z

I think this just means tomorrow will be brutal :)

๐Ÿ˜ฏ 2
carnundotcom 2022-12-06T14:36:42.303889Z

indeed... has felt like the calm before the storm for a while now

nooga 2022-12-06T14:36:48.998699Z

first week is always kinda like this and then it turns into a full time job

๐Ÿ˜† 1
๐Ÿ˜„ 4
2022-12-06T14:52:16.847859Z

What I spent 5 minutes debugging last night... Zipmap maps aren't ordered.

day06.main> (for [[n] (zipmap (range) "abcdedghijklmn")] n)
(0 7 1 4 13 6 3 12 2 11 9 5 10 8)
day06.main> (for [[n] (map-indexed vector "abcdefghijklmn")] n)
(0 1 2 3 4 5 6 7 8 9 10 11 12 13)
keep-indexed wasn't really in my vocabulary, but now that I see those solutions, I will definitely start using it ore

bhauman 2022-12-06T14:57:56.518779Z

@xnooga yeah the perf of let-go is going to definitely get tested for sure

๐Ÿซฃ 1
nooga 2022-12-06T15:18:53.548879Z

bring it on I say

๐Ÿ˜‚ 1
dogenpunk 2022-12-06T18:16:46.757169Z

Danggit! I always forget partition can take a size argumentโ€ฆ

Luis Santos 2022-12-06T18:23:11.360089Z

(defn sliding-window [seq length]
  (loop [result ()
         remaining seq]
    (let [chunk (take length remaining)]
      (if (< (count chunk) length)
        (reverse result)
        (recur (cons chunk result) (rest remaining))))))


(->> (sliding-window (slurp "input/day-6-input.txt") 14)
     (reduce (fn [acc n]
               (if (= (count (set n)) 14)
                 (reduced (+ acc (count n)))
                 (inc acc )))
             0))
I wont forget that partition trick. ๐Ÿ˜„

Michael Ducharm 2022-12-06T18:24:30.620029Z

I was looking for an equivalent of rust's windows function, and learned that partition could do that. Very handy!

robertfw 2022-12-06T19:25:45.710409Z

I went with a queue for this one but loving the succinct partition answer!

(defn find-marker [stream marker-len]
  (reduce-kv (fn [acc k v]
               (cond
                 (< (count acc) marker-len) (conj acc v)
                 (= marker-len (count (into #{} acc))) (reduced k)
                 :else (conj (pop acc) v)))
             (clojure.lang.PersistentQueue/EMPTY)
             (into [] stream)))

peterh 2022-12-06T21:05:31.816689Z

(defn partition-till-detected [n s]
  (take-while (complement (partial apply distinct?))
              (partition n 1 s)))

(defn count-to-marker [n s]
  (+ n (count (partition-till-detected n s))))

(count-to-marker 4 input)  ;; part 1
(count-to-marker 14 input) ;; part 2

Apple 2022-12-06T05:13:59.533889Z

;; 202206
(let [d (slurp "resources/202206")
      f #(->> (partition % 1 d)
              (keep-indexed (fn [i x]
                              (if (= % (count (set x)))
                                (+ % i))))
              first)]
  (map f [4 14]))
;; (1850 2823)

๐Ÿ‘ 4
๐Ÿ™Œ 4
2022-12-06T05:16:31.919729Z

https://gitlab.com/maximoburrito/advent2022/-/blob/main/src/day06/main.clj worst performance yet

mchampine 2022-12-06T05:29:16.928129Z

;; Part 1
(defn sop [n s]
  (let [cs (partition-all n 1 s)
        mk (apply str (first (filter #(= % (distinct %)) cs)))]
    (+ n (str/index-of s mk))))

(sop 4 input) ;; 1343

;; Part 2
(sop 14 input) ;; 2193

2022-12-06T05:38:30.010259Z

https://github.com/rap1ds/advent-of-code-2022/blob/main/src/day6.clj And happy independence day Finland ๐Ÿ‡ซ๐Ÿ‡ฎ!

๐ŸŽ‰ 4
nbardiuk 2022-12-06T05:41:56.663669Z

TIL distinct? https://github.com/nbardiuk/adventofcode/blob/master/2022/src/day06.clj

๐ŸŽ‰ 1
2022-12-06T06:58:00.951959Z

https://github.com/volesen/aoc2022/blob/main/day6/day6.clj

๐Ÿ‘ 2
2022-12-06T07:03:56.406009Z

Keeping with the advent-of-transducers theme

liebs 2022-12-06T07:06:46.191979Z

this regrettably only got me the first part but gonna keep working with it

(def input (-> "public/puzzle_inputs/day_six.txt" io/resource slurp))

(defn process-data [n s]
  (let [packets (map #(apply str %) (partition n 1 s))
        unique-packets (map (comp #(apply str %) distinct) packets)
        nums (filter some? (map #(if (= %1 %2) %3 nil)
                   packets unique-packets (range)))]
    (first (drop n nums))))

2022-12-06T07:12:25.553709Z

I think changing your last line to (+ (first nums) n) should do it

๐Ÿ‘€ 1
motform 2022-12-06T07:37:41.379619Z

Seems like I missed out on the take-while train ๐Ÿ˜… https://github.com/motform/advent-of-clojure/blob/master/src/advent_of_clojure/2022/06.clj