Fork me on GitHub
#adventofcode
<
2022-12-03
>
norman05:12:03

Day 3 - solutions

norman05:12:02

Oh - intersection can be passed more than two sets - how did I miss that. 🙂

Ben Lieberman05:12:47

(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...

Martin Půda07:12:02

(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)))

jaihindhreddy07:12:03

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)]))

Luis Santos07:12:16

(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 +)))

mchampine08:12:37

;; 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

pieterbreed09:12:53

Nice solutions all. Haven't seen anybody use str/index-of for item-type score fn:

(fn [it]
  (-> "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
      (str/index-of it)
      inc))

👍 1
Piotr Kaznowski10:12:09

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))}))

1
peterh11:12:47

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. :)

mbjarland09:12:34

(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))))

dumrat16:12:01

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 +)))

👍 1
J07:12:16

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))))

👀 2
erwinrooijakkers14:12:38

@UHZPYLPU1 what question do you ask to chatGPT?

J14:12:48

The all aoc day3 state. I only add in Clojure at the end of the part1 question

tschady11:12:45

Did I miss the memo about Advent-of-Transduce? 🎅

😅 2
👍 1