This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-12-01
Channels
- # adventofcode (77)
- # announcements (1)
- # architecture (6)
- # babashka (16)
- # beginners (28)
- # calva (2)
- # cider (5)
- # clj-kondo (28)
- # clojure (63)
- # clojure-europe (39)
- # clojure-nl (1)
- # clojure-norway (10)
- # clojure-uk (3)
- # clojurebridge (2)
- # clojurescript (23)
- # cursive (1)
- # datalevin (1)
- # dev-tooling (2)
- # emacs (16)
- # events (3)
- # fulcro (8)
- # guix (3)
- # honeysql (16)
- # introduce-yourself (1)
- # jobs (1)
- # joyride (91)
- # lambdaisland (1)
- # lsp (18)
- # membrane (1)
- # nbb (1)
- # off-topic (62)
- # pathom (4)
- # portal (11)
- # rdf (4)
- # re-frame (4)
- # releases (3)
- # shadow-cljs (55)
- # tools-deps (10)
- # xtdb (3)
- # yada (5)
Here's mine for today. Nice and easy start for the month 🙂 https://github.com/skazhy/advent/blob/acf852ec2ea2ec527b451c0993456c15fe8e95d5/src/advent/2022/day1.clj
"\R\R"
❤️and I thought https://github.com/CarnunMP/Advent-of-Code/blob/master/src/y2022/d1.clj was terse! 🙃
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 +))
https://github.com/genmeblog/advent-of-code/blob/master/src/advent_of_code_2022/day01.clj
https://github.com/benjamin-asdf/advent-of-code-2021/blob/master/src/Y2022/calories.clj
(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 +)))
That's neat, splitting on "\n\n" to get rid of havintg to partition by nil or "", then remove them
My day01 in babashka and nbb: https://github.com/borkdude/advent-of-babashka/blob/main/src/aoc22/day01.cljc
here’s my day 1 with #C035GRLJEP8 and #C04ATSH4QNM https://github.clerk.garden/mk/advent-of-clerk/commit/876b9bb61434bd632ef25fe1d39a3550209c073c/src/advent_of_clerk/day_01.html
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 +)))
@U04V15CAJ thanks TIL about take-nth and sort-by -
is prettier than reverse
@UJEK1RGJ0 if I change parse-int
to parse-long
and lines
to clojure.string/split-lines
the code worked in bb too :)
@U04V15CAJ 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
@UJEK1RGJ0 maybe you can support reader conditionals, would be fun to compare solutions in different clojure dialects
@U013YN3T4DA 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(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 🙂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
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
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.
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
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 @U04V4KLKC 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 😄.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}
)
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
(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)))))
here’s my day 1 with #C035GRLJEP8 and #C04ATSH4QNM https://github.clerk.garden/mk/advent-of-clerk/commit/876b9bb61434bd632ef25fe1d39a3550209c073c/src/advent_of_clerk/day_01.html
> 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/
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.
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)
Just a reminder: there exists #CLX41ASCS script made by @tws which produces such nice badges:
original source: https://github.com/tschady/advent-of-code/blob/main/script/update_badges.clj
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")}
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.
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
I also have a little input fetching utility: https://github.com/alexalemi/advent/blob/main/scripts/utils.clj
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
how do you make this nicer
(->> x
(map #(map parse-long %))
(map #(reduce + %))
the two maps can you combine them?(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 day1I'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
I was just about to give clojure.walk/walk
a try for this, not sure if it will work. TBD
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)))
(defn parse-input [input]
(map (fn [xs]
(->> (re-seq #"\d+" xs)
(transduce (map parse-long) +)))
(str/split input #"\R\R")))
yes, what @U067R559Q 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)))))