adventofcode

2022-12-01T05:00:09.772339Z

Day 1 - have fun!

πŸ€“ 1
😴 2
πŸ–– 5
3
πŸ₯΄ 1
mbjarland 2022-12-07T18:20:06.211129Z

(ns day-01
  (:require [clojure.string :as str]))

(defn input []
  (for [elf (str/split (slurp "../day_01.data") #"\n\n")]
   (reduce + (map parse-long (re-seq #"\d+" elf)))))

(defn solution-1 []
  (apply max (input)))

(defn solution-2 []
  (reduce + (take-last 3 (sort (input)))))

dogenpunk 2022-12-05T20:26:32.467659Z

Just realized people are posting these every day. Here’s my day 1: https://github.com/dogenpunk/advent-of-code/blob/main/src/aoc22/day01.cljc

πŸ˜„ 1
karlis 2022-12-01T05:48:25.460629Z

Here's mine for today. Nice and easy start for the month πŸ™‚ https://github.com/skazhy/advent/blob/acf852ec2ea2ec527b451c0993456c15fe8e95d5/src/advent/2022/day1.clj

πŸ‘ 1
Aleks 2022-12-01T08:57:30.058809Z

"\R\R"
❀️

carnundotcom 2022-12-01T09:52:17.979429Z

and I thought https://github.com/CarnunMP/Advent-of-Code/blob/master/src/y2022/d1.clj was terse! πŸ™ƒ

2022-12-01T10:29:32.675789Z

Day 1 easy as expected.

(defn parse-input []
  (->> (slurp "puzzle-inputs/day1")
       (str/split-lines)
       (map parse-long)
       (partition-by nil?)
       (remove #(= '(nil) %))
       (map #(apply + %))))

;; part 1
(->> (parse-input)
     (apply max))

;; part 2
(->> (parse-input)
     (sort >)
     (take 3)
     (reduce +))

tschady 2022-12-01T11:07:22.386469Z

time to update my helpers with parse-long

Martin PΕ―da 2022-12-01T12:09:07.236399Z

(ns aoc2022.day01
  (:require [clojure.string :as s])
  (:gen-class))

(defn block->calories [block]
  (transduce (map parse-long) + (s/split-lines block)))

(def data
  (->> (s/split (slurp "resources/input01.txt") #"\n\n")
       (map block->calories)))

(defn part1 [data]
  (reduce max data))

(defn part2 [data]
  (->> data
       (sort >)
       (take 3)
       (reduce +)))

πŸ‘ 4
2022-12-01T12:16:37.261739Z

That's neat, splitting on "\n\n" to get rid of havintg to partition by nil or "", then remove them

borkdude 2022-12-01T12:18:11.714399Z

My day01 in babashka and nbb: https://github.com/borkdude/advent-of-babashka/blob/main/src/aoc22/day01.cljc

mkvlr 2022-12-01T12:19:28.413329Z

here’s my day 1 with #clerk and #clerk-garden https://github.clerk.garden/mk/advent-of-clerk/commit/876b9bb61434bd632ef25fe1d39a3550209c073c/src/advent_of_clerk/day_01.html

mkvlr 2022-12-01T12:21:59.855649Z

oh I see I missed part 2

nooga 2022-12-01T12:27:44.289929Z

This is let-go, not Clojure nor CLJS:

(ns day1)

(defn parse [ls]
   (loop [[l & r] ls sum 0 out []]
     (cond
       (nil? l) out
       (empty? l) (recur r 0 (conj out sum))
       :else (recur r (+ sum (parse-int l)) out))))

(def data (->> "day1.txt" slurp lines parse (sort >)))
(println "1:" (first data))
(println "2:" (->> data (take 3) (apply +))) 

2022-12-01T12:31:53.967639Z

whats let-go ?

nooga 2022-12-01T12:32:52.955499Z

https://github.com/nooga/let-go @qmstuart

πŸ‘ 1
mkvlr 2022-12-01T12:32:58.882799Z

@borkdude thanks TIL about take-nth and sort-by - is prettier than reverse

borkdude 2022-12-01T12:35:03.496979Z

@xnooga if I change parse-int to parse-long and lines to clojure.string/split-lines the code worked in bb too :)

πŸŽ‰ 1
nooga 2022-12-01T12:36:03.024329Z

@borkdude I just literally plopped lines and parse-int into my stdlib so I could solve the AoC πŸ˜„ will clean that up some day when it's time to get my APIs closer to the real deal

borkdude 2022-12-01T12:38:16.524119Z

@xnooga maybe you can support reader conditionals, would be fun to compare solutions in different clojure dialects

πŸ€” 1
nooga 2022-12-01T12:44:14.435019Z

cool idea, I'll look into this over the weekend :D

2022-12-01T13:16:34.798179Z

Has filterv always been in clojure, or is a recent thing ?

borkdude 2022-12-01T13:17:18.693979Z

user=> (:added (meta #'filterv))
"1.4"

2022-12-01T13:17:35.858789Z

TIL πŸ™‚ Had no idea there as a v version

borkdude 2022-12-01T13:17:48.655839Z

There will also be a partitionv in 1.12 :)

πŸ’― 1
🀩 1
2022-12-01T13:19:05.732259Z

Is that going to make each bucket a vector too, or more like [(1 2) (3 4)] ?

borkdude 2022-12-01T13:21:20.988899Z

@qmstuart You can try with:

clj -Sdeps '{:deps {org.clojure/clojure {:mvn/version "1.12.0-alpha1"}}}'
and read here: https://clojure.org/news/2022/06/30/clojure1-12-alpha1

πŸ‘ 1
πŸ’― 1
Kirill Chernyshov 2022-12-01T16:21:08.280759Z

(ns aoc2022.day-1
  (:require [aoc2022.misc :as misc]))

(defprotocol Top
  (-push [this] [this x]))

(defrecord TopOne [x]
  Top
  (-push [_] x)
  (-push [this y]
    (if (> y x)
      (new TopOne y)
      this)))

(defrecord TopThree [x y z]
  Top
  (-push [_] (+ x y z))
  (-push [this a]
    (cond
      (> a x)
      (new TopThree a x y)

      (> a y)
      (new TopThree x a y)

      (> a z)
      (new TopThree x y a)

      :else
      this)))

(defn solve-1 []
  (binding [misc/*day* 1]
    (misc/read-lines
     (fn [xs]
       (transduce
        (comp (map parse-long)
              (partition-by nil?)
              (map #(reduce (fnil + 0 0) 0 %)))
        -push
        (new TopOne 0)
        xs)))))

(defn solve-2 []
  (binding [misc/*day* 1]
    (misc/read-lines
     (fn [xs]
       (transduce
        (comp (map parse-long)
              (partition-by nil?)
              (map #(reduce (fnil + 0 0) 0 %)))
        -push
        (new TopThree 0 0 0)
        xs)))))

(comment

  (time
   (dotimes [_ 10000]
     (solve-1)))

  (time
   (dotimes [_ 10000]
     (solve-2)))

  )
day one without using sort πŸ™‚

πŸ‘ 4
πŸ‘πŸ» 1
😱 1
nooga 2022-12-01T18:35:20.203279Z

need to wrap my head around implementing transducers, it would be fun to have them at the bottom, gosh I think I really need a let-go channel

peterh 2022-12-02T01:18:00.879349Z

I use the β€œAdvent of Clerk” template and really enjoy trying to figure out a nice experimental, notebook-oriented workflow. I try learning from other solutions posted here and document my insights in β€œTIL” comments. πŸ™‚ https://github.com/formsandlines/aoc2022-clojure/blob/main/src/advent_of_clerk/day_01.cljc

peterh 2022-12-02T01:22:58.447819Z

Btw, since I wanted to run tests from Babashka, I needed to exclude the Clerk import via a reader conditional, because otherwise I cannot run the test command due to an error.

Chase 2022-12-02T02:27:29.510919Z

Excited for a new AOC and to learn from all your cool solutions! https://github.com/Chase-Lambert/aoc-clojure/blob/main/src/aoc/2022/day_01.clj

jaihindhreddy 2022-12-02T04:30:58.860149Z

Sorting is a lot of unnecessary work considering that we only need top 3, especially for large datasets. Here's something that doesn't do a full-sort, much like @delaguardo did (although his method is even more bespoke):

(defn day1 [fpath]
  (let [xs (str/split (slurp fpath) #"\R\R")
        q (java.util.PriorityQueue. (count xs) (comparator >))
        _ (run! #(->> (re-seq #"\d+" %)
                   (transduce (map parse-long) +)
                   (.add q))
            xs)
        x (.poll q), y (.poll q), z (.poll q)]
    [x (+ x y z)]))
I'd appreciate any feedback πŸ˜„.

robertfw 2022-12-02T07:23:51.435149Z

after some refactoring I ended up with this as an attempt to do the task in one lazy pass

(ns adventofcode2022.day1
  (:require
   [ :as io]))

(comment
  (with-open [rdr (-> "adventofcode2022/day1.txt" io/resource io/reader)]
    (let [calorie-sums
          (sequence (comp (partition-by #{""})
                          (remove #{'("")})
                          (map #(map parse-long %))
                          (map #(reduce + %)))
                    (line-seq rdr))

          top-three
          (reduce (fn [acc x]
                    (if (< (first acc) x)
                      (sort (conj (rest acc) x))
                      acc))
                  '(0 0 0)
                  calorie-sums)]
      {:part1 (last top-three)
       :part2 (reduce + top-three)}))
  ;; => {:part1 69310, :part2 206104}
  )

Slackbot 2022-12-01T12:08:04.301489Z

This message was deleted.

mkvlr 2022-12-01T12:20:16.145019Z

sorry, didn’t know & move it there

tschady 2022-12-01T12:13:09.090319Z

> A group of a thousand elves on an expedition carrying backpacks filled to the brim --v 4 > By MidJourney > https://www.reddit.com/r/adventofcode/comments/z9g0i0/ai_imagine_advent_of_code_2022_day_1/

πŸ˜„ 12
borkdude 2022-12-01T12:15:22.167489Z

I created a repository to help with doing Advent of Code in #babashka and #nbb! https://github.com/borkdude/advent-of-babashka-template Spoiler alert: it contains a solution for day 1.

Ellis 2022-12-01T12:26:50.895899Z

I did something similar that also pulls the input file too. All you need is to curl the URL and pass the cookie in a header https://github.com/elken/aoc (contains solutions so spoiler alert)

borkdude 2022-12-01T12:27:38.233359Z

oh, we can update this repo to do that

borkdude 2022-12-01T12:27:59.535289Z

with bb new-day --token ....

borkdude 2022-12-01T12:28:04.633659Z

PR welcome

Ellis 2022-12-01T12:28:31.854179Z

Nice, yeah I will when I'm off the train 😁

genmeblog 2022-12-01T13:58:14.431069Z

Just a reminder: there exists #babashka script made by @tws which produces such nice badges:

🀯 3
genmeblog 2022-12-01T13:59:46.172349Z

original source: https://github.com/tschady/advent-of-code/blob/main/script/update_badges.clj

borkdude 2022-12-01T14:03:12.239619Z

Oh, it would be nice to have a PR with a link to this in the repo I just created...

borkdude 2022-12-01T14:03:21.349829Z

or maybe integrate it in that repo

πŸ‘ 1
tschady 2022-12-01T15:15:26.259879Z

now you’re going to force me to clean that up crying-laughing-joy-blood

tschady 2022-12-01T15:17:42.578709Z

I execute with bb badges

{:paths ["resources" "script"]
 :tasks {badges   {:doc  "Updates README badges with latest puzzle completion stats."
                   :task (shell "bb script/update_badges.clj")}

borkdude 2022-12-01T15:20:31.057179Z

Cool :) You can also do (load-file "script/...")

genmeblog 2022-12-01T15:21:43.608259Z

Don't forget that you have to download session cookie manually to use this script.

tschady 2022-12-01T15:42:41.912889Z

and get the favicon locally

borkdude 2022-12-01T15:43:27.020499Z

Maybe someone can make this a proper library so it can be used in bb.edn

tschady 2022-12-01T15:44:19.881229Z

it’s on my list, along with downloading input: https://github.com/tschady/advent-of-code/blob/main/script/download_input.clj This was just for my use, i didn’t librify it yet.

tschady 2022-12-01T15:44:35.366929Z

bb dl 2022 1

borkdude 2022-12-01T15:44:36.427369Z

yeah, no obligations of course, just would be nice to have for people :)

2022-12-02T06:42:08.371579Z

Great script! Eric has requested that we all put a "User-Agent" field into our requests from scripts. I've modified your script to do that here: https://github.com/alexalemi/advent/blob/main/scripts/badges.clj#L32

2022-12-02T06:42:26.155379Z

I also have a little input fetching utility: https://github.com/alexalemi/advent/blob/main/scripts/utils.clj

2022-12-02T06:43:27.514979Z

where I try to be nice and ensure that I don't make more than one request by checking to ensure that it is the proper time and not requesting the input once I've already gotten it. In this way shortly before midnight I can just run watch bb fetch (I set up a bb task) and it'll only make one request to the page and grab the input while I'm reading the problem

Apple 2022-12-01T15:54:16.111179Z

how do you make this nicer

(->> x
             (map #(map parse-long %))
             (map #(reduce + %))
the two maps can you combine them?

mbjarland 2022-12-07T18:27:54.891779Z

yes, what @zelark said, this was mine:

(ns day-01
  (:require [clojure.string :as str]))

(defn input []
  (for [elf (str/split (slurp "../day_01.data") #"\n\n")]
   (reduce + (map #(Integer/parseInt %) (re-seq #"\d+" elf)))))

(defn solution-1 []
  (apply max (input)))

(defn solution-2 []
  (reduce + (take-last 3 (sort (input)))))

Apple 2022-12-01T15:56:00.129639Z

(let [x (->> (slurp "src/y2022/input202201")
             (re-seq #"\d+|\n\n")
             (partition-by #{"\n\n"})
             (remove #{["\n\n"]})
             (map #(map parse-long %))
             (map #(reduce + %))
             sort
             reverse
             (take 3))]
  [(first x) (reduce + x)])
my solution for day1

borkdude 2022-12-01T16:00:21.845419Z

you can use transducers for to speed things up

borkdude 2022-12-01T16:00:38.080049Z

but in this case it's not important, but it is a way to fold those maps

borkdude 2022-12-01T16:00:57.073939Z

fwiw, I haven't used it in my solution

Ellis 2022-12-01T16:11:46.582249Z

I've tried to force myself to use transducers more this year as a learning exercise, but I already failed lol Edit: found a way https://github.com/elken/aoc/blob/master/src/solutions/2022/day01.clj

liebs 2022-12-01T16:38:45.040399Z

I was just about to give clojure.walk/walk a try for this, not sure if it will work. TBD

2022-12-01T16:46:34.990949Z

To me, for cries out as the most idiomatic way to express this, and for just two operations I might not bother threading:

(for [group x]
  (reduce + (map parse-long group)))

2022-12-01T16:47:34.237219Z

Or with the threading

(for [group x]
  (->> group
       (map parse-long)
       (reduce +)))

Aleks 2022-12-01T18:08:16.229329Z

(defn parse-input [input]
  (map (fn [xs]
         (->> (re-seq #"\d+" xs)
              (transduce (map parse-long) +)))
       (str/split input #"\R\R")))

βœ… 3