Last active
December 4, 2022 02:04
-
-
Save Frank-Buss/e5ff800aa2416cf1650ead4d01239abd to your computer and use it in GitHub Desktop.
first Clojure code, day 3 of advent of code 2022
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns test | |
(:require [clojure.string :refer [split-lines]]) | |
(:require [clojure.set :refer [intersection]])) | |
(def rucksacks "vJrwpWtwJgWrhcsFMMfFFhFp | |
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL | |
PmmdzqPrVvPwwTWBwg | |
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn | |
ttgJtRGJQctTZtZT | |
CrZsJsPPZsGzwwsLwLmpwMDw") | |
(defn split-half [s] | |
(split-at (/ (count s) 2) s)) | |
(defn priority [c] | |
(let [a (int \a) | |
A (int \A) | |
z (int \z)] | |
(if (<= a c z) | |
(+ (- c a) 1) | |
(+ (- c A) 27)))) | |
(defn intersection-and-sum [a] | |
(->> (map (fn [a] (apply intersection (map set a))) a) | |
(map (fn [a] (vec a))) | |
(flatten) | |
(map int) | |
(map priority) | |
(reduce +))) | |
(println "part 1:" | |
(->> (split-lines rucksacks) | |
(map split-half) | |
(map (fn [a] (map vec a))) | |
(intersection-and-sum))) | |
(println "part 2:" | |
(->> (split-lines rucksacks) | |
(partition 3) | |
(intersection-and-sum))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment