mirror of
https://github.com/Ajetski/advent-of-code.git
synced 2025-09-30 13:03:19 -09:00
format
This commit is contained in:
parent
d98ca00ba5
commit
bed4c55c93
@ -5,42 +5,40 @@
|
|||||||
|
|
||||||
(def input (get-puzzle-input 1))
|
(def input (get-puzzle-input 1))
|
||||||
|
|
||||||
(def numeric-value-map {"one" 1
|
;; part 1
|
||||||
"two" 2
|
(->> input
|
||||||
"three" 3
|
(map #(Integer/parseInt
|
||||||
"four" 4
|
(str (re-find #"\d" %)
|
||||||
"five" 5
|
(re-find #"\d" (string/reverse %)))))
|
||||||
"six" 6
|
(reduce +))
|
||||||
"seven" 7
|
|
||||||
"eight" 8
|
|
||||||
"nine" 9})
|
|
||||||
|
|
||||||
|
(def numeric-value-map {"one" 1,
|
||||||
|
"two" 2,
|
||||||
|
"three" 3,
|
||||||
|
"four" 4,
|
||||||
|
"five" 5,
|
||||||
|
"six" 6,
|
||||||
|
"seven" 7,
|
||||||
|
"eight" 8,
|
||||||
|
"nine" 9})
|
||||||
(defn numeric-value [s]
|
(defn numeric-value [s]
|
||||||
(or (numeric-value-map s) s))
|
(or (numeric-value-map s) s))
|
||||||
|
|
||||||
;; part 1
|
(defn generate-regex-pattern [mapping-fn]
|
||||||
(->> input
|
(->> (keys numeric-value-map)
|
||||||
(map (fn [line]
|
(map mapping-fn)
|
||||||
{:first (re-find #"\d" line)
|
(#(conj % "\\d"))
|
||||||
:last (re-find #"\d" (string/reverse line))}))
|
(string/join "|")
|
||||||
(map #(str (:first %) (:last %)))
|
re-pattern))
|
||||||
(map #(Integer/parseInt %))
|
(def forwards-search-pattern (generate-regex-pattern identity))
|
||||||
(reduce +))
|
(def backwards-search-pattern (generate-regex-pattern string/reverse))
|
||||||
|
|
||||||
;; part 2
|
;; part 2
|
||||||
(def forawrd-search-regex (->> (conj (keys numeric-value-map) "\\d")
|
|
||||||
(string/join "|")
|
|
||||||
re-pattern))
|
|
||||||
|
|
||||||
(def backwards-search-regex (->> (conj (->> numeric-value-map keys (map string/reverse)) "\\d")
|
|
||||||
(string/join "|")
|
|
||||||
re-pattern))
|
|
||||||
|
|
||||||
(->> input
|
(->> input
|
||||||
(map (fn [line]
|
(map #(Integer/parseInt
|
||||||
(let [f (re-find forawrd-search-regex line)
|
(str (numeric-value (re-find forwards-search-pattern %))
|
||||||
l (string/reverse (re-find backwards-search-regex (string/reverse line)))]
|
(numeric-value (string/reverse
|
||||||
(str (numeric-value f) (numeric-value l)))))
|
(re-find backwards-search-pattern
|
||||||
(map #(Integer/parseInt %))
|
(string/reverse %)))))))
|
||||||
(reduce +))
|
(reduce +))
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user