Skip to content

Instantly share code, notes, and snippets.

@smnplk smnplk/advent_2017.clj
Last active Dec 4, 2017

Embed
What would you like to do?
Advent of Code 2017
# Day 1 part 1
# Inverse Captcha
(def input-str "77736991856689225253142335214746294932318813454849177823468674346512426482777696993348135287531487622845155339235443718798255411492778415157351753377959586612882455464736285648473397681163729345143319577258292849619491486748832944425643737899293811819448271546283914592546989275992844383947572926628695617661344293284789225493932487897149244685921644561896799491668147588536732985476538413354195246785378443492137893161362862587297219368699689318441563683292683855151652394244688119527728613756153348584975372656877565662527436152551476175644428333449297581939357656843784849965764796365272113837436618857363585783813291999774718355479485961244782148994281845717611589612672436243788252212252489833952785291284935439662751339273847424621193587955284885915987692812313251556836958571335334281322495251889724281863765636441971178795365413267178792118544937392522893132283573129821178591214594778712292228515169348771198167462495988252456944269678515277886142827218825358561772588377998394984947946121983115158951297156321289231481348126998584455974277123213413359859659339792627742476688827577318285573236187838749444212666293172899385531383551142896847178342163129883523694183388123567744916752899386265368245342587281521723872555392212596227684414269667696229995976182762587281829533181925696289733325513618571116199419759821597197636415243789757789129824537812428338192536462468554399548893532588928486825398895911533744671691387494516395641555683144968644717265849634943691721391779987198764147667349266877149238695714118982841721323853294642175381514347345237721288281254828745122878268792661867994785585131534136646954347165597315643658739688567246339618795777125767432162928257331951255792438831957359141651634491912746875748363394329848227391812251812842263277229514125426682179711184717737714178235995431465217547759282779499842892993556918977773236196185348965713241211365895519697294982523166196268941976859987925578945185217127344619169353395993198368185217391883839449331638641744279836858188235296951745922667612379649453277174224722894599153367373494255388826855322712652812127873536473277")
(def input-seq (map #(Character/getNumericValue %) input-str))
(defn chk-captcha [digits]
(let [cd (cons (last digits) digits)]
(->> cd
(partition 2 1)
(filter #(= (first %) (last %)))
(map first)
(apply +))))
# Part 2
(defn chk-captcha2 [digits]
(let [c (count digits)
n (/ c 2)
pairs
(keep-indexed (fn [i digit]
(let [n-i (+ i n)
next-i (if (>= n-i c)
(mod n-i c)
n-i)]
(if (= digit (nth digits next-i))
digit)))
digits)]
(apply + pairs)))
(chk-captcha2 input-seq)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.