mirror of
https://github.com/Ajetski/advent-of-code.git
synced 2025-09-30 09:23:17 -09:00
clean up regex. compute not hardcode
This commit is contained in:
parent
0ce843f177
commit
574d0888cc
@ -6,14 +6,14 @@
|
||||
(def input (get-puzzle-input 1))
|
||||
|
||||
(def numeric-value-map {"one" 1
|
||||
"two" 2
|
||||
"three" 3
|
||||
"four" 4
|
||||
"five" 5
|
||||
"six" 6
|
||||
"seven" 7
|
||||
"eight" 8
|
||||
"nine" 9})
|
||||
"two" 2
|
||||
"three" 3
|
||||
"four" 4
|
||||
"five" 5
|
||||
"six" 6
|
||||
"seven" 7
|
||||
"eight" 8
|
||||
"nine" 9})
|
||||
|
||||
(defn numeric-value [s]
|
||||
(or (numeric-value-map s) s))
|
||||
@ -28,10 +28,18 @@
|
||||
(reduce +))
|
||||
|
||||
;; 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
|
||||
(map (fn [line]
|
||||
(let [f (re-find #"one|two|three|four|five|six|seven|eight|nine|\d" line)
|
||||
l (string/reverse (re-find #"eno|owt|eerht|ruof|evif|xis|neves|thgie|enin|\d" (string/reverse line)))]
|
||||
(let [f (re-find forawrd-search-regex line)
|
||||
l (string/reverse (re-find backwards-search-regex (string/reverse line)))]
|
||||
(str (numeric-value f) (numeric-value l)))))
|
||||
(map #(Integer/parseInt %))
|
||||
(reduce +))
|
||||
|
Loading…
x
Reference in New Issue
Block a user