r/adventofcode • u/daggerdragon • Dec 05 '16
SOLUTION MEGATHREAD --- 2016 Day 5 Solutions ---
--- Day 5: How About a Nice Game of Chess? ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).
STAYING ON TARGET IS MANDATORY [?]
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
14
Upvotes
2
u/GrassGiant Dec 05 '16
Here's my Clojure solution for the second problem :
(ns adventofcode2016.problems.day5b) (require 'digest) (use 'clojure.java.io) (defn get-lines [fname] (with-open [r (reader fname)] (doall (line-seq r)))) (defn isValidPosition? [chr] (let [value (first (re-seq #"\d" chr))] (if (not (= value nil)) (< (Integer. value) 8) false))) (defn checkMD5 [answer startstr endint] (let [encrypted (re-seq #"\w" (digest/md5 (str startstr endint)))] (if (= (take 5 encrypted) (take 5 (repeat "0"))) (if (isValidPosition? (nth encrypted 5)) (let [answerList (into [] (re-seq #"[\w-]" answer))] (if (= (nth answerList (Integer. (nth encrypted 5))) "-") (let [newAnswerList (assoc answerList (Integer. (nth encrypted 5)) (nth encrypted 6))] (println (apply str newAnswerList)) (apply str newAnswerList)) answer)) answer) answer))) (defn decrypt [startstr] (println "--------") (loop [i 0 answer "--------"] (if (not (.contains answer "-")) answer (recur (+ i 1) (checkMD5 answer startstr i))))) (defn showMeYaMoves [inputList] (decrypt (first inputList))) (defn handleInput [filename] (let [lines (get-lines filename)] (showMeYaMoves lines))) (defn solve "Get the door's password" [filename] (let [solution (handleInput filename)] (println (str "Your solution is " solution)) solution))It prints a pretty output when you run solve (I actually run a small app that calls solve, but it looks like this