mirror of
https://github.com/Ajetski/advent-of-code.git
synced 2025-11-27 12:32:45 -10:00
Compare commits
No commits in common. "23b5ed2e90142b4418bbd0783711dadc73b3c705" and "463a2917444cb54fe2d6ef1a2467fe8911e03619" have entirely different histories.
23b5ed2e90
...
463a291744
@ -1,98 +0,0 @@
|
|||||||
(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)
|
|
||||||
|
|
||||||
|
|
||||||
@ -1,52 +0,0 @@
|
|||||||
(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