From ebd7f129afc2f1137667344096548661273baaad Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Fri, 3 Dec 2021 13:23:52 -0500 Subject: [PATCH] day3: part 1 one-liner --- day3/part1.clj | 50 ++++++++++++++++++++++---------------------------- 1 file changed, 22 insertions(+), 28 deletions(-) diff --git a/day3/part1.clj b/day3/part1.clj index c896928..915ee47 100644 --- a/day3/part1.clj +++ b/day3/part1.clj @@ -1,31 +1,25 @@ -(def counts - (loop [line (read-line) - tot (repeat (count line) 0) - ] - (if (empty? line) - tot - (recur - (read-line) - (map + tot (map #(if (= % \1) 1 0) line)) - ) - ) - ) - ) +(require '[clojure.string :as str]) -(loop [cnts counts gamma 0 epsilon 0] - (if (empty? cnts) - (println (* gamma epsilon)) - (recur - (rest cnts) - (if (> (first cnts) 500) - (inc (* 2 gamma)) - (* 2 gamma) - ) - (if (< (first cnts) 500) - (inc (* 2 epsilon)) - (* 2 epsilon) - ) - ) - ) +(println + (->> "./in" + (slurp) + (str/split-lines) + (map (fn [l] (map #(if (= % \1) 1 0) l))) + (apply (partial map +)) + (map #(if (< % 500) \1 \0)) + (str/join) + (#(Integer/parseInt % 2)) + (#(* % (bit-xor % (dec (int (Math/pow 2 12)))))) + ) ) +; (->> input data file name +; read in entire contents +; split contents into array of lines +; for each line, transform characters '1'/'0' to numbers +; build sum array using the lines +; convert back to array of characters +; join characters into single string +; convert binary string to a number (gamma) +; multiply gamma by its bit-inverse (bit length hard-coded) +