Skip to content

Instantly share code, notes, and snippets.

@velppa
Created December 10, 2020 23:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save velppa/60211e1b26989ba2690e9d55099268f2 to your computer and use it in GitHub Desktop.
Save velppa/60211e1b26989ba2690e9d55099268f2 to your computer and use it in GitHub Desktop.
Advent Of Code
(ns day9
(:require [clojure.string :as str]))
(def input (->> (slurp "input.txt")
(#(str/split % #"\n"))
(map read-string)))
(def PREAMBLE-LENGTH 25)
(defn pairs
([a b] (list [a b]))
([a b c & rest]
(let [r (into [b c] (vec rest))]
(concat (for [x r] [a x])
(apply pairs r)))))
(defn verify [n]
(let [preamble (take PREAMBLE-LENGTH (drop (- (dec n) PREAMBLE-LENGTH) input))
elem (first (drop (dec n) input))
allowed (into #{} (map #(apply + %) (apply pairs preamble)))]
(allowed elem)))
(filter some? (for [n (range (inc PREAMBLE-LENGTH) (inc (- (count input) PREAMBLE-LENGTH)))]
(when-not (verify n) n)))
;; => (634)
(first (drop 633 input))
;; => 507622668 - invalid number from step 1
(defn contiguous [coll]
(for [n (range (count coll))]
(take (inc n) coll)))
(defn full-cont [[a & rest :as full]]
(concat (contiguous full) (when rest (full-cont rest))))
(filter #(= 507622668 (reduce + %)) (full-cont input))
;; => ((19457622 25638292 33571966 23658917 24391116 36326467 23805765 22019650 23657289 25476462 29229871 25419362 37609803 27205477 27262577 57230883 45661149) (507622668))
(apply min '(19457622 25638292 33571966 23658917 24391116 36326467 23805765 22019650 23657289 25476462 29229871 25419362 37609803 27205477 27262577 57230883 45661149))
;; => 19457622
(apply max '(19457622 25638292 33571966 23658917 24391116 36326467 23805765 22019650 23657289 25476462 29229871 25419362 37609803 27205477 27262577 57230883 45661149))
;; => 57230883
(+ 19457622 57230883)
;; => 76688505 - encryption weakness from step 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment