-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
orbit-stabilizer
committed
Nov 12, 2024
1 parent
0d1e48f
commit 44aaa9e
Showing
5 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
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
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") |
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
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) |
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
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") |
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
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") |
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
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") |