-
-
Save jurjanpaul/e1cde90f7e7bebe77ef88c44e037a6fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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