Skip to content

Commit

Permalink
Add clojure code
Browse files Browse the repository at this point in the history
  • Loading branch information
orbit-stabilizer committed Nov 12, 2024
1 parent 0d1e48f commit 44aaa9e
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 0 deletions.
22 changes: 22 additions & 0 deletions 2023/2/part_two.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(require '[clojure.string :as str])

(defn get-max-colour [line colour]
(let [pattern (re-pattern (str "(\\d+) " colour))]
(as-> (re-seq pattern line) x
(apply max (map #(Integer/parseInt (nth %1 1)) x)))))

(defn make-max-game [line]
(let [id (Integer/parseInt (re-find #"\d+" line))
red (get-max-colour line "red")
green (get-max-colour line "green")
blue (get-max-colour line "blue")]
{:id id :red red :green green :blue blue}))

(defn main [file-name]
(as-> (slurp file-name) x
(str/split x #"\n")
(map make-max-game x)
(map #(* (:red %1) (:green %1) (:blue %1)) x)
(apply + x)))

(main "z_input.txt")
12 changes: 12 additions & 0 deletions 2023/3/part_one.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(require '[clojure.string :as str])

(defn main [file-name]
(->> (slurp file-name)
(str/split-lines)
(map (partial re-seq #"\d+"))))

(main "sample.txt")

(def a (to-array-2d [[1 2 3][4 5 6]]))

(aget a 1 0)
24 changes: 24 additions & 0 deletions 2023/4/part_one.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
(require '[clojure.string :as str])
(require '[clojure.set :as c-set])
(require '[clojure.math :as math])

(defn helper [line]
(as-> line x
(str/split x #":")
(nth x 1)
(str/split x #"\|")
(map str/trim x)
(map #(str/split %1 #" ") x)
(map (fn [x] (filter #(not= "" %1) x)) x)
(map set x)
(apply c-set/intersection x)
(count x)
(if (> x 0) (math/pow 2 (- x 1)) 0)))

(defn main [file-name]
(->> (slurp file-name)
(str/split-lines)
(map helper)
(apply +)))

(main "z_input.txt")
45 changes: 45 additions & 0 deletions 2023/4/part_two.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
(require '[clojure.string :as str])
(require '[clojure.set :as c-set])

(defn update-cards-helper [cards c n k final]
(if (or (= k c) (= n final))
cards
(update-cards-helper
(assoc cards n (+ (get cards n) c))
c
(+ n 1)
(+ k 1)
final)))

(defn update-cards [cards counts n final]
(if (= n final)
cards
(let [new-cards (update-cards-helper cards (nth counts n) (+ n 1) 0 final)]
(update-cards new-cards counts (+ n 1) final))))

(defn build-cards [n]
(into {} (map #(-> [%1 1]) (range n))))

(defn helper [line]
(as-> line x
(str/split x #":")
(nth x 1)
(str/split x #"\|")
(map str/trim x)
(map #(str/split %1 #" ") x)
(map (fn [x] (filter #(not= "" %1) x)) x)
(map set x)
(apply c-set/intersection x)
(count x)))

(defn main [file-name]
(let [lines (->> (slurp file-name) (str/split-lines))
cards (build-cards (count lines))
counts (map helper lines)]
;(->>
(update-cards cards counts 0 (count lines))))

;(vals)
;(apply +))))

(main "sample.txt")
28 changes: 28 additions & 0 deletions 2023/6/part_one.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
(require '[clojure.string :as str])

(defn read-file [file-name]
(->> (slurp file-name)
(str/split-lines)))

(defn make-races [document]
(->> document
(map #(re-seq #"\d+" %))
(map #(map parse-long %))
(apply map vector)))

(defn compute-ways-to-win [[time distance]]
(->> (range time)
(map #(- (* time %) (* % %)))
(filter #(> % distance))
(count)))

(defn calculate-result [nums]
(apply * nums))

(defn main [file-name]
(->> (read-file file-name)
(make-races)
(map compute-ways-to-win)
(calculate-result)))

(main "z_input.txt")

0 comments on commit 44aaa9e

Please sign in to comment.