Skip to content

Instantly share code, notes, and snippets.

@Solaxun
Created December 4, 2021 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Solaxun/4de4f6fd2adaeff4763f7976e60d5c75 to your computer and use it in GitHub Desktop.
Save Solaxun/4de4f6fd2adaeff4763f7976e60d5c75 to your computer and use it in GitHub Desktop.
AoC 2021: Clojure Day 3
(ns aoc-clj-2021.day3
(:require [clojure.string :as str]
[clojure.set :as set]
[clojure.java.io :as io]))
(def data
(->> (io/resource "day3.txt")
slurp
str/split-lines))
(defn calc-rate [rate-type]
(let [lkp {:gamma second :epsilon first}]
(->> (apply map vector data)
(map frequencies)
(map (partial sort-by second)) ; sort maps by value (freq)
(map (lkp rate-type)) ; smallest freq is first, largest second
(map first) ; get num from [num freq] pairs
(str/join)
(#(Integer/parseInt % 2)))))
(* (calc-rate :gamma)
(calc-rate :epsilon))
;; part 2
(defn get-common [f xs]
(let [nums (group-by identity xs)
most-or-least (f (sort-by (comp count val) nums))
tie-breaker (if (= f first) \0 \1)]
(if (apply = (map count (map val nums)))
tie-breaker
(first most-or-least))))
(def get-most-common (partial get-common last))
(def get-least-common (partial get-common first))
(defn rating [f]
(loop [i 0 d data]
(if (> (count d) 1)
(let [common (map f (apply map vector d))]
(recur (inc i)
(filter #(= (nth % i) (nth common i))
d)))
(Integer/parseInt (first d) 2))))
(def o2-gen (rating get-most-common))
(def co2-scrub (rating get-least-common))
(* o2-gen co2-scrub)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment