Skip to content

Instantly share code, notes, and snippets.

Last active Aug 29, 2015
What would you like to do?
Yet another fasttrack solution
(ns pairs.core
(:require [clojure.string :as str]
[clojure.math.combinatorics :as combo]))
(def letters (map identity "pairs"))
(defn name->counts [name]
(fn [acc value]
(if-not (nil? value)
(update-in acc [value] inc)
(reduce (fn [acc v] (assoc acc v 0)) {} letters)
(for [c name] (when (contains? (set letters) c) c))))
(defn names->combined [name1 name2]
(let [result
(into {} (map
(fn [a b] [(first a) (+ (second a) (second b))])
(map result letters)))
(defn pair->match [name1 name2]
(let [start (apply names->combined (map name->counts [name1 name2]))]
(loop [c start]
(let [step (for [i (range (dec (count c)))]
(let [result (+ (nth c i) (nth c (inc i)))]
(if (>= result 10)
(+ (int (/ result 10)) (mod result 10))
(if (> (count step) 2)
(recur step)
(+ (* 10 (first step)) (second step)))))))
(defn names->combinations [names] (combo/combinations names 2))
;; get the names that match
(slurp "fast_track_generoitu_nimilista.txt")
(pmap (fn [[n1 n2]] [n1 n2 (pair->match n1 n2)]))
(filter (fn [[n1 n2 result]] (= result 99)))
(pmap #(str/join " " (take 2 %)))
(str/join "\n")
(spit "fasttrack_parhaat_parit.txt"))
;; just count
(slurp "fast_track_generoitu_nimilista.txt")
(pmap #(apply pair->match %))
(filter #(= 99 %))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment