55 lines
1.1 KiB
Clojure
55 lines
1.1 KiB
Clojure
(ns day1
|
|
(:require [clojure.string :as string]
|
|
[helpers :refer [get-input]]))
|
|
|
|
(def test-input "L68
|
|
L30
|
|
R48
|
|
L5
|
|
R60
|
|
L55
|
|
L1
|
|
L99
|
|
R14
|
|
L82
|
|
")
|
|
|
|
(defn- parse-line [l]
|
|
[(first l) (Integer/parseInt (apply str (rest l)))])
|
|
|
|
(defn- parse-input [i]
|
|
(->> (string/split-lines i)
|
|
(map parse-line)))
|
|
|
|
(defn- rotate [values [direction step]]
|
|
(let [value (peek values)]
|
|
(conj
|
|
values
|
|
(if (= direction \R)
|
|
(rem (+ value step) 100)
|
|
(let [value (- value (rem step 100))]
|
|
(if (neg? value)
|
|
(- 100 (rem (- value) 100))
|
|
value))))))
|
|
|
|
;(def input (parse-input test-input))
|
|
(def input (parse-input (get-input 1)))
|
|
|
|
(def dial (reduce rotate [50] input))
|
|
|
|
(println "Password:" (count (filterv zero? dial)))
|
|
|
|
(def rotations (partition 2 (interleave dial input)))
|
|
|
|
(defn- count-zeros [[a [direction step]]]
|
|
(cond
|
|
(= direction \R)
|
|
(quot (+ a step) 100)
|
|
(= direction \L)
|
|
(if (>= step a)
|
|
(let [r (inc (abs (quot (- a step) 100)))]
|
|
(if (zero? a) (dec r) r))
|
|
0)))
|
|
|
|
(println "Password method 0x434C49434B:" (apply + (map count-zeros rotations)))
|