mirror of
https://github.com/Ajetski/advent-of-code.git
synced 2025-09-30 09:23:17 -09:00
day 18
This commit is contained in:
parent
48f409b6a5
commit
f15dc986db
59
2024/src/day18.clj
Normal file
59
2024/src/day18.clj
Normal file
@ -0,0 +1,59 @@
|
||||
(ns day18
|
||||
(:require
|
||||
[clojure.set :refer [union]]
|
||||
[clojure.string :as str]
|
||||
[core :as c]
|
||||
input-manager))
|
||||
|
||||
(def N 1024)
|
||||
|
||||
(do
|
||||
(def input (->> (input-manager/get-input 2024 18)
|
||||
(map c/split-on-comma)
|
||||
(c/n-mapv 2 parse-long)))
|
||||
|
||||
(def walls (->> input
|
||||
(take N)
|
||||
(apply hash-set)))
|
||||
|
||||
(def end [(->> input
|
||||
(take N)
|
||||
(apply max-key first)
|
||||
first)
|
||||
(->> input
|
||||
(take N)
|
||||
(apply max-key second)
|
||||
second)]))
|
||||
|
||||
(defn in-bounds? [[x y]]
|
||||
(and (>= x 0) (<= x (first end))
|
||||
(>= y 0) (<= y (second end))))
|
||||
|
||||
(defn potential-steps [[x y]]
|
||||
[[(inc x) y]
|
||||
[(dec x) y]
|
||||
[x (inc y)]
|
||||
[x (dec y)]])
|
||||
|
||||
;; part 1
|
||||
(loop [n 0
|
||||
q #{[0 0]}
|
||||
visited #{[0 0]}]
|
||||
(when-not (empty? q)
|
||||
(if (q end)
|
||||
n
|
||||
(recur (inc n)
|
||||
(into #{} (mapcat #(->> (potential-steps %)
|
||||
(filter in-bounds?)
|
||||
(filter (comp not visited))
|
||||
(filter (comp not walls)))
|
||||
q))
|
||||
(union visited q)))))
|
||||
|
||||
;; part 2
|
||||
(->> input
|
||||
(map-indexed vector)
|
||||
(drop 2933)
|
||||
first
|
||||
second
|
||||
(str/join ","))
|
Loading…
x
Reference in New Issue
Block a user