Skip to content

Instantly share code, notes, and snippets.

@mharju
Last active August 29, 2015 14:05
Show Gist options
  • Save mharju/817c3fe763369312e16c to your computer and use it in GitHub Desktop.
Save mharju/817c3fe763369312e16c to your computer and use it in GitHub Desktop.
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]
(reduce
(fn [acc value]
(if-not (nil? value)
(update-in acc [value] inc)
acc))
(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))])
name1
name2))]
(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))
result)))]
(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")
(.toLowerCase)
(str/split-lines)
(names->combinations)
(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")
(.toLowerCase)
(str/split-lines)
(names->combinations)
(pmap #(apply pair->match %))
(filter #(= 99 %))
(count))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment