Skip to content

Instantly share code, notes, and snippets.

@bernik
Created December 22, 2014 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bernik/5f7c5c52cde8bff71564 to your computer and use it in GitHub Desktop.
Save bernik/5f7c5c52cde8bff71564 to your computer and use it in GitHub Desktop.
(require '[leiningen.exec :as le])
(ns foo
(:require [clojure.string :as str]))
(def source-text "https://gist.githubusercontent.com/pqr/0cf155de6b8f95c3c400/raw/56fc9aa6c06bf3e55a2a02d3e4a857580feff2bc/haskell_vs_php_example.txt")
(defn get-lines [text] (map str/trim (str/split-lines text)))
(defn drop-header-line [lines]
(if (re-find #"Container No\\." (first lines))
(rest lines)
lines))
(defn drop-flag-line [lines]
(cond
(<= (count lines) 20) lines
(re-find #"Flag\\:" (nth lines 20)) (concat (take 20 lines) (drop 21 lines))
:else lines))
(defn lines-without-header-and-flag [lines] (-> lines drop-header-line drop-flag-line))
(defn split-per-block
([lines]
(split-per-block [] lines))
([blocks lines]
(cond
(< (count lines) 20) blocks
(>= (count (lines-without-header-and-flag lines)) 20) (let [first-20-lines (take 20 (lines-without-header-and-flag lines))
other-lines (drop 21 (lines-without-header-and-flag lines))
new-found-blocks (concat blocks first-20-lines)]
(split-per-block new-found-blocks other-lines))
:else blocks)))
(defn parse [text] (-> text
get-lines
split-per-block))
(println "Count: " (count (parse (slurp source-text))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment