Day 1 - have fun!
(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)))))Day 1 https://github.com/bhauman/advent-of-code-2022/blob/main/src/adv2022/day1/sol.clj
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
https://gitlab.com/maximoburrito/advent2022/-/blob/main/src/day01/main.clj
Here's mine for today. Nice and easy start for the month π https://github.com/skazhy/advent/blob/acf852ec2ea2ec527b451c0993456c15fe8e95d5/src/advent/2022/day1.clj
https://github.com/rap1ds/advent-of-code-2022/blob/main/src/day1.clj
https://github.com/nbardiuk/adventofcode/blob/master/2022/src/day01.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
https://github.com/tschady/advent-of-code/blob/main/src/aoc/2022/d01.clj
time to update my helpers with parse-long
(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 #clerk and #clerk-garden https://github.clerk.garden/mk/advent-of-clerk/commit/876b9bb61434bd632ef25fe1d39a3550209c073c/src/advent_of_clerk/day_01.html
oh I see I missed part 2
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 +))) whats let-go ?
@borkdude thanks TIL about take-nth and sort-by - is prettier than reverse
@xnooga if I change parse-int to parse-long and lines to clojure.string/split-lines the code worked in bb too :)
@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
@xnooga maybe you can support reader conditionals, would be fun to compare solutions in different clojure dialects
cool idea, I'll look into this over the weekend :D
Has filterv always been in clojure, or is a recent thing ?
user=> (:added (meta #'filterv))
"1.4"TIL π Had no idea there as a v version
There will also be a partitionv in 1.12 :)
Is that going to make each bucket a vector too, or more like [(1 2) (3 4)] ?
@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(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 @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 π.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}
)
This message was deleted.
sorry, didnβt know & move it there
> 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)
oh, we can update this repo to do that
with bb new-day --token ....
PR welcome
Nice, yeah I will when I'm off the train π
Just a reminder: there exists #babashka script made by @tws which produces such nice badges:
https://github.com/genmeblog/advent-of-code/blob/master/badges/badges.bb
original source: https://github.com/tschady/advent-of-code/blob/main/script/update_badges.clj
Oh, it would be nice to have a PR with a link to this in the repo I just created...
or maybe integrate it in that repo
now youβre going to force me to clean that up crying-laughing-joy-blood
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")}Cool :) You can also do (load-file "script/...")
Don't forget that you have to download session cookie manually to use this script.
and get the favicon locally
Maybe someone can make this a proper library so it can be used in bb.edn
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.
bb dl 2022 1
yeah, no obligations of course, just would be nice to have for people :)
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?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)))))(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 day1you can use transducers for to speed things up
but in this case it's not important, but it is a way to fold those maps
fwiw, I haven't used it in my solution
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
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)))Or with the threading
(for [group x]
(->> group
(map parse-long)
(reduce +)))(defn parse-input [input]
(map (fn [xs]
(->> (re-seq #"\d+" xs)
(transduce (map parse-long) +)))
(str/split input #"\R\R")))