Skip to content

Instantly share code, notes, and snippets.

@FreeAgent
Created January 18, 2017 22:15
Show Gist options
  • Save FreeAgent/7f42c45eb25ca802a71f19b1b4542edf to your computer and use it in GitHub Desktop.
Save FreeAgent/7f42c45eb25ca802a71f19b1b4542edf to your computer and use it in GitHub Desktop.
A solution in Clojure to problem 8 from Project Euler (https://projecteuler.net/problem=8)
(ns largestproduct.core)
(def INPUT_FILE "input.txt")
(defn parse-int
"parse a string & return an integer value"
[s] (Integer. (re-find #"\d+" s )))
(defn get-int-sequence
"strip out newlines & convert chars to ints" [input-data]
(let [num-char-seq (filter #(not= %1 \newline) (seq input-data))
int-char-seq (map #(parse-int (str %1)) num-char-seq)]
int-char-seq))
(defn get-data-coll "read data-file & return sequence of ints" []
(let [raw-data (slurp INPUT_FILE)
num-data (get-int-sequence raw-data)]
num-data))
(defn get-largest-coll
"print the sequence with largest product; col-size is the size of the sequence"
[col-size]
(let [num-data (get-data-coll)
part-data (partition col-size 1 num-data)
products (map #(reduce * %1) part-data)
max-val (apply max products)
lookup (zipmap products part-data)
max-seq (lookup max-val)]
(println max-seq max-val)))
@FreeAgent
Copy link
Author

At the REPL, run this command to print the solution to the problem:
(get-largest-coll 13)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment