This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-12-03
Channels
- # adventofcode (23)
- # announcements (2)
- # babashka (54)
- # babashka-sci-dev (2)
- # beginners (14)
- # biff (5)
- # calva (25)
- # cherry (9)
- # clj-kondo (4)
- # clojure-europe (2)
- # clojure-japan (2)
- # clojurescript (1)
- # data-science (6)
- # datascript (3)
- # datomic (1)
- # dev-tooling (25)
- # etaoin (5)
- # events (5)
- # hyperfiddle (1)
- # nbb (2)
- # off-topic (27)
- # shadow-cljs (1)
- # xtdb (7)
(ns slothrop.day-three.rucksacks
(:require [ :as io]
[clojure.string :as string]
[clojure.set :refer [intersection]]))
(def lower (zipmap (map char (range 97 123)) (range 1 27)))
(def upper (zipmap (map char (range 65 91)) (range 27 53)))
(with-open [rdr (-> "public/puzzle_inputs/day_three.txt"
io/resource
io/reader)]
(->> rdr
line-seq
(partition 3) ; pt 2
#_(map #(split-at (/ (count %) 2) %)) ; pt 1
(map (fn [[s1 s2 s3]] (intersection (set s1) (set s2) (set s3))))
(map #(if (Character/isUpperCase (first %))
(get upper (first %))
(get lower (first %))))
(apply +)))
Continuing to phone it in on readability...(ns aoc2022.day03
(:require [clojure.string :as s]
[clojure.set :as set])
(:gen-class))
(def data (-> (slurp "resources/input03.txt")
(s/split-lines)))
(defn priority [^Character c]
(- (int c) (if (Character/isLowerCase c) 96 38)))
(defn common-char-with-rating [group]
(->> (map set group)
(apply set/intersection)
first
priority))
(defn line-get-common-char [line]
(let [c (count line)]
(common-char-with-rating (split-at (/ c 2) line))))
(defn part1 [data]
(transduce (map line-get-common-char)
+
data))
(defn part2 [data]
(transduce (map common-char-with-rating)
+
(partition 3 data)))
Nothing very interesting.
(ns com.jaihindhreddy.advent22
(:require [clojure.string :as str]
[clojure.set :as set]))
(defn day3 [fs]
(let [priority (fn [c]
(let [x (int c)]
(- x (if (> x 96) 96 38))))
lines (str/split-lines fs)]
[(transduce
(map #(let [n (quot (count %) 2)]
(priority
(first
(set/intersection
(set (subs % 0 n))
(set (subs % n)))))))
+ 0 lines)
(transduce
(comp (partition-all 3)
(map #(priority (first (apply set/intersection (map set %))))))
+ 0 lines)]))
My https://github.com/abyala/advent-2022-clojure/blob/main/docs/day03.md with linked https://github.com/abyala/advent-2022-clojure/blob/main/src/advent_2022_clojure/day03.clj.
(defn char-range [start end]
(map char (range (int start) (inc (int end)))))
(def priorities
(zipmap
(concat (char-range \a \z) (char-range \A \Z))
(range 1 53)))
(defn parse-rucksack [rs]
(let [rs (seq rs)]
(split-at (/ (count rs) 2) rs)))
(defn item-priority [col]
(->> col
(map set)
(apply clojure.set/intersection)
(first)
(priorities)))
(defn day3-part1 []
(->> (string/split-lines (slurp "input/day-3-input-1.txt"))
(map parse-rucksack)
(map item-priority)
(reduce +)))
(defn day3-part2 []
(->> (string/split-lines (slurp "input/day-3-input-1.txt"))
(partition 3)
(map seq)
(map item-priority)
(reduce +)))
;; part1
(defn crr26 [c] (map char (range (int c) (+ (int c) 26))))
(def priorities (zipmap (concat (crr26 \a) (crr26 \A))
(range 1 53)))
(defn solvgrp [grp]
(->> (map set grp)
(apply set/intersection)
first
(get priorities)))
(->> (map #(split-at (/ (count %) 2) %) input)
(map solvgrp)
(apply +)) ;; 7850
;; part 2
(apply + (map solvgrp (partition 3 input))) ;; 2581
Nice solutions all. Haven't seen anybody use str/index-of
for item-type score fn:
(fn [it]
(-> "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
(str/index-of it)
inc))
I'm using it (without inc
):
(defn pts [it]
(->> it (map set) (apply intersection) first
(index-of ":abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")))
(defn -main [day]
(let [input (file->lines day)]
{:part1 (transduce (map #(pts (partition (/ (count %) 2) %))) + input)
:part2 (transduce (map pts) + (partition 3 input))}))
Here is mine, now in the https://github.clerk.garden: https://github.clerk.garden/formsandlines/aoc2022-clojure/commit/1dd601cb5f5d590162e9cb3ab62c68c78e83f3a6/src/advent_of_clerk/day_03.html Pretty much what everyone did, I guess, using intersection on sets. I converted the type mathematically on the char code, although not as sophisticated as the hack by tschadys coworker. :)
(ns day-03)
(def priority
(merge (zipmap "abcdefghijklmnopqrstuvwxyz" (range 1 27))
(zipmap "ABCDEFGHIJKLMNOPQRSTUVWXYZ" (range 27 53))))
(def lines (re-seq #".+" (slurp "../day_03.data")))
(defn input-1 []
(for [line lines
:let [c (count line)
h (/ c 2)
[a b] [(subs line 0 h) (subs line h c)]]]
(some (set a) b)))
(defn input-2 []
(for [[a b c] (partition 3 lines)]
(some (set (filter (set a) b)) c)))
(defn solution-1 []
(reduce + (map priority (input-1))))
(defn solution-2 []
(reduce + (map priority (input-2))))
Very late. I tend to like these long pipelines of transformations instead of breaking them up. Inscrutable but fun. Everyone seems to have had the same idea though.
(ns aoc2022.day3
(:require [ :refer [resource]]
[clojure.string :as str]
[clojure.set :refer [intersection]]))
(defn data []
(->> (resource "inputs/day3.txt")
(slurp)
(str/split-lines)))
(defn part1 []
(->> (data)
(map #(vector (into #{} (subs % 0 (/ (count %) 2)))
(into #{} (subs % (/ (count %) 2) (count %)))))
(map (partial apply intersection))
(map (comp int first))
(map #(if (> % 96) (- % 96) (- % 38)))
(reduce +)))
(defn part2 []
(->> (data)
(map (partial into #{}))
(partition 3)
(map (partial apply intersection))
(map (comp int first))
(map #(if (> % 96) (- % 96) (- % 38)))
(reduce +)))
Wild, part 1 was "solved" in 10 seconds: https://twitter.com/ostwilkens/status/1598458146187628544
Day 3 generated by chatGPT. Many errors in Clojure code:
(defn priority [ch]
(let [offset (if (Character/isLowerCase ch) 96 64)]
(- (char ch) offset)))
(defn solve [input]
(let [counts (frequencies input)]
(first (sort-by (comp second >) counts))))
(def input ["vJrwpWtwJgWrhcsFMMfFFhFp"
"jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL"
"PmmdzqPrVvPwwTWBwg"
"wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn"
"ttgJtRGJQctTZtZT"
"CrZsJsPPZsGzwwsLwLmpwMDw"])
(let [common-item (solve input)]
(println "The common item is" common-item)
(println "Its priority is" (priority common-item))
(println "The sum of the priorities is" (* (count input) (priority common-item))))
@UHZPYLPU1 what question do you ask to chatGPT?