Skip to content

Instantly share code, notes, and snippets.

@middlesphere
Created December 10, 2017 20:56
Show Gist options
  • Save middlesphere/09c0f6f8d882a36de259b59134f90d29 to your computer and use it in GitHub Desktop.
Save middlesphere/09c0f6f8d882a36de259b59134f90d29 to your computer and use it in GitHub Desktop.
;;;;;;;;;;; day 01 ;;;;;;;;;;;;;;;
;; half 1 of day 01
;; init sequence
(def captcha "823936645345581272695677318513459491834641129844393742672553544439126314399846773234845535593355348931499496184839582118817689171948635864427852215325421433717458975771369522138766248225963242168658975326354785415252974294317138511141826226866364555761117178764543435899886711426319675443679829181257496966219435831621565519667989898725836639626681645821714861443141893427672384716732765884844772433374798185955741311116365899659833634237938878181367317218635539667357364295754744829595842962773524584225427969467467611641591834876769829719248136613147351298534885563144114336211961674392912181735773851634298227454157885241769156811787611897349965331474217223461176896643242975397227859696554492996937235423272549348349528559432214521551656971136859972232854126262349381254424597348874447736545722261957871275935756764184378994167427983811716675476257858556464755677478725146588747147857375293675711575747132471727933773512571368467386151966568598964631331428869762151853634362356935751298121849281442128796517663482391226174256395515166361514442624944181255952124524815268864131969151433888721213595267927325759562132732586252438456569556992685896517565257787464673718221817783929691626876446423134331749327322367571432532857235214364221471769481667118117729326429556357572421333798517168997863151927281418238491791975399357393494751913155219862399959646993428921878798119215675548847845477994836744929918954159722827194721564121532315459611433157384994543332773796862165243183378464731546787498174844781781139571984272235872866886275879944921329959736315296733981313643956576956851762149275521949177991988236529475373595217665112434727744235789852852765675189342753695377219374791548554786671473733124951946779531847479755363363288448281622183736545494372344785112312749694167483996738384351293899149136857728545977442763489799693492319549773328626918874718387697878235744154491677922317518952687439655962477734559232755624943644966227973617788182213621899579391324399386146423427262874437992579573858589183571854577861459758534348533553925167947139351819511798829977371215856637215221838924612644785498936263849489519896548811254628976642391428413984281758771868781714266261781359762798")
;;convert to vector of longs
(def v (mapv #(Long/parseLong %) (map str captcha)))
;; partition vector by pairs with step 1. and add last and first digits as pair to it.
(defn make-pairs
[v]
(conj (into [] (partition 2 1 v)) (list (last v) (first v))))
(defn find-matched
[result pair]
(if (= (first pair)
(second pair))
(conj result (first pair))
result))
(def day01-answer-1 (reduce + (reduce find-matched [] (make-pairs v) )))
;; half 2 of day 01
(defn get-half-len-element [v idx]
(let [half-len (/ (count v) 2)]
(first (drop half-len (drop idx (cycle v))))))
(def half-v (for [i (range (count v))
:let [x (nth v i)
y (get-half-len-element v i)]
:when (= x y)]
x))
(def day01-answer-2 (reduce + half-v))
;;;;;;;;;;; day 02 ;;;;;;;;;;;;;;;
(def init-matrix [[737 1866 1565 1452 1908 1874 232 1928 201 241 922 281 1651 1740 1012 1001]
[339 581 41 127 331 133 51 131 129 95 499 527 518 435 508 494]
[1014 575 1166 259 152 631 1152 1010 182 943 163 158 1037 1108 1092 887]
[56 491 409 1263 1535 41 1431 1207 1393 700 1133 53 131 466 202 62]
[632 403 118 352 253 672 711 135 116 665 724 780 159 133 90 100]
[1580 85 1786 1613 1479 100 94 1856 546 76 1687 1769 1284 1422 1909 1548]
[479 356 122 372 786 1853 979 116 530 123 1751 887 109 1997 160 1960]
[446 771 72 728 109 369 300 746 86 910 566 792 616 84 338 57]
[6599 2182 200 2097 4146 7155 7018 1815 1173 4695 201 7808 242 3627 222 7266]
[1729 600 651 165 1780 2160 626 1215 149 179 1937 1423 156 129 634 458]
[1378 121 146 437 1925 2692 130 557 2374 2538 2920 2791 156 317 139 541]
[1631 176 1947 259 2014 153 268 752 2255 347 227 2270 2278 544 2379 349]
[184 314 178 242 145 410 257 342 183 106 302 320 288 151 449 127]
[175 5396 1852 4565 4775 665 4227 171 4887 181 2098 4408 2211 3884 2482 158]
[1717 3629 244 258 281 3635 235 4148 3723 4272 3589 4557 4334 4145 3117 4510]
[55 258 363 116 319 49 212 44 303 349 327 330 316 297 313 67]])
(def diffs (for [row init-matrix
:let [min-v (apply min row)
max-v (apply max row)]]
(- max-v min-v)))
(def day02-answer-1 (reduce + diffs))
;;half 2 of day 02
(defn divisible-by?
"return if x divisible by y without remainder."
[x y]
(instance? java.lang.Long (/ x y)))
(def diffs2 (for [row init-matrix
:let [sorted-row (into [] (sort > row))
result (loop [dividend (first sorted-row)
divisors (into [] (rest sorted-row))]
(when dividend
(let [found-divisors (filter #(divisible-by? dividend %) divisors)]
(if (not (empty? found-divisors))
(/ dividend (first found-divisors))
(recur (first divisors) (into [] (rest divisors)))))))]
:when (not (nil? result))]
result))
(def day02-answer-2 (reduce + diffs2))
;;;;;;;;;;; day 03 ;;;;;;;;;;;;;;;
(defn square [x] (* x x))
(defn up-corner
"return start value if next square"
[input ]
(let [v (int (-> input Math/sqrt Math/ceil))]
(if (even? v)
(+ 1 v)
v)))
(defn low-corner
"return start value of current square"
[up-corner]
(- up-corner 2))
;; input value is always: low-corner <= input <=up-corner
(defn detect-side-and-delta
"detect direction and delta from start of current side of square"
[up-corner low-corner input]
(first (for [direction [:up :left :down :right]
side-delta (range 1 up-corner)
:let [cur-val (+ (square low-corner) side-delta (* (- up-corner 1) (case direction
:up 0
:left 1
:down 2
:right 3)))]
:when (= cur-val input)]
[direction side-delta])))
(defn manh-dist
[input]
(let [square-side (dec (up-corner input))
[_ delta] (detect-side-and-delta (up-corner input) (low-corner (up-corner input)) input)
half-size (/ square-side 2)]
(if (>= delta half-size)
(+ half-size (- delta half-size))
(+ half-size (- half-size delta)))))
(manh-dist 325489)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment