Created
January 18, 2017 22:15
-
-
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)
This file contains hidden or 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 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))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
At the REPL, run this command to print the solution to the problem:
(get-largest-coll 13)