(ns edition2022.day3
(:require [clojure.string :as str]
[clojure.set :as s]))
(defn read-input []
(slurp "resources/day3.in"))
(defn create-rucksacks [in-str]
(let [lines (str/split-lines in-str)
rucksacks (map seq lines)]
rucksacks))
(defn split-compartments [rucksack]
(partition (/ (count rucksack) 2) rucksack))
(defn find-shared-item [x]
(apply s/intersection (map #(apply hash-set %) x)))
(defn calculate-priority [item]
(let [num-val (int item)]
(if (> num-val 90)
(- num-val 96)
(- num-val 64 -26)
)))
(defn sum-priorities-for-compartments [rucksacks]
(->> rucksacks
(map split-compartments)
(map find-shared-item)
(map seq)
(flatten)
(map calculate-priority)
(apply +)
))
(defn sum-priorities-for-groups [rucksacks]
(->> rucksacks
(partition 3)
(map find-shared-item)
(map seq)
(flatten)
(map calculate-priority)
(apply +)))
(comment
(read-input)
(first (create-rucksacks))
(split-compartments (first (create-rucksacks)))
(find-shared-item (split-compartments (first (create-rucksacks))))
(calculate-priority \Z)
(sum-priorities-for-compartments (create-rucksacks "vJrwpWtwJgWrhcsFMMfFFhFp\njqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL\nPmmdzqPrVvPwwTWBwg\nwMqvLMZHhHMvwLHjbvcjnnSBnvTQFn\nttgJtRGJQctTZtZT\nCrZsJsPPZsGzwwsLwLmpwMDw"))
(sum-priorities-for-compartments (create-rucksacks "aa\naa"))
(sum-priorities-for-compartments (create-rucksacks (read-input)))
(sum-priorities-for-groups (create-rucksacks (read-input))))