mirror of
https://github.com/Ajetski/advent-of-code.git
synced 2025-09-30 05:23:17 -09:00
golf
This commit is contained in:
parent
22ac0db085
commit
b25e12b3a2
@ -17,3 +17,18 @@
|
||||
(if (.find m)
|
||||
(recur m (assoc res (.start m) (.group m)))
|
||||
res)))
|
||||
|
||||
(defn dbg [x]
|
||||
(println x)
|
||||
x)
|
||||
|
||||
(defn get-coords
|
||||
"returns a lazy seq representing list of list x, y tuples"
|
||||
[list-of-lists]
|
||||
(->> list-of-lists count range
|
||||
(map #(->> % (get list-of-lists) count range))
|
||||
(map-indexed (fn [row cols]
|
||||
(map #(list row %) cols)))))
|
||||
|
||||
(defn bool->binary [condition]
|
||||
(if condition 1 0))
|
||||
|
@ -1,5 +1,7 @@
|
||||
(ns day04
|
||||
(:require [input-manager :refer [get-input]]))
|
||||
(:require [input-manager :refer [get-input]]
|
||||
[core :as c]
|
||||
[clojure.string :as str]))
|
||||
|
||||
(def input (get-input 4))
|
||||
|
||||
@ -8,51 +10,38 @@
|
||||
|
||||
;; part 1
|
||||
(->> input
|
||||
(map (partial map-indexed vector))
|
||||
(map-indexed vector)
|
||||
(reduce (fn [acc [row s]]
|
||||
(reduce (fn [acc2 [col _c]]
|
||||
(+ acc2 (->> (for [offset (range 4)]
|
||||
(map #(apply get-char %)
|
||||
[[row (+ col offset)]
|
||||
[row (- col offset)]
|
||||
[(+ row offset) col]
|
||||
[(- row offset) col]
|
||||
[(- row offset) (- col offset)]
|
||||
[(- row offset) (+ col offset)]
|
||||
[(+ row offset) (- col offset)]
|
||||
[(+ row offset) (+ col offset)]]))
|
||||
(apply map vector)
|
||||
(filter #(= % (seq "XMAS")))
|
||||
count)))
|
||||
acc
|
||||
s))
|
||||
0))
|
||||
c/get-coords
|
||||
(map (partial reduce (fn [acc [row col]]
|
||||
(+ acc
|
||||
(->> (for [offset (range 4)]
|
||||
(map #(apply get-char %)
|
||||
[[row (+ col offset)]
|
||||
[row (- col offset)]
|
||||
[(+ row offset) col]
|
||||
[(- row offset) col]
|
||||
[(- row offset) (- col offset)]
|
||||
[(- row offset) (+ col offset)]
|
||||
[(+ row offset) (- col offset)]
|
||||
[(+ row offset) (+ col offset)]]))
|
||||
(apply map vector)
|
||||
(filter #(= % (seq "XMAS")))
|
||||
count)))
|
||||
0))
|
||||
(reduce +))
|
||||
|
||||
;; part 2
|
||||
(->> input
|
||||
(map reverse)
|
||||
(map (partial map-indexed vector))
|
||||
(map-indexed vector)
|
||||
(reduce (fn [acc [row s]]
|
||||
(reduce (fn [acc2 [col _c]]
|
||||
(+ acc2 (if (= (get-char row col) \A)
|
||||
(let [corners [[(dec row) (dec col)]
|
||||
[(inc row) (dec col)]
|
||||
[(dec row) (inc col)]
|
||||
[(inc row) (inc col)]]
|
||||
chars (mapv #(apply get-char %) corners)]
|
||||
(if (and (= 2
|
||||
(count (filter #(= % \S) chars))
|
||||
(count (filter #(= % \M) chars)))
|
||||
(not= (get chars 0)
|
||||
(get chars 3)))
|
||||
1
|
||||
0))
|
||||
0)))
|
||||
acc
|
||||
s))
|
||||
0)
|
||||
;
|
||||
)
|
||||
c/get-coords
|
||||
(map (partial reduce (fn [acc [row col]]
|
||||
(+ acc
|
||||
(if (= (get-char row col) \A)
|
||||
(->> [[[(dec row) (dec col)] [(inc row) (inc col)]]
|
||||
[[(inc row) (dec col)] [(dec row) (inc col)]]]
|
||||
(map (partial map #(apply get-char %)))
|
||||
(map set)
|
||||
(apply = #{\M \S})
|
||||
c/bool->binary)
|
||||
0)))
|
||||
0))
|
||||
(reduce +))
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user