mirror of
https://github.com/Ajetski/advent-of-code.git
synced 2025-11-27 11:32:45 -10:00
Compare commits
2 Commits
463a291744
...
23b5ed2e90
| Author | SHA1 | Date | |
|---|---|---|---|
| 23b5ed2e90 | |||
| aae825a9df |
98
2024/src/day16.clj
Normal file
98
2024/src/day16.clj
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
(ns day16
|
||||||
|
(:require
|
||||||
|
[input-manager :refer [get-input]]
|
||||||
|
[core :as c]
|
||||||
|
[clojure.data.priority-map :refer [priority-map]]))
|
||||||
|
|
||||||
|
(def maze
|
||||||
|
#_(c/split-whitespace "###############
|
||||||
|
#.......#....E#
|
||||||
|
#.#.###.#.###.#
|
||||||
|
#.....#.#...#.#
|
||||||
|
#.###.#####.#.#
|
||||||
|
#.#.#.......#.#
|
||||||
|
#.#.#####.###.#
|
||||||
|
#...........#.#
|
||||||
|
###.#.#####.#.#
|
||||||
|
#...#.....#.#.#
|
||||||
|
#.#.#.###.#.#.#
|
||||||
|
#.....#...#.#.#
|
||||||
|
#.###.#.#.#.#.#
|
||||||
|
#S..#.....#...#
|
||||||
|
###############")
|
||||||
|
(get-input 2024 16))
|
||||||
|
(def grid (->> maze
|
||||||
|
(map-indexed (fn [row-idx row]
|
||||||
|
(map-indexed (fn [col-idx c]
|
||||||
|
[[row-idx col-idx] c])
|
||||||
|
row)))
|
||||||
|
(mapcat identity)
|
||||||
|
(into {})))
|
||||||
|
(defn find-char [c]
|
||||||
|
(->> grid
|
||||||
|
(filter (comp #{c} second))
|
||||||
|
ffirst))
|
||||||
|
(def start (find-char \S))
|
||||||
|
(def end (find-char \E))
|
||||||
|
|
||||||
|
(defn rotate-clockwise [dir]
|
||||||
|
(condp = dir
|
||||||
|
:right :down
|
||||||
|
:down :left
|
||||||
|
:left :up
|
||||||
|
:up :right))
|
||||||
|
(defn rotate-counterclockwise [dir]
|
||||||
|
(condp = dir
|
||||||
|
:down :right
|
||||||
|
:left :down
|
||||||
|
:up :left
|
||||||
|
:right :up))
|
||||||
|
(defn direction-offset [dir]
|
||||||
|
(condp = dir
|
||||||
|
:up [-1 0]
|
||||||
|
:right [0 1]
|
||||||
|
:down [1 0]
|
||||||
|
:left [0 -1]))
|
||||||
|
|
||||||
|
(defn find-search-path [[start start-dir] goal]
|
||||||
|
(loop [[[[pos dir] cost] & _ :as whole] (priority-map [start start-dir] 0)
|
||||||
|
visited #{[start start-dir]}
|
||||||
|
shortest {start 0}
|
||||||
|
n 0]
|
||||||
|
(if (= pos goal)
|
||||||
|
shortest
|
||||||
|
(let [searches (->> [[[(mapv + pos (direction-offset dir)) dir] (inc cost)]
|
||||||
|
[[pos (rotate-clockwise dir)] (+ 1000 cost)]
|
||||||
|
[[pos (rotate-counterclockwise dir)] (+ 1000 cost)]]
|
||||||
|
(filter (comp not visited first))
|
||||||
|
(filter (comp grid ffirst))
|
||||||
|
(filter (comp not #{\#} grid ffirst)))
|
||||||
|
searches-no-costs (mapv (comp first pop) searches)
|
||||||
|
seaches-no-dirs (mapv (juxt ffirst last) searches)]
|
||||||
|
(recur (into (pop whole) searches)
|
||||||
|
(into visited searches-no-costs)
|
||||||
|
(into (into {} seaches-no-dirs) shortest)
|
||||||
|
(inc n))))))
|
||||||
|
|
||||||
|
(def searched (find-search-path [start :right] end))
|
||||||
|
|
||||||
|
;; part 1 solution
|
||||||
|
(searched end)
|
||||||
|
|
||||||
|
(def max-idx (->> (keys grid)
|
||||||
|
(map first)
|
||||||
|
(apply max)))
|
||||||
|
(def updated-grid (->> grid
|
||||||
|
(map (fn [[k :as entry]] (if (and (contains? searched k) (#{\S \E \.} (grid k)))
|
||||||
|
[k \O]
|
||||||
|
entry)))
|
||||||
|
(into {})))
|
||||||
|
(->> (range 0 max-idx)
|
||||||
|
(map (fn [row] (map #(vector row %) (range 0 max-idx))))
|
||||||
|
(map (partial map updated-grid))
|
||||||
|
(map (partial apply str))
|
||||||
|
(interpose "\n")
|
||||||
|
(apply str)
|
||||||
|
print)
|
||||||
|
|
||||||
|
|
||||||
52
2024/src/day22.clj
Normal file
52
2024/src/day22.clj
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
(ns day22
|
||||||
|
(:require input-manager))
|
||||||
|
|
||||||
|
(def input (->> (input-manager/get-input 2024 22)
|
||||||
|
(map parse-long)))
|
||||||
|
|
||||||
|
(def prune-const 16777216)
|
||||||
|
(defn prune [x] (mod x prune-const))
|
||||||
|
(defn mix [secret v] (bit-xor secret v))
|
||||||
|
|
||||||
|
(defn next-step [secret]
|
||||||
|
(let [a (prune (mix secret (* secret 64)))
|
||||||
|
b (prune (mix a (int (/ a 32))))
|
||||||
|
c (prune (mix b (* b 2048)))]
|
||||||
|
c))
|
||||||
|
|
||||||
|
(defn get-nums
|
||||||
|
([n] (get-nums n 0))
|
||||||
|
([n i] (lazy-seq (cons n (get-nums (next-step n) (inc i))))))
|
||||||
|
|
||||||
|
(def solutions (->> input
|
||||||
|
(map get-nums)
|
||||||
|
(mapv (comp (partial into [])
|
||||||
|
(partial take 2001)))))
|
||||||
|
|
||||||
|
;; part 1 sol
|
||||||
|
(->> solutions
|
||||||
|
(map last)
|
||||||
|
(apply +))
|
||||||
|
|
||||||
|
;; part 2 sol
|
||||||
|
(->> (map (partial map #(mod % 10)) solutions)
|
||||||
|
(map #(map (fn [& prices]
|
||||||
|
(let [price-diff-list (map - (rest prices) prices)
|
||||||
|
sell-price (last prices)]
|
||||||
|
[price-diff-list sell-price]))
|
||||||
|
% (rest %) (drop 2 %) (drop 3 %) (drop 4 %)))
|
||||||
|
(reduce (fn [acc buyer]
|
||||||
|
(->> (reduce (fn [[visited acc2] [price-diff-list price]]
|
||||||
|
(if (visited price-diff-list)
|
||||||
|
[visited acc2]
|
||||||
|
[(conj visited
|
||||||
|
price-diff-list)
|
||||||
|
(update acc2
|
||||||
|
price-diff-list
|
||||||
|
#(+ (or % 0) price))]))
|
||||||
|
[#{} acc]
|
||||||
|
buyer)
|
||||||
|
second))
|
||||||
|
{})
|
||||||
|
vals
|
||||||
|
(apply max))
|
||||||
Loading…
x
Reference in New Issue
Block a user