Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b62c9346f5 | |||
| 505453974b |
@@ -1,27 +0,0 @@
|
|||||||
(ns day11
|
|
||||||
(:require [core]
|
|
||||||
input-manager
|
|
||||||
[clojure.string :as str]))
|
|
||||||
|
|
||||||
(def input (-> (input-manager/get-input-raw 2017 11)
|
|
||||||
(str/split #",")))
|
|
||||||
|
|
||||||
(def offsets {"n" [0 1]
|
|
||||||
"ne" [0.5 0.5]
|
|
||||||
"se" [0.5 -0.5]
|
|
||||||
"s" [0 -1]
|
|
||||||
"sw" [-0.5 -0.5]
|
|
||||||
"nw" [-0.5 0.5]})
|
|
||||||
|
|
||||||
(defn distance-from-origin [pos]
|
|
||||||
(let [[x y] (map abs pos)]
|
|
||||||
(+ (* x 2) (- y x))))
|
|
||||||
|
|
||||||
|
|
||||||
(->> input
|
|
||||||
(map offsets)
|
|
||||||
(reductions #(map + %1 %2) [0 0])
|
|
||||||
(map distance-from-origin)
|
|
||||||
((juxt last (partial apply max)))
|
|
||||||
(zipmap [:part-one :part-two]))
|
|
||||||
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
(ns day12
|
|
||||||
(:require
|
|
||||||
[clojure.string :as str]
|
|
||||||
core
|
|
||||||
input-manager
|
|
||||||
[clojure.set :refer [difference]]))
|
|
||||||
|
|
||||||
(def input (->> (input-manager/get-input 2017 12)
|
|
||||||
(map #(str/replace % #"," ""))
|
|
||||||
(map core/split-whitespace)
|
|
||||||
(map (fn [[a _ & rst]]
|
|
||||||
[(parse-long a) (mapv parse-long rst)]))
|
|
||||||
(into {})))
|
|
||||||
|
|
||||||
(defn group-from-start [start]
|
|
||||||
(loop [visited #{}
|
|
||||||
queue [start]]
|
|
||||||
(if-not (seq queue)
|
|
||||||
visited
|
|
||||||
(let [[curr & rst] queue
|
|
||||||
ns (filter (complement visited) (input curr))]
|
|
||||||
(recur (into visited ns)
|
|
||||||
(concat rst ns))))))
|
|
||||||
|
|
||||||
;; part one
|
|
||||||
(count (group-from-start 0))
|
|
||||||
|
|
||||||
;; part two
|
|
||||||
(loop [ks (into #{} (keys input))
|
|
||||||
i 0]
|
|
||||||
(if-not (seq ks)
|
|
||||||
i
|
|
||||||
(recur (difference ks (group-from-start (first ks)))
|
|
||||||
(inc i))))
|
|
||||||
@@ -1,58 +0,0 @@
|
|||||||
(ns day13
|
|
||||||
(:require
|
|
||||||
[clojure.string :as str]
|
|
||||||
[core]
|
|
||||||
[input-manager]))
|
|
||||||
|
|
||||||
(def lengths (->> (input-manager/get-input 2017 13)
|
|
||||||
(map (comp #(mapv parse-long %)
|
|
||||||
#(str/split % #": ")))
|
|
||||||
(into {})))
|
|
||||||
|
|
||||||
(defn move-sensor [{:keys [idx depth increasing-depth] :as sensor}]
|
|
||||||
(let [[keep-moving reverse-step] (if increasing-depth [inc dec] [dec inc])
|
|
||||||
depth' (keep-moving depth)]
|
|
||||||
(if (or (> depth' (dec (lengths idx)))
|
|
||||||
(< depth' 0))
|
|
||||||
(-> sensor
|
|
||||||
(update :depth reverse-step)
|
|
||||||
(update :increasing-depth not))
|
|
||||||
(assoc sensor :depth depth'))))
|
|
||||||
|
|
||||||
(def initial-sensors (->> lengths
|
|
||||||
keys
|
|
||||||
(map #(hash-map :depth 0
|
|
||||||
:idx %
|
|
||||||
:increasing-depth true))))
|
|
||||||
|
|
||||||
(defn is-spotted-by-sensors [acc pos]
|
|
||||||
(some #(when (and (= (:idx %) pos)
|
|
||||||
(= (:depth %) 0))
|
|
||||||
%)
|
|
||||||
(:sensors acc)))
|
|
||||||
|
|
||||||
(->> lengths keys (apply max) inc range
|
|
||||||
(reduce (fn [acc pos]
|
|
||||||
(let [spotted (is-spotted-by-sensors acc pos)]
|
|
||||||
(-> acc
|
|
||||||
(update :sensors (partial map move-sensor))
|
|
||||||
(update :solution + (if spotted
|
|
||||||
(* pos (lengths pos))
|
|
||||||
0)))))
|
|
||||||
{:sensors initial-sensors
|
|
||||||
:solution 0})
|
|
||||||
:solution)
|
|
||||||
|
|
||||||
(loop [i 0
|
|
||||||
start-val initial-sensors]
|
|
||||||
(if
|
|
||||||
(= true
|
|
||||||
(->> lengths keys (apply max) inc range
|
|
||||||
(reduce (fn [acc pos]
|
|
||||||
(let [spotted (is-spotted-by-sensors acc pos)]
|
|
||||||
(if spotted
|
|
||||||
(reduced true)
|
|
||||||
(update acc :sensors (partial map move-sensor)))))
|
|
||||||
{:sensors start-val})))
|
|
||||||
(recur (inc i) (map move-sensor start-val))
|
|
||||||
i))
|
|
||||||
+19
-25
@@ -1,37 +1,31 @@
|
|||||||
(ns day03
|
(ns day03
|
||||||
(:require [core :as c]
|
(:require [input-manager]
|
||||||
[input-manager :refer [get-input]]
|
|
||||||
[clojure.string :as str]))
|
[clojure.string :as str]))
|
||||||
|
|
||||||
(def input (str/join (get-input 3)))
|
(def input (input-manager/get-input-raw 2024 3))
|
||||||
|
|
||||||
(defn parse-muls
|
(defn compute-muls [s]
|
||||||
"takes in a string containing mul instructions
|
(map (comp #(reduce * %)
|
||||||
returns list of parsed integral multiplication results"
|
#(map parse-long %)
|
||||||
[s]
|
#(str/split % #",")
|
||||||
(->> (c/get-match-groups #"mul\((\d{1,3}),(\d{1,3})\)" s)
|
#(subs % 4 (dec (count %)))) ;; remove `mul(` prefix and `)` suffix
|
||||||
(map #(map parse-long %))
|
s))
|
||||||
(map #(reduce * %))))
|
|
||||||
|
(defn enforce-toggles [[el & rst]]
|
||||||
|
(cond (= el "do()") (recur rst)
|
||||||
|
(= el "don't()") (recur (drop-while #(not= % "do()") rst))
|
||||||
|
el (conj (lazy-seq (enforce-toggles rst)) el)))
|
||||||
|
|
||||||
;; part 1
|
;; part 1
|
||||||
(->> input
|
(->> input
|
||||||
parse-muls
|
(re-seq #"mul\(\d{1,3},\d{1,3}\)")
|
||||||
|
compute-muls
|
||||||
(reduce +))
|
(reduce +))
|
||||||
|
|
||||||
;; part 2
|
;; part 2
|
||||||
(->> input
|
(->> input
|
||||||
(c/re-pos #"mul\((\d{1,3}),(\d{1,3})\)|(do\(\))|(don't\(\))")
|
(re-seq #"mul\(\d{1,3},\d{1,3}\)|do\(\)|don't\(\)")
|
||||||
(sort-by key)
|
enforce-toggles
|
||||||
(reduce
|
compute-muls
|
||||||
(fn [acc [_idx instr]]
|
(reduce +))
|
||||||
(cond
|
|
||||||
(.startsWith instr "mul") (if (:on acc)
|
|
||||||
(update acc :val
|
|
||||||
+ (->> instr parse-muls first))
|
|
||||||
acc)
|
|
||||||
(.startsWith instr "don't") (assoc acc :on false)
|
|
||||||
(.startsWith instr "do") (assoc acc :on true)))
|
|
||||||
{:on true
|
|
||||||
:val 0})
|
|
||||||
:val)
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user