day 4
This commit is contained in:
parent
e07b4d92b8
commit
8993238612
67
src/day4.clj
Normal file
67
src/day4.clj
Normal file
@ -0,0 +1,67 @@
|
||||
(ns day4
|
||||
(:require [clojure.string :as string]
|
||||
[helpers :refer [get-input]]))
|
||||
|
||||
(def example
|
||||
"..@@.@@@@.
|
||||
@@@.@.@.@@
|
||||
@@@@@.@.@@
|
||||
@.@@@@..@.
|
||||
@@.@@@@.@@
|
||||
.@@@@@@@.@
|
||||
.@.@.@.@@@
|
||||
@.@@@.@@@@
|
||||
.@@@@@@@@.
|
||||
@.@.@@@.@.")
|
||||
|
||||
(def input (mapv (partial apply vector) (string/split-lines example)))
|
||||
(def input (mapv (partial apply vector) (string/split-lines (get-input 4))))
|
||||
(def line-len (count input))
|
||||
(def column-len (count (first input)))
|
||||
|
||||
(defn adjacent [[line col]]
|
||||
(let [first-col (dec col)
|
||||
last-col (inc col)
|
||||
line-up (map #(vector (dec line) %) (range first-col (inc last-col)))
|
||||
line-down (map #(vector (inc line) %) (range first-col (inc last-col)))]
|
||||
(->> (concat line-up [[line first-col] [line last-col]] line-down)
|
||||
(remove (fn [[line col]] (or (neg? line) (neg? col))))
|
||||
(filter (fn [[line col]] (and (< line line-len) (< col column-len)))))))
|
||||
|
||||
(def coords
|
||||
(->>
|
||||
(range line-len)
|
||||
(mapcat
|
||||
(fn [l]
|
||||
(map (fn [c] [l c])
|
||||
(range column-len))))))
|
||||
|
||||
(defn roll? [m c]
|
||||
(= \@ (get-in m c)))
|
||||
|
||||
(defn adjacent-rolls [m c]
|
||||
(filter (partial roll? m)
|
||||
(adjacent c)))
|
||||
|
||||
(defn accessible? [m c]
|
||||
(and
|
||||
(roll? m c)
|
||||
(> 4 (count (adjacent-rolls m c)))))
|
||||
|
||||
(defn accessible-rolls [m]
|
||||
(filter (partial accessible? m) coords))
|
||||
|
||||
(defn remove-rolls [m rolls]
|
||||
(reduce (fn [m c] (assoc-in m c \x))
|
||||
m rolls))
|
||||
|
||||
(println "Result:" (count (accessible-rolls input)))
|
||||
|
||||
(println "Total:"
|
||||
(loop [m input
|
||||
a (accessible-rolls input)
|
||||
r 0]
|
||||
(if (empty? a)
|
||||
r
|
||||
(let [m' (remove-rolls m a)]
|
||||
(recur m' (accessible-rolls m') (+ r (count a)))))))
|
||||
Loading…
x
Reference in New Issue
Block a user