Skip to content

Instantly share code, notes, and snippets.

@jurjanpaul
Forked from vijaykiran/yo.clj
Last active November 14, 2019 20:32
Show Gist options
  • Save jurjanpaul/e1cde90f7e7bebe77ef88c44e037a6fc to your computer and use it in GitHub Desktop.
Save jurjanpaul/e1cde90f7e7bebe77ef88c44e037a6fc to your computer and use it in GitHub Desktop.
(ns day4
(:require [clojure.java.io :as io]
[clojure.string :as str]))
(def input1 (io/resource "input1"))
(def lines (line-seq (io/reader input1)))
(defn parse [crypted]
(let [parts (str/split crypted #"-")
[sector checksum] (str/split (last parts) #"[\[\]]")
name-parts (butlast parts)]
{:parts name-parts
:crypted-name (str/join "" name-parts)
:sector (Integer/parseInt sector)
:checksum checksum}))
(defn checksum [letters]
(let [freq (frequencies letters)
sorted (sort (fn [[la fa] [lb fb]]
(if (= fa fb)
(compare la lb)
(compare fb fa))) freq)
five-top (take 5 (map key sorted))]
(apply str five-top)))
(defn real-room? [{expected-checksum :checksum
crypted-name :crypted-name}]
(= expected-checksum (checksum crypted-name)))
(defn answer [lines]
(transduce
(comp (map parse)
(filter real-room?)
(map :sector))
+ 0 lines))
(defn shift [char n]
(let [alphabet (cycle "abcdefghijklmnopqrstuvwxyz")]
(nth alphabet (+ n (- (int char) (int \a))))))
(defn shift-word [word n]
(apply str (map #(shift % n) word)))
(defn shift-room-name [{:keys [parts sector]}]
(str/join " " (map #(shift-word % sector) parts)))
(defn find-north-pole [lines]
(into [] (comp (map parse)
(map #(assoc % :shifted (shift-room-name %)))
(filter #(str/index-of (:shifted %) "north"))
(map :sector))
lines))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment