September 10, 2018

Advent of Code

I just found the Advent of Code website and this project is amazing. This post will keep you updated about my solutions. You can also find then on my Github page.

I will try to finish one new puzzle each week. I am doing a lot of different work and this puzzles are really nice to sparse some time when I got stuck at some readings. o/

   

Let's go!    

Day 1 - Challenge 1:

finished at 2018-09-10

The captcha requires you to review a sequence of digits (your puzzle input) and find the sum of all digits that match the next digit in the list. The list is circular, so the digit after the last digit is the first digit in the list.

For example:

  • 1122 produces a sum of 3 (1 + 2) because the first digit (1) matches the second digit and the third digit (2) matches the fourth digit.
  • 1111 produces 4 because each digit (all 1) matches the next.
  • 1234 produces 0 because no digit matches the next.
  • 91212129 produces 9 because the only digit that matches the next one is the last digit, 9.

Solution:

(defn challenge1 [n]
  (let [fvals (filter #(not (nil? %)) (map #(when (= %1 %2)
                                              (Character/digit %1 10)) (str n) (subs (str n) 1)))
        f (Character/digit (first (str n)) 10)
        l (Character/digit (last (str n)) 10)]
    (if (not (empty? fvals))
      (+ (reduce + fvals) (if (= f l) f 0))
      (if (= f l) f 0))))

Tags: challenges clojure