]> code.bitgloo.com Git - clyne/advent-of-code.git/commitdiff
day3: part 1 one-liner
authorClyne Sullivan <clyne@bitgloo.com>
Fri, 3 Dec 2021 18:23:52 +0000 (13:23 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Fri, 3 Dec 2021 18:23:52 +0000 (13:23 -0500)
day3/part1.clj

index c896928a9d843c3e90731e089ceec8955d11222d..915ee47e50fdb8bf6195ab6b47d1ab74e8126188 100644 (file)
@@ -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)
+