Last active
December 8, 2019 19:04
-
-
Save jprudent/2f2a415f74b797a5987f4a83d1e14c05 to your computer and use it in GitHub Desktop.
Advent of code 2019
This file contains hidden or 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
;; _ _ _ __ ____ _ | |
;; / \ __| |_ _____ _ __ | |_ ___ / _| / ___|___ __| | ___ | |
;; / _ \ / _` \ \ / / _ \ '_ \| __| / _ \| |_ | | / _ \ / _` |/ _ \ | |
;; / ___ \ (_| |\ V / __/ | | | |_ | (_) | _| | |__| (_) | (_| | __/ | |
;;/_/ \_ \__, _| \_/ \___|_| |_| \__| \___/|_| \____ \___/ \__, _| \___| | |
;; ____ ___ _ ___ | |
;; |___ \ / _ \/ |/ _ \ | |
;; __) | | | | | (_) | | |
;; / __/| |_| | |\__, | | |
;; |_____|\___/|_| /_/ | |
;; day 4 - step 2 | |
(count (filter (fn [x] | |
(and (<= 178416 x 676461) | |
(reduce #(if (<= (int %1) (int %2)) %2 (reduced nil)) (str x)) | |
(some #{2} (map count (partition-by identity (str x)))))) | |
(range 100000 1000000))) | |
;; day 4 - step 1 | |
;;However, they do remember a few key facts about the password: | |
;; | |
;;It is a six-digit number. | |
;;The value is within the range given in your puzzle input. | |
;;Two adjacent digits are the same (like 22 in 122345). | |
;;Going from left to right, the digits never decrease; they only ever increase or stay the same (like 111123 or 135679). | |
(count (filter (fn [x] | |
(and (<= 178416 x 676461) | |
(re-matches #".*(11|22|33|44|55|66|77|88|99|00).*" (str x)) | |
(reduce #(if (<= (int %1) (int %2)) %2 (reduced nil)) (str x)))) | |
(range 100000 1000000))) | |
;; day 3 - step 2 | |
;; (awful n² algorithm) | |
(let [read-problem (fn [] | |
(->> (slurp "/home/jerome/.IntelliJIdea2019.2/config/scratches/advent_2019_3_2") | |
#_"R8,U5,L5,D3\nU7,R6,D4,L4" | |
#_"R75,D30,R83,U83,L12,D49,R71,U7,L72\nU62,R66,U55,R34,D71,R55,D58,R83" | |
(str/split-lines) | |
(map #(str "[" % "]")) | |
(map read-string))) | |
read-command (fn [dir] | |
(let [[_ cmd dist] (re-matches #"(R|L|U|D)(\d+)" (name dir)) | |
dist (read-string dist)] | |
[cmd dist])) | |
cmd->xys (fn [[x y] [cmd dist]] | |
(case cmd | |
"U" (reverse (for [y' (range y (+ y dist))] [x (inc y')])) | |
"D" (for [y' (range (- y dist) y)] [x y']) | |
"L" (for [x' (range (- x dist) x)] [x' y]) | |
"R" (reverse (for [x' (range x (+ x dist))] [(inc x') y])))) | |
dir->xys (fn dir->xys | |
[[[x y] & _ :as coors] dir] | |
(let [[cmd dist] (read-command dir)] | |
(concat (cmd->xys [x y] (read-command dir)) | |
coors))) | |
path->coors (fn path->coors | |
[path] | |
(reduce dir->xys '([0 0]) path)) | |
[coors-wire-1 coors-wire-2] (map (comp butlast path->coors) (read-problem)) | |
coors-wire-1 (map-indexed #(vector (inc %1) %2) (reverse coors-wire-1)) | |
coors-wire-2 (map-indexed #(vector (inc %1) %2) (reverse coors-wire-2)) | |
_ (println (count coors-wire-2) (count coors-wire-1)) | |
intersections (for [[i coor-1] coors-wire-1 | |
[j coor-2] coors-wire-2 | |
:when (= coor-1 coor-2) | |
:let [_ (println i "," j)]] | |
(+ i j)) | |
r (apply min intersections)] | |
r) | |
;; day 3 - step 1 | |
(let [cartesian (->> (slurp "/home/jerome/.IntelliJIdea2019.2/config/scratches/advent_2019_3_1") | |
#_"R8,U5,L5,D3\nU7,R6,D4,L4" | |
#_"R75,D30,R83,U83,L12,D49,R71,U7,L72\nU62,R66,U55,R34,D71,R55,D58,R83" | |
(str/split-lines) | |
(map #(str "[" % "]")) | |
(map read-string) | |
(map (fn path->coors | |
[path] | |
path | |
(reduce (fn dir->xys | |
[[[x y] & _ :as coors] dir] | |
(let [[_ cmd dist] (re-matches #"(R|L|U|D)(\d+)" (name dir)) | |
dist (read-string dist)] | |
(concat (case cmd | |
"U" (reverse (for [y' (range y (+ y dist))] [x (inc y')])) | |
"D" (for [y' (range (- y dist) y)] [x y']) | |
"L" (for [x' (range (- x dist) x)] [x' y]) | |
"R" (reverse (for [x' (range x (+ x dist))] [(inc x') y]))) | |
coors))) | |
'([0 0]) | |
path))) | |
(map butlast) | |
(map set) | |
(apply clojure.set/intersection) | |
(map #(apply + (map (fn abx [x] (max x (- x))) %))) | |
(apply min))] | |
cartesian | |
) | |
;; day 2 - step 2 | |
(for [noun (range 100) | |
verb (range 100) | |
:let [[answer] (loop [memory (-> (->> (str "[" (slurp "/home/jerome/.IntelliJIdea2019.2/config/scratches/advent_2019_2_2") "]") | |
(read-string)) | |
(assoc 1 noun) | |
(assoc 2 verb)) | |
pc 0] | |
(case (memory pc) | |
1 (recur (assoc memory (memory (+ 3 pc)) | |
(+ (memory (memory (+ 1 pc))) | |
(memory (memory (+ 2 pc))))) | |
(+ 4 pc)) | |
2 (recur (assoc memory (memory (+ 3 pc)) | |
(* (memory (memory (+ 1 pc))) | |
(memory (memory (+ 2 pc))))) | |
(+ 4 pc)) | |
99 memory))] | |
:when (= 19690720 answer)] | |
{:noun noun :verb verb :solution (+ (* 100 noun) verb)}) | |
;; day 2 - step 1 | |
(loop [memory (-> (->> (str "[" (slurp "/home/jerome/.IntelliJIdea2019.2/config/scratches/advent_2019_2") "]") | |
(read-string)) | |
(assoc 1 12) | |
(assoc 2 2))é | |
pc 0] | |
(case (memory pc) | |
1 (recur (assoc memory (memory (+ 3 pc)) | |
(+ (memory (memory (+ 1 pc))) | |
(memory (memory (+ 2 pc))))) | |
(+ 4 pc)) | |
2 (recur (assoc memory (memory (+ 3 pc)) | |
(* (memory (memory (+ 1 pc))) | |
(memory (memory (+ 2 pc))))) | |
(+ 4 pc)) | |
99 memory)) | |
;; day 1 - step 2 | |
(->> (slurp "/home/jerome/.IntelliJIdea2019.2/config/scratches/advent_2019_1.txt") | |
(str/split-lines) | |
(map read-string) | |
(map (comp (partial reduce +) | |
(partial take-while pos?) | |
rest | |
(partial iterate #(-> (quot % 3) (- 2))))) | |
(reduce +)) | |
;; day 1 - step 1 | |
(->> (slurp "/home/jerome/.IntelliJIdea2019.2/config/scratches/advent_2019_1.txt") | |
(str/split-lines) | |
(map read-string) | |
(map #(-> (quot % 3) (- 2))) | |
(reduce +)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment